Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

stream in and stream out simultaneously

Posted on 2004-03-20
8
Medium Priority
?
351 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

636 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