File Handling in C (Reading a file line by line)
In this tutorial, we are working with the files in the C.
File handling in any language requires Three main steps to follow
We are using fopen(file stream).
there is some modes to open a file.
Without wasting much time lets start working.
Create a New C file of whatever name you want mine is files.c.
create the basic schema of C language.
Create a FILE pointer to point a stream and open a file into the read mode using "r".
you may need to include the string.h to your headers.
the data of the file is
output of this code is
this is reading from the file.
we can also try for the append mode but to keep the tutorial short let's wrap it up here. append works in the same way.
in this mode, previous data is not deleted and new data appended to the file.
Thanks for being here.
if you loved this post please check my other post for the file handling in c++
you can also check out my other post for the various programming language.
File handling in any language requires Three main steps to follow
- Open the file in Read/Write mode.
- Do various operation that you want to do.
- Close the file.
We are using fopen(file stream).
there is some modes to open a file.
- Read mode opens file into the read mode if file not found returns -1.
- Write mode opens file into the write mode if the file does not present creates a new file if there is previous data it will be overwritten.
- Append mode opens the file in the append mode and append data to it
Without wasting much time lets start working.
Create a New C file of whatever name you want mine is files.c.
create the basic schema of C language.
#include <stdio.h>
int main(int argc, char const *argv[])
{
/* code */
return 0;
}
Read From the file
Create a FILE pointer to point a stream and open a file into the read mode using "r".
you may need to include the string.h to your headers.
the data of the file is
#include <stdio.h>
int main(){
FILE *fp;
// suppose the size of the line is 128 character.
char line[128];
fp = fopen("data.txt", "r");
while (fgets(line, sizeof(line), fp)) {
printf("%s\n",line );
}
fclose(fp);
return 0;
}
output of this code is
this is reading from the file.
Write to a File
so for the writing, we have to open the same file into the write mode.
example program for this is.
#include <stdio.h>
int main(){
FILE *fp;
// suppose the size of the line is 128 character.
char line[128];
fp = fopen("data.txt", "w");
for (int iter = 0; iter < 20; ++iter)
{
fprintf(fp, "%d\n",iter );
}
printf("Data written to file\n");
fclose(fp);
return 0;
}
the output of this program is.
we can also try for the append mode but to keep the tutorial short let's wrap it up here. append works in the same way.
in this mode, previous data is not deleted and new data appended to the file.
Thanks for being here.
if you loved this post please check my other post for the file handling in c++
you can also check out my other post for the various programming language.
Comments
Post a Comment