How to Read File Line by Line in C
Read File Line by Line Using fscanf in C
- Use the
fscanf
Office to Read File Line by Line in C - Use the
fscanf
Function to Read File Word by Discussion in C
This article will explain several methods of how to read a file line by line using fscanf
in C.
Use the fscanf
Role to Read File Line by Line in C
The fscanf
role is part of the C standard library formatted input utilities. Multiple functions are provided for dissimilar input sources like scanf
to read from stdin
, sscanf
to read from the character string, and fscanf
to read from the FILE
pointer stream. The latter tin can exist used to read the regular file line past line and shop them in the buffer.
fscanf
takes formatting specification similar to the printf
specifiers, and all of them are listed in full item on this page. In the post-obit case, nosotros open up the sample input file using the fopen
function call and classify retentivity of total file size to store the read stream into it. "%[^\n] "
format string is specified to read file stream until the new line character is encountered. fscanf
returns EOF
when the cease of the input is reached; thus we iterate with while
loop and impress each line one by one.
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> const char* filename = "input.txt"; int main(int argc, char *argv[]) { FILE *in_file = fopen(filename, "r"); struct stat sb; stat(filename, &sb); char *file_contents = malloc(sb.st_size); while (fscanf(in_file, "%[^\n] ", file_contents) != EOF) { printf("> %s\n", file_contents); } fclose(in_file); exit(EXIT_SUCCESS); }
Output:
> Temporary cord to be written to file
Note that, even though the previous code will virtually likely run successfully if the input file name exists in the filesystem, but multiple part calls tin fail all the same and terminate the program abnormally. The side by side example code is the modified version, where the fault checking routines are implemented.
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> const char* filename = "input.txt"; int main(int argc, char *argv[]) { FILE *in_file = fopen(filename, "r"); if (!in_file) { perror("fopen"); exit(EXIT_FAILURE); } struct stat sb; if (stat(filename, &sb) == -one) { perror("stat"); go out(EXIT_FAILURE); } char *file_contents = malloc(sb.st_size); while (fscanf(in_file, "%[^\n] ", file_contents) != EOF) { printf("> %s\n", file_contents); } fclose(in_file); go out(EXIT_SUCCESS); }
Output:
> Temporary string to be written to file
Use the fscanf
Part to Read File Give-and-take by Word in C
Another useful case to utilize the fscanf
function is to traverse the file and parse every space-separated token. Note that the simply thing to exist inverse from the previous example is the format specifier to "%[^\n ] "
. stat
organization call is to retrieve the file size, and the value is used to pass every bit the malloc
argument to classify the buffer. This method may be wasteful for some scenarios, but it ensures that fifty-fifty the largest single-line files can be stored in the buffer.
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> const char* filename = "input.txt"; int master(int argc, char *argv[]) { FILE *in_file = fopen(filename, "r"); if (!in_file) { perror("fopen"); exit(EXIT_FAILURE); } struct stat sb; if (stat(filename, &sb) == -one) { perror("stat"); get out(EXIT_FAILURE); } char *file_contents = malloc(sb.st_size); while (fscanf(in_file, "%[^\n ] ", file_contents) != EOF) { printf("> %s\n", file_contents); } fclose(in_file); exit(EXIT_SUCCESS); }
Output:
> Temporary > string > to > be > written > to > file
Write for us
DelftStack articles are written by software geeks similar yous. If you lot also would like to contribute to DelftStack by writing paid articles, yous can check the write for the states page.
Related Article - C File
Source: https://www.delftstack.com/howto/c/fscanf-line-by-line-in-c/
0 Response to "How to Read File Line by Line in C"
Postar um comentário