?
Solved

stream in and stream out simultaneously

Posted on 2004-03-20
8
Medium Priority
?
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10641292
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
ID: 10641325
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
ID: 10641880
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:jkr
jkr earned 600 total points
ID: 10641895
>>Is there any way to read the entire line?

'fgets()' is just fine.
0
 

Author Comment

by:janiescrinc
ID: 10642103
thank you for all of your help.
0
 

Author Comment

by:janiescrinc
ID: 10642131
...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 1400 total points
ID: 10644938
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

764 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