Solved

stream in and stream out simultaneously

Posted on 2004-03-20
8
340 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:janiescrinc
  • 3
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Could you post the full code? The above looks OK. What e.g. is

//do something with the line and then output it

standing for?
0
 
LVL 8

Expert Comment

by:manish_regmi
Comment Utility
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 );
   }
0
 

Author Comment

by:janiescrinc
Comment Utility
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?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 86

Assisted Solution

by:jkr
jkr earned 150 total points
Comment Utility
>>Is there any way to read the entire line?

'fgets()' is just fine.
0
 

Author Comment

by:janiescrinc
Comment Utility
thank you for all of your help.
0
 

Author Comment

by:janiescrinc
Comment Utility
...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.



0
 
LVL 8

Accepted Solution

by:
manish_regmi earned 350 total points
Comment Utility
hi,
 The fgets just worked fine. It should work.
try copying other files. do ypu get the same errror.
Sometimes you get those type of errors if you try to copy binary files. Are u trying to copy binary files.
For text files, your code works fine.

another cause might be that. you are using it in a function. If u call the function first time it will copy. close the handle. In the second time it start copying from the first. In case your src is changing, at the end of your program, you will ger the last entry only.
to avoid this open the file just once (somewhere in main()) and close in the end of the program. This can solve your problem.

If you just want to try out, open  fileDest as "a+"(append).

regards manish
 
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now