I am trying to use fopen and fget to input two files and then output them
into one file. Each input file has two columns and 20 rows, however since
the first column in each input file is same ( numbers 0...19), i want the
output file to have 3 columns and 20 rows. Right now i am using a one
dimensional array "array1[SIZE]" to input each file:, and the output file
has 2 columns and 42 rows.
#include <stdlib.h>
#include <stdio.h>
#define SIZE 20
main()
{
char array1[SIZE], array2[SIZE];
FILE *fp;
if ( (fp = fopen("1a.data" , "r")) == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}
while ( !feof(fp) )
{
fgets(array1, SIZE, fp);
printf("%s",arr ay1);
}
fclose(fp);
printf("\n\n");
if ( (fp = fopen("1b.data" , "r")) == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}
while ( !feof(fp) )
{
fgets(array2, SIZE, fp);
printf("%s",arr ay2);
}
fclose(fp);
return(0);
} 7 1546
On Thu, 26 Feb 2004 01:20:39 GMT, "pillip" <pi****@pillip. com> wrote: I am trying to use fopen and fget to input two files and then output them into one file. Each input file has two columns and 20 rows, however since the first column in each input file is same ( numbers 0...19), i want the output file to have 3 columns and 20 rows. Right now i am using a one dimensional array "array1[SIZE]" to input each file:, and the output file has 2 columns and 42 rows.
There are several approaches to this that would work well enough. The most
general solution, which would work no matter how many rows the two files
have, would be to read one line's worth of data (not necessarily "a line"
as you're doing) from each file, create a line of output right away by
processing that input, and going back for more (if any). In that case you'd
just have three file streams going at the same time.
The other approach, if you really want to read one line at a time, is to
collect the data up in memory and combine it after all the data has been
read. But unless there's some /really/ good reason for this, such as you
have to read to the end of the data before being able to calculate
something that goes at the beginning of the output stream, I'd recommend
the first approach. It is certainly more memory-efficient, anyway.
Since you've said you have two columns ("items") on each line of input, you
may want to consider reading the data in using fscanf to put everything
immediately into some variables, and then creating your output lines with
fprintf. That would make it very easy to select what to write (i.e., skip
the first item from each line of the 2nd file). It would also give you
ready-made hooks for sanity-checking your input as you read it (the return
values from fscanf, and the ability to check the values of the variables
you've just read into).
HTH,
-leor
#include <stdlib.h> #include <stdio.h>
#define SIZE 20
main() { char array1[SIZE], array2[SIZE]; FILE *fp;
if ( (fp = fopen("1a.data" , "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); }
while ( !feof(fp) ) { fgets(array1, SIZE, fp); printf("%s",arr ay1); } fclose(fp);
printf("\n\n");
if ( (fp = fopen("1b.data" , "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); }
while ( !feof(fp) ) { fgets(array2, SIZE, fp); printf("%s",arr ay2); } fclose(fp);
return(0); }
Leor Zolman
BD Software le**@bdsoft.com www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
OK i have tried as you said and it "almost" works. Only problem is that the
ouput file keeps showing the last row of the two input files continously. I
think i am not looping it correctly, but couldnt find out the mistake. Here
is the code:
#include <stdlib.h>
#include <stdio.h>
#define rows 20
main ()
{
FILE *fp;
float value1, value2;
int degree;
int i;
char filename[20];
if ((fp = fopen("1a.data" , "r")) == NULL)
{
fprintf (stderr, "Error opening file.\n");
exit (1);
}
for (i =0; i < rows; i ++)
{
fscanf (fp, "%d %g", °ree, &value1);
}
fclose(fp);
if ((fp = fopen("1b.data" , "r")) == NULL)
{
fprintf (stderr, "Error opening file.\n");
exit (1);
}
for (i =0; i < rows; i ++)
{
fscanf (fp, "%d %g", °ree, &value2);
}
fclose(fp);
puts ("Enter filename:");
gets (filename);
if ((fp = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "Error opening file %s.", filename);
exit (1);
}
for (i=0; i < rows; i++)
{
fprintf(fp, "\n %d %f %f", degree, value1, value2);
}
fclose(fp);
return (0);
}
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:76******** *************** *********@4ax.c om... On Thu, 26 Feb 2004 01:20:39 GMT, "pillip" <pi****@pillip. com> wrote:
I am trying to use fopen and fget to input two files and then output them into one file. Each input file has two columns and 20 rows, however since the first column in each input file is same ( numbers 0...19), i want the output file to have 3 columns and 20 rows. Right now i am using a one dimensional array "array1[SIZE]" to input each file:, and the output file has 2 columns and 42 rows.
There are several approaches to this that would work well enough. The most general solution, which would work no matter how many rows the two files have, would be to read one line's worth of data (not necessarily "a line" as you're doing) from each file, create a line of output right away by processing that input, and going back for more (if any). In that case
you'd just have three file streams going at the same time.
The other approach, if you really want to read one line at a time, is to collect the data up in memory and combine it after all the data has been read. But unless there's some /really/ good reason for this, such as you have to read to the end of the data before being able to calculate something that goes at the beginning of the output stream, I'd recommend the first approach. It is certainly more memory-efficient, anyway.
Since you've said you have two columns ("items") on each line of input,
you may want to consider reading the data in using fscanf to put everything immediately into some variables, and then creating your output lines with fprintf. That would make it very easy to select what to write (i.e., skip the first item from each line of the 2nd file). It would also give you ready-made hooks for sanity-checking your input as you read it (the return values from fscanf, and the ability to check the values of the variables you've just read into).
HTH, -leor
/tools/stlfilt.html
On Thu, 26 Feb 2004 03:01:34 GMT, "pillip" <pi****@pillip. com> wrote: OK i have tried as you said and it "almost" works. Only problem is that the ouput file keeps showing the last row of the two input files continously. I think i am not looping it correctly, but couldnt find out the mistake. Here is the code:
[snip]
You're reading in 20 rows, each time overwriting the same temporary
variables. When it is done reading, all you have is the last row's worth of
data, which you then write out 20 times.
You may want to re-read what I wrote... if you want to take my first
(suggested) approach, you'll need /three/ FILE *'s going all at once, two
for reading and one for writing. Open all three files, and in /each/
iteration of the loop read two lines and write out one.
-leor
Leor Zolman
BD Software le**@bdsoft.com www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
On Thu, 26 Feb 2004 01:20:39 +0000, pillip wrote: I am trying to use fopen and fget to input two files and then output them into one file. Each input file has two columns and 20 rows, however since the first column in each input file is same ( numbers 0...19), i want the output file to have 3 columns and 20 rows. Right now i am using a one dimensional array "array1[SIZE]" to input each file:, and the output file has 2 columns and 42 rows.
#include <stdlib.h> #include <stdio.h>
#define SIZE 20
main()
int main(void) /* This will be mandatory in C99 and is good practice now */
{ char array1[SIZE], array2[SIZE]; FILE *fp;
if ( (fp = fopen("1a.data" , "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1);
exit(EXIT_FAILU RE); /* 1 is not portable */
}
while ( !feof(fp) )
You don't want this test. feof() won't return true until after you attempt
to read beyond the end of the file, at which point you will have printed
some kind of garbage with the printf() statement below.
{ fgets(array1, SIZE, fp); printf("%s",arr ay1); } fclose(fp);
printf("\n\n");
if ( (fp = fopen("1b.data" , "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1);
see above
}
while ( !feof(fp) )
see above
{ fgets(array2, SIZE, fp); printf("%s",arr ay2); } fclose(fp);
return(0);
nothing wrong with return(0), but since you have included stdlib.h, you
can also return EXIT_SUCCESS, if you want.
}
Sorry I didn't answer your question. I don't understand it completely, and
since you didn't provide 1a.data, and 1b.data, I can't run it on my
machine to see what you are talking about.
--Mac
On Thu, 26 Feb 2004 03:01:34 +0000, pillip wrote: OK i have tried as you said and it "almost" works. Only problem is that the ouput file keeps showing the last row of the two input files continously. I think i am not looping it correctly, but couldnt find out the mistake. Here is the code:
#include <stdlib.h> #include <stdio.h>
#define rows 20
it is customary, when #defining some constant, to use all caps, like this:
#define ROWS 20 main ()
int main(void) { FILE *fp; float value1, value2; int degree; int i; char filename[20];
if ((fp = fopen("1a.data" , "r")) == NULL) { fprintf (stderr, "Error opening file.\n"); exit (1);
exit(EXIT_FAILU RE);
}
for (i =0; i < rows; i ++) { fscanf (fp, "%d %g", °ree, &value1);
fscanf() returns a value. You shouldn't ignore it, since it can help you
figure out if something has gone wrong. Also, in this loop, you just
repeatedly clobber the last value by overwriting the data in degree and
value1.
}
fclose(fp);
if ((fp = fopen("1b.data" , "r")) == NULL) { fprintf (stderr, "Error opening file.\n"); exit (1); }
for (i =0; i < rows; i ++) { fscanf (fp, "%d %g", °ree, &value2);
This loop has the same problem as the loop above.
}
fclose(fp);
puts ("Enter filename:"); gets (filename);
Never use gets(). It is impossible to use it safely. Use fgets(filename,
20, stdin), or scanf("%19s", filename) instead. if ((fp = fopen(filename, "w")) == NULL) { fprintf(stderr, "Error opening file %s.", filename); exit (1);
see above
}
for (i=0; i < rows; i++) { fprintf(fp, "\n %d %f %f", degree, value1, value2);
It is fairly obvious that this loop will print the same value over and
over again. I think you want one loop, two input files, and one output
file. You could also just use stdout for output, until you get the thing
working the way you want.
} fclose(fp);
return (0); } "Leor Zolman" <le**@bdsoft.co m> wrote in message news:76******** *************** *********@4ax.c om... On Thu, 26 Feb 2004 01:20:39 GMT, "pillip" <pi****@pillip. com> wrote:
>I am trying to use fopen and fget to input two files and then output them
[snip] HTH, -leor /tools/stlfilt.html
Don't top-post. It makes the conversation very difficult to follow.
--Mac
Thanks i got it working
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:06******** *************** *********@4ax.c om... On Thu, 26 Feb 2004 03:01:34 GMT, "pillip" <pi****@pillip. com> wrote:
OK i have tried as you said and it "almost" works. Only problem is that
theouput file keeps showing the last row of the two input files continously.
Ithink i am not looping it correctly, but couldnt find out the mistake.
Hereis the code: [snip]
You're reading in 20 rows, each time overwriting the same temporary variables. When it is done reading, all you have is the last row's worth
of data, which you then write out 20 times.
You may want to re-read what I wrote... if you want to take my first (suggested) approach, you'll need /three/ FILE *'s going all at once, two for reading and one for writing. Open all three files, and in /each/ iteration of the loop read two lines and write out one. -leor
Leor Zolman BD Software le**@bdsoft.com www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix C++ users: Download BD Software's free STL Error Message Decryptor at www.bdsoft.com/tools/stlfilt.html
On Wed, 25 Feb 2004 20:54:03 -0800, "Mac" <fo*@bar.net> wrote: On Thu, 26 Feb 2004 03:01:34 +0000, pillip wrote: char filename[20];
puts ("Enter filename:"); gets (filename);
Never use gets(). It is impossible to use it safely. Use fgets(filename, 20, stdin), or scanf("%19s", filename) instead.
But in the former case, you must then (find and) strip the trailing
\n, unless you actually want (and your system allows!) filenames
containing a newline; and in the latter case you cannot have filenames
containing whitespace (not permitted on some systems anyway).
- David.Thompson1 at worldnet.att.ne t This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dariusz |
last post by:
I want to use arrays in my website (flat file for a guestbook), but despite
having read through countless online tutorials on the topic, I just can't
get my code to work.
I know there are guestbook scripts out there - but that doesn't help me
learn how to programme arrays !!!
The following is the code for the PHP (called externally), which does
execute...
|
by: Psykarrd |
last post by:
I am trying to declare a string variable as an array of char's. the
code looks like this.
char name;
then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.
|
by: Pasacco |
last post by:
dear
I want to ask help on this problem.
Array a is partitioned into a0 and a1 in main(). Then
a1 is partitioned into a2 and a3 in th_partition()
function. And I think this problem is something about parameter
passing.
If someone give me comment, it will be thankful.
thankyou very much
|
by: John Smith |
last post by:
I have a two dimentional char array. Before filling it using
strtok(), I reset its elements to '\0' using two nested for loops.
The code works as I hope it would but I wonder whether I really
need to reset the array. The program would run faster if I don't
need to reset.
------------------------------------------
int Array(void)
.................
|
by: Thomas Connolly |
last post by:
Anyone know if there is a C# equivallent to:
enum
{
LIFFE_SIZE_AUTOMARKETREF = 15
};
typedef char LiffeAutoMarketReference ;
Thanks,
| |
by: hothead098 |
last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material)
For this assignment you are to:
1) Create and manage arrays
a) One of type integers (containing 10 elements).
b) One of type strings (containing 20 elements).
c) One of type char (containing 30 elements).
|
by: vinod.bhavnani |
last post by:
Hello all,
I need desperate help
Here is the problem:
My problem today is with multidimensional arrays.
Lets say i have an array A this is a 4 dimensional
static array.
|
by: rllioacvuher |
last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one to use as an index to the freearray cells).
Here is the exact problem specifications:
Create an ordered list template class named OLType to implement an ordered list with operations of insert, remove, print, empty, full, size. The storage...
|
by: thegreatest21 |
last post by:
Right, I am making a Tax Calculator and need an array to store the data that has been typed in when the user is prompted. However, being completely new to Java I am having some difficulty getting to grip with Arrays...so I am here to ask for help and see what help I can get. Here's the code that I already have;
System.out.println("Please Enter In Your First Name: ");
String firstName = bufRead.readLine();
...
|
by: sumalats |
last post by:
Hello,
I need to use Visual basic to acquire some data through the serial port, store it etc. As I am just getting acquainted with VB6 I need some help.
My Controller board (8 bit micro) sends out serial data 3 bytes at a time ( a packet). The data are typically binary bytes and can have values from 0 to 255. To signal the start of a set of bytes the CTS line is given a high. The data set is sent at intervals of 35 milliseconds. I need to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |