All C program code Examples

File / Management

// write char values to File using putc() or fputc() function

#include

int main()
{
FILE *fp;
char ch;

fp = fopen( “file/data-1.txt”, “w” );

printf( ” Enter String ( ctrl+z to end ) : ” );
ch = getchar();
while( ch != EOF )
{
putc( ch, fp ); /* fputc( ch, fp ); */
ch = getchar();
}

fclose( fp );

return 0;
}

Output:

Enter String ( ctrl+z to end ) : C Programming Examples^Z

// read char values from File using getc() or fgetc() function

#include

int main()
{
FILE *fp;
char ch;

fp = fopen( “file/data-1.txt”, “r” );

printf( “String : ” );
ch = getc( fp ); /* ch = fgutc( fp ); */
while( ch != EOF )
{
printf( “%c”, ch );
ch = getc( fp ); /* ch = fgutc( fp ); */
}

fclose( fp );

return 0;
}

Output:

String : C Programming Examples

// write integer values to File using putw() function

#include

int main()
{
FILE *fp;
int n;

fp = fopen( “file/data-1.txt”, “w” );

while( 1 )
{
printf( ” Enter Number ( -1 to end ) : ” );
scanf( “%d”, &n );
if( n == -1 )
break;
putw( n, fp );
}

fclose( fp );

return 0;
}

Output:

Enter Number ( -1 to end ) : 20
Enter Number ( -1 to end ) : 40
Enter Number ( -1 to end ) : 50
Enter Number ( -1 to end ) : -1

// read integer values from File using getw() function

#include

int main()
{
FILE *fp;
int n;

fp = fopen( “file/data-1.txt”, “r” );

printf( “\n Numbers are :” );
n = getw( fp );
while( n != EOF )
{
printf( ” %d”, n );
n = getw( fp );
}

fclose( fp );

return 0;
}

Output:

Numbers are : 20 40 50

// write string to File using fputs() function

#include

int main()
{
FILE *fp;
char s[50];

fp = fopen( “file/data-1.txt”, “w” );

printf( ” Enter String : ” );
gets( s );

fputs( s, fp );
fputs( “\n”, fp );

fclose( fp );

return 0;
}

Output:

Enter String : C Programming Examples

// read string values from File using fgets() function

#include

int main()
{
FILE *fp;
char s[50];

fp = fopen( “file/data-1.txt”, “r” );

printf( “\n String = ” );
fgets( s, 49, fp );
printf( “%s”, s );

fclose( fp );

return 0;
}

Output:

String = C Programmming Examples

// write multiple lines of text to File using fputs() function

#include

#include

int main()
{
FILE *fp;
char s[50];

fp = fopen( “file/data-1.txt”, “w” );

printf( ” Enter Lines of Text : \n” );
gets( s );

while( strlen(s) > 0 )
{
fputs( s, fp );
fputs( “\n”, fp );
gets( s );
}

fclose( fp );

return 0;
}

Output:

Enter Lines of Text :
C
Programming
Examples

// read multiple lines of text from file using fgets() function

#include

int main()
{
FILE *fp;
char s[50];

fp = fopen( “file/data-1.txt”, “r” );

printf( ” Entered Text : \n” );
fgets( s, 49, fp );
while( !feof( fp ) )
{
printf( “%s”, s );
fgets( s, 49, fp );
}

fclose( fp );

return 0;
}

Output:

Entered Text :
C
Programming
Examples

// write different types of values to File using fprintf() function

#include

int main()
{
FILE *fp;
int rno = 290;
char name[50] = “Nils”;
float marks = 84.69;

fp = fopen( “file/data-1.txt”, “w”);
fprintf( fp, “%d %s %f\n”, rno, name, marks );
printf( “\n Details saved in file.” );

fclose( fp );

return 0;
}

Output:

Details saved in file.

// read different types of values from File using fscanf() function

#include

#include

int main()
{
FILE *fp;
int rno;
char name[50];
float marks;

fp = fopen( “test”, “r”);
fscanf( fp, “%d %s %f”, &rno, name, &marks );

printf( “\n Details from file : ” );
printf( “\n Roll No. : %d”, rno );
printf( “\n Name : %s”, name );
printf( “\n Marks : %.2f”, marks );

fclose( fp );

return 0;
}

Output:

Details from file :
Roll No. : 290
Name : Nils
Marks : 84.69

// use getc() & putc() function with console ( stdin & stdout )

#include

int main( )
{
char ch ;

printf( “\n Enter String( ctrl+z to end ):\n” );
while ( ( ch = getc ( stdin ) ) != EOF )
{
putc ( ch, stdout ) ;
}

return 0;
}

Output:

Enter String( ctrl+z to end ):
Nils
Patel
^Z

// read structure variable from File using fread() function

#include

#include

struct student
{
int rno;
char name[50];
float marks;
};

int main( )
{
FILE *fp;

struct student s;

fp = fopen( “students”, “rb” );
fread( &s, sizeof( s ), 1, fp );

printf( “\n Roll No. : %d”, s.rno );
printf( “\n Enter Name : %s”, s.name );
printf( “\n Enter Marks : %.2f”, s.marks );

fclose( fp ) ;

return 0;
}

Output:

Roll No. : 290
Enter Name : Nils
Enter Marks : 84.69

// write structure variable to File using fprintf() function

#include

struct student
{
int rno;
char name[50];
float marks;
};

int main( )
{
FILE *fp;

struct student s;

fp = fopen( “students”, “w” );

printf( ” Enter Roll No. : ” );
scanf( “%d”, &s.rno );
printf( ” Enter Name : ” );
fflush( stdin );
gets( s.name );
printf( ” Enter Marks : ” );
scanf( “%f”, &s.marks );

fprintf( fp, “%d %s %f\n”, s.rno, s.name, s.marks );

fclose( fp ) ;

return 0;
}

Output:

Enter Roll No. : 23
Enter Name : Nils
Enter Marks : 92.5

// read structure variable from File using fscanf() function

#include
#include

struct student
{
int rno;
char name[50];
float marks;
};

int main( )
{
FILE *fp;

struct student s;
clrscr();

fp = fopen( “students”, “r” );
fscanf( fp, “%d %s %f”, &s.rno, s.name, &s.marks );

printf( “\n Roll No. : %d”, s.rno );
printf( “\n Enter Name : %s”, s.name );
printf( “\n Enter Marks : %.2f”, s.marks );

fclose( fp ) ;

return 0;
}

Output:

Roll No. : 23
Enter Name : Nils
Enter Marks : 92.5

// write structure variables to File using fprintf() function to store multiple record

#include

struct student
{
int rno;
char name[50];
float marks;
};

int main( )
{
FILE *fp;
char another = ‘Y’;

struct student s;

fp = fopen ( “students”, “w” ) ;

while ( another == ‘Y’ || another == ‘y’ )
{
printf( “\n Enter Roll No, Name & Marks : ” );
scanf( “%d %s %f”, &s.rno, s.name, &s.marks );
fprintf( fp, “%d %s %f\n”, s.rno, s.name, s.marks );
printf( ” Add another record (Y/N) : ” );
fflush( stdin );
another = getche( );
}

fclose( fp );

return 0;
}

Output:

Enter Roll No, Name & Marks : 23 Nils 84.5
Add another record (Y/N) : y
Enter Roll No, Name & Marks : 22 Pintu 81.3
Add another record (Y/N) : n