In: Computer Science
#include <stdio.h>
int main()
{
FILE *fp1;
char c;
fp1= fopen ("C:\\myfiles\\newfile.txt", "r");
while(1)
{
c = fgetc(fp1);
if(c==EOF)
break;
else
printf("%c", c);
}
fclose(fp1);
return 0;
}
1) In the above program the statement which is functioning for
opening a file is:-
fp1= fopen ("C:\\myfiles\\newfile.txt", "r");
2) Syntax for opening a file is:-
ptr= fopen("fileopen","mode");
3) The mode that is basically used in files is:-
"r" - Open for reading
"w" - Open for writing
"a" - Open for append, Data is added to the end of file
"r+" - Open for both reading and writing
"w+" - Open for both reading and writing
"a+" - Open for both reading and appending
The Opening mode that being used in the program is "Open for
Reading".
Ex:- fp1= fopen ("C:\\myfiles\\newfile.txt", "r");
Here "r" denotes that file is in Reading mode only.
4) In pointer "fp" it points to the file from where we are
reading the data, the address of this
("C:\\myfiles\\newfile.txt") file is located in this fp.
5) In the above program if(c==EOF) return true then it means
file is not opened. Otherwise if this condition is false declare
that file is opened.
Here, C is a pointer points to the file and EOF denotes
End-of-file.
6) The Code that can be written to check whether a file is
opened or not is as follow:-
if(c==EOF)
{
// Condition true means file is not opened.
printf("File is not opened");
break;
}
else
{
// File is opened and it reads the character in the files.
printf("%c",c);
}
7) The file read operation can be performed by both fscanf and
fgets. It also perform the same operation as
function scanf and gets but with additional parameter that is file
pointer. So, it depends on you whether you want
to read file line by line or character by character.
Ex:-
FILE *fp1
fp1= fopen ("C:\\myfiles\\newfile.txt",
"r");
// let the file having text written as January 2020.
fscanf(fp1, "%s %d", str1,&year);
8) The file write operation can be performed by the function
fprintf and fputs and fputc (for character)
works as same that of printf and puts.
having an additional parameter that is file pointer.
Ex:-
FILE *fp1;
fp1 = fopen(“fileName.txt”,
“w”);
#include <stdio.h>
int main()
{
char c;
FILE *fp;
fp = fopen("C:\\myfile.txt","w");
if(fp == NULL)
{
printf("File not opened");
break;
}
printf("Enter any character: ");
scanf("%c",&c);
//Input you entered is stored in the file
fputc(c, fp);
fclose(fp);
return 0;
}
9) Function used for closing a file is fclose
function:-
Ex:-
FILE *fp1
fp1= fopen ("C:\\myfiles\\newfile.txt",
"r");
// .............some operation performed...............
fclose(fp1);
Note:- To close file after doing file operation is must necessary at the end of program.
10)
#include <stdio.h>
int main() {
FILE * fp;
char c;
//open a file
fp = fopen("myfiles.txt", "w");
//writing operation
while ((c = getchar()) != EOF) {
putc(c, fp);
}
//close file
fclose(fp);
printf("Data Entered:\n");
//reading operation
fp = fopen("myfiles.txt", "r");
while ((c = getc(fp)) != EOF) {
printf("%c", c);
}
//close file
fclose(fp);
return 0;
}
11) Read a I/O string file in C:-
// Read
text until new line is encountered:-
fscanf(fp,"%[^\n]",c);
printf("Data from the file:\n %s",c);
fclose(fp);
Write a I/O string file in C:-
printf("Enter your string:");
// Stored the input string into array "str"
gets(str);
// Copied the content of str into file using pointer
"fp"
fputs(str, fp);
fclose(fp);
12)
#include <stdio.h>
int main()
{
FILE *fp;
//Char array to store string
char str[100];
//Opening the file in mode "r"
fp = fopen("C:\\mynewtextfile.txt", "r");
//Check if there is any issue in opening the file
if (fp == NULL)
{
puts("Issue in opening the input file");
}
//Loop for reading the file till end
while(1)
{
if(fgets(str, 10, fp) ==NULL)
break;
else
printf("%s", str);
}
//Closing the input file
fclose(fp);
return 0;
}
Here, we check condition fgets(str,10,fp) because fgets returns
NULL when there is no more records are available to be read.
str - Represent the string in which it stores the string reading it
from file.
10 - length of string that need to be read everytime
fp - pointer to the file, which is going to read the file