C Code Read File Line by Line
Read File Line by Line Using fscanf in C
- Apply the
fscanf
Function to Read File Line by Line in C - Use the
fscanf
Part to Read File Word by Discussion in C
This article will explicate several methods of how to read a file line by line using fscanf
in C.
Utilise 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 unlike 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 can exist used to read the regular file line by line and store them in the buffer.
fscanf
takes formatting specification similar to the printf
specifiers, and all of them are listed in full detail on this page. In the following example, we open the sample input file using the fopen
function call and allocate retentiveness of full file size to store the read stream into information technology. "%[^\northward] "
format string is specified to read file stream until the new line grapheme is encountered. fscanf
returns EOF
when the finish of the input is reached; thus we iterate with while
loop and print 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, "%[^\northward] ", file_contents) != EOF) { printf("> %southward\n", file_contents); } fclose(in_file); leave(EXIT_SUCCESS); }
Output:
> Temporary string to be written to file
Annotation that, even though the previous code volition most probable run successfully if the input file name exists in the filesystem, but multiple office calls can neglect notwithstanding and finish the program abnormally. The next example lawmaking 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) == -1) { perror("stat"); exit(EXIT_FAILURE); } char *file_contents = malloc(sb.st_size); while (fscanf(in_file, "%[^\due north] ", file_contents) != EOF) { printf("> %s\northward", file_contents); } fclose(in_file); exit(EXIT_SUCCESS); }
Output:
> Temporary string to exist written to file
Employ the fscanf
Part to Read File Word past Word in C
Another useful instance to apply the fscanf
function is to traverse the file and parse every space-separated token. Note that the just thing to be changed from the previous example is the format specifier to "%[^\n ] "
. stat
system call is to retrieve the file size, and the value is used to pass as the malloc
argument to allocate the buffer. This method may be wasteful for some scenarios, but it ensures that even the largest unmarried-line files can be stored in the buffer.
#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"); leave(EXIT_FAILURE); } struct stat sb; if (stat(filename, &sb) == -ane) { 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 like you lot. If you too would like to contribute to DelftStack past writing paid manufactures, you can check the write for u.s. page.
Related Article - C File
Source: https://www.delftstack.com/howto/c/fscanf-line-by-line-in-c/
0 Response to "C Code Read File Line by Line"
Post a Comment