Solved

stream in and stream out simultaneously

Posted on 2004-03-20
8
345 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
port mapped I/o query 10 203
Certified Pentester Exam - need assistance on compiling an exploit 6 403
Read file descriptor 3 in C 14 112
Windows Server 2012 R2 - connect to computer 13 33
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 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.

766 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