janiescrinc
asked on
stream in and stream out simultaneously
HI
I am trying to stream a file in and then stream out the line to a different file
example:
CString fileSource ="in.txt";
CString fileDest ="out.txt";
FILE *in_stream;
FILE *out_stream;
char line[100];
if( (in_stream = fopen( fileSource, "r" )) != NULL )
{
if(out_stream = fopen(fileDest,"w")) !=NULL)
while( fgets( line, 100, in_stream )){
.... //do something with the line and then output it
fprintf(out_stream, "%s", line);
}
fclose(out_stream);
}
fclose( in_stream );
}
both files are opened and the first line is read.
after that nothing happens.
I cannot get this to work.
I have heard of memory mapping files but I have no Idea how to do this, or if it would solve my problem.
Thanks for any suggestions.
I am trying to stream a file in and then stream out the line to a different file
example:
CString fileSource ="in.txt";
CString fileDest ="out.txt";
FILE *in_stream;
FILE *out_stream;
char line[100];
if( (in_stream = fopen( fileSource, "r" )) != NULL )
{
if(out_stream = fopen(fileDest,"w")) !=NULL)
while( fgets( line, 100, in_stream )){
.... //do something with the line and then output it
fprintf(out_stream, "%s", line);
}
fclose(out_stream);
}
fclose( in_stream );
}
both files are opened and the first line is read.
after that nothing happens.
I cannot get this to work.
I have heard of memory mapping files but I have no Idea how to do this, or if it would solve my problem.
Thanks for any suggestions.
hi,
fgets stops reading after getting a new line character. it returns NULL and your loop stops.
so read till eof. using fgetc.
if( (in_stream = fopen( fileSource, "r" )) != NULL )
{
if(out_stream = fopen(fileDest,"w")) !=NULL)
while((i = fgetc(in_stream)) != EOF){
fputc(i,out_stream);
}
fclose(out_stream);
}
fclose( in_stream );
}
fgets stops reading after getting a new line character. it returns NULL and your loop stops.
so read till eof. using fgetc.
if( (in_stream = fopen( fileSource, "r" )) != NULL )
{
if(out_stream = fopen(fileDest,"w")) !=NULL)
while((i = fgetc(in_stream)) != EOF){
fputc(i,out_stream);
}
fclose(out_stream);
}
fclose( in_stream );
}
ASKER
Hi,
I used fgets because it read in the text file line by line.
fgetc reads in characters not lines...
Is there any way to read the entire line?
I used fgets because it read in the text file line by line.
fgetc reads in characters not lines...
Is there any way to read the entire line?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you for all of your help.
ASKER
...actually the problem is that the project was upgraded from VC++ 5 to VC++ 7
The file streaming didn't work.
To fix the error I simply created a new Console project in VC++ and added MFC support.
After cutting and pasting the code into the new VC++ 7 project. The streaming worked.
The file streaming didn't work.
To fix the error I simply created a new Console project in VC++ and added MFC support.
After cutting and pasting the code into the new VC++ 7 project. The streaming worked.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
//do something with the line and then output it
standing for?