Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: C Language File Handling Notes
Description: This is very Easy Notes For Student can Easily Learn

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


C Tutorial - File Handling

Files let you store data on secondary storage such as a hard disk so that your programs can later
retrieve that data again
...


Text files
Text files are used for storing character strings in a file
...

Code:

1
2
3
4
5
6
7
#include ...
fopen takes 2 parameters which are the file
name and the open mode
...

Code:

1
2
3

4
5
6
7
8
#include ...
txt","w");
return 0;
}

The above example will create a file called test
...
The "w" means that the file is being opened for
writing and if the file does not exist then it will be created
...
If the file exists then make
it blank
...
If the file
exists then make it blank
...


a+

Open for reading and appending and create the file if it does not exist
...
fprintf is just like printf except that you
must use the file pointer as the first parameter
...
h>

int main()
{
FILE *f;
f = fopen("test
...
If you do not close a file then some of
the data might not be written to it
...

Code:

1
2
3
4
5
6
7
8
9
10
#include ...
txt","w");
fprintf(f,"Hello");

fclose(f);
return 0;
}

The fgets command is used when reading from a text file
...
The 3 parameters for fgets are the buffer
variable, the size of the buffer and the file pointer
...
h>

int main()
{
FILE *f;
char buf[100];
f = fopen("test
...
When you open a data file you must add
the letter b to the open mode parameter of fopen
...
h>

int main()
{
FILE *f;
f = fopen("test
...
The first parameter of fwrite is a pointer to the variable that you want
to write to the file
...
The third
parameter is the number of variables to be written
...

Code:

1
2
3
4
5
6
7

8
9
10
11
12
#include ...
dat","wb");
buf = 100;
fwrite(&buf,sizeof(buf),1,f);
fclose(f);
return 0;
}

fread is used to read from a file and is the same as fwrite except that the data is read into the
variable instead of writing from it
...

Code:

1
2
3
4
5
6
7
8
9
10
11
12
#include ...
dat","rb");
fread(&buf,sizeof(buf),1,f);
printf("%d\n",buf);
fclose(f);
return 0;
}

Data files using structures
You can read and write structures to data files in the same way as you do with any other data type
...
h>

struct
{
char name[100];
int age;
} p;

int main()
{
FILE *f;
strcpy(p
...
age = 25;
f = fopen("test
...
User is also asked for the line
number that is to be deleted
...

The file is opened and all the data is transferred to another file except that one line the user specifies
to delete
...
h>
int main() {
FILE *fp1, *fp2;
//consider 40 character string to store filename
char filename[40];
char c;
int del_line, temp = 1;
//asks user for file name
printf("Enter file name: ");
//receives file name from user and stores in 'filename'
scanf("%s", filename);
//open file in read mode
fp1 = fopen(filename, "r");
c = getc(fp1);
//until the last character of file is obtained
while (c != EOF)
{

printf("%c", c);
//print current character and read next character
c = getc(fp1);
}
//rewind
rewind(fp1);
printf(" \n Enter line number of the line to be deleted:");
//accept number from user
...
c", "w");
c = getc(fp1);
while (c != EOF) {
c = getc(fp1);
if (c == '\n')
temp++;
//except the line to be deleted
if (temp != del_line)
{
//copy all lines in file copy
...

fclose(fp1);
fclose(fp2);
//remove original file
remove(filename);
//rename the file copy
...
c", filename);
printf("\n The contents of file after being modified are as follows:\n");
fp1 = fopen(filename, "r");
c = getc(fp1);
while (c != EOF) {
printf("%c", c);
c = getc(fp1);

}
fclose(fp1);
return 0;
}

Output:
Enter file name:abc
...

Hello
how are you?
I am fine
hope the same
Enter line number of the line to be deleted:4
The contents of file after being modified are as follows:
hi
...
Entered file name is stored in
a char array 'filename'
...
Character 'c' is used
to read characters from the file and print them to the output
...
The file pointer is rewinded back and all the lines of the file except for the line to be
deleted are copied into another file "copy
...
Now "copy
...
The
original file is opened in read mode and the modified contents of the file are displayed on the screen
...
h>
int main(void) {
FILE *fp1, *fp2;
//'filename'is a 40 character string to store filename
char filename[40];
char c;
int del_line, temp = 1;
//asks user for file name
printf("Enter file name: ");
//receives file name from user and stores in 'filename'
scanf("%s", filename);
fp1 = fopen(filename, "r");
//open file in read mode
c = getc(fp1);
//print the contents of file
...

printf(" Enter line number to be deleted and replaced");
scanf("%d", &del_line);
//take fp1 to start point
...
c in write mode
fp2 = fopen("copy
...
c", filename);
fp1 = fopen(filename, "r");
//reads the character from file
c = getc(fp1);
//until last character of file is encountered
while (c != EOF){
printf("%c", c);
//all characters are printed
c = getc(fp1);

}
//close the file pointer
fclose(fp1);
return 0;
}

Output:
Enter file name:abc
...

hello
how are you?
hope the same
Enter line number of the line to be deleted and replaced:4
Enter new text: sayonara see you soon
hi
...
The File by name entered by user is
opened in read mode
...
Next the data to
be replaced is asked
...
c"
...
New data is stored in
its place and remaining lines of the original file are also transferred
...
Both the file pointers are closed and the original file
is again opened in read mode and the contents of the original file is printed as output
...

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include ...

sample_chr = getc(fp);
}
fclose(fp); //close file
...
txt
There are 4 lines in abc
...
A file by the given name is opened in
read-mode using a File pointer 'fp'
...
If a new line character(' ') is encountered, the integer variable
'no_lines' is incremented
...
This process is continued until the last character of the file(EOF) is
encountered
...



Title: C Language File Handling Notes
Description: This is very Easy Notes For Student can Easily Learn