C File I/O – Table of Contents
-------------------------------------------------
we will learn how to perform input/output(I/O) operations on a file using C programming language.
1. Opening a File
2. Reading a File
4. Closing a file
A Simple C Program to Open, Read and Close the file
#include <stdio.h>
int main()
{
/* Pointer to the file */
FILE *fp1;
/* Character variable to read the content of file */
char c;
/* Opening a file in r mode*/
fp1= fopen ("C:\\myfiles\\newfile.txt", "r");
/* Infinite loop –I have used the break to come out of the loop*/
while(1)
{
c = fgetc(fp1);
if(c==EOF)
break;
else
printf("%c", c);
}
fclose(fp1);
return 0;
}
In the above program, we are opening a file newfile.txt in r mode, reading the content of the file and displaying it on the console. lets understand the each operation in detail:
-
-------------------------------------------------
we will learn how to perform input/output(I/O) operations on a file using C programming language.
1. Opening a File
2. Reading a File
4. Closing a file
A Simple C Program to Open, Read and Close the file
#include <stdio.h>
int main()
{
/* Pointer to the file */
FILE *fp1;
/* Character variable to read the content of file */
char c;
/* Opening a file in r mode*/
fp1= fopen ("C:\\myfiles\\newfile.txt", "r");
/* Infinite loop –I have used the break to come out of the loop*/
while(1)
{
c = fgetc(fp1);
if(c==EOF)
break;
else
printf("%c", c);
}
fclose(fp1);
return 0;
}
In the above program, we are opening a file newfile.txt in r mode, reading the content of the file and displaying it on the console. lets understand the each operation in detail:
-
No comments:
Post a Comment