Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GAH! File I/O!

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
US
I have totally forgotten how to do file I/O in C++..

Can someone give me an example of how to open a file and read from it and then write the data to another file all in text mode? It woudl be handy if I could do this a line at a time as well.
 
Hi, you can use almost 3 strategy:

1) classic standard C approach ( fopen,fgets...)
2) Advanced Windows32bit function ( CreateFile, ReadFile...)
3) Using MFC CFile

I'll show the first

# include <stdio.h>
main()
{
FILE *fin, *fou ;
char buf[80];

fin=fopen(&quot;infile.txt&quot;,&quot;r&quot;); // test error
fou=fopen(&quot;oufile.txt&quot;,&quot;w&quot;); // test error

while( fgets( buf, sizeof(buf)-1, fin ) !=NULL )
{
// buf contains also LF
fprintf( fou, &quot;%s&quot;, buf ) ;
}

fclose( fin ) ;
fclose( fou ) ;

}


BYE
 
If you use CStdioFile (which inherits from CFile), you can use the functions ReadString() and WriteString() to process a text file line-by-line.

CMR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top