Solved

stream in and stream out simultaneously

Posted on 2004-03-20
8
344 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 86

Assisted Solution

by:jkr
jkr earned 150 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 350 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.

770 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