#include< stdio. h>
#include< stdlib. h>
int main ( ) {
FILE * pf = fopen ( "test.txt" , "w" ) ;
if ( pf == NULL ) {
printf ( "fail to open file!" ) ;
exit ( - 1 ) ;
}
fputs ( "abcd" , pf) ;
fputs ( "efghi\n" , pf) ;
fputs ( "jklmn\n" , pf) ;
fclose ( pf) ;
pf = NULL ;
return 0 ;
}
#include< stdio. h>
#include< stdlib. h>
int main ( ) {
char line[ 500 ] ;
FILE * fp;
if ( ( fp= fopen ( "test.txt" , "r" ) ) == NULL ) {
printf ( "file fail to open!\n" ) ;
exit ( - 1 ) ;
}
while ( fgets ( line, sizeof ( line) , fp) != NULL ) {
printf ( "%s" , line) ;
}
fclose ( fp) ;
fp = NULL ;
return 0 ;
}
#include < iostream>
#include < fstream>
#include < stdlib. h>
using namespace std;
int main ( ) {
char buffer[ 256 ] ;
fstream in ( "test.txt" , ios: : in ) ;
if ( ! in . is_open ( ) ) {
cout << "Error opening file" ; exit ( 1 ) ;
}
while ( ! in . eof ( ) ) {
in . getline ( buffer, 100 ) ;
cout << buffer << endl;
}
in . close ( ) ;
return 0 ;
}
#include < iostream>
#include < fstream>
#include < stdlib. h>
using namespace std;
int main ( ) {
char buffer[ 256 ] ;
fstream out ( "out.txt" , ios : : out || ios: : app) ;
if ( out. is_open ( ) ) {
out << "This is a line.\n" ;
out << "This is another line.\n" ;
}
out. close ( ) ;
return 0 ;
}