C++ Read File Into String Line by Line
Read File Line by Line Using fscanf in C
- Use the
fscanfFunction to Read File Line by Line in C - Use the
fscanfRole to Read File Word by Word in C
This article will explain several methods of how to read a file line by line using fscanf in C.
Apply the fscanf Function to Read File Line by Line in C
The fscanf function is part of the C standard library formatted input utilities. Multiple functions are provided for different input sources like scanf to read from stdin, sscanf to read from the grapheme string, and fscanf to read from the FILE arrow stream. The latter can be used to read the regular file line by line and store them in the buffer.
fscanf takes formatting specification like to the printf specifiers, and all of them are listed in full particular on this folio. In the following case, nosotros open the sample input file using the fopen function call and classify memory of total file size to store the read stream into it. "%[^\due north] " format string is specified to read file stream until the new line character is encountered. fscanf returns EOF when the end of the input is reached; thus we iterate with while loop and print each line one past i.
#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("> %southward\northward", file_contents); } fclose(in_file); get out(EXIT_SUCCESS); } Output:
> Temporary string to be written to file Annotation that, even though the previous code will most likely run successfully if the input file name exists in the filesystem, only multiple part calls can fail withal and terminate the programme abnormally. The side by side example code is the modified version, where the error 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"); go out(EXIT_FAILURE); } struct stat sb; if (stat(filename, &sb) == -1) { perror("stat"); leave(EXIT_FAILURE); } char *file_contents = malloc(sb.st_size); while (fscanf(in_file, "%[^\n] ", file_contents) != EOF) { printf("> %southward\n", file_contents); } fclose(in_file); exit(EXIT_SUCCESS); } Output:
> Temporary cord to exist written to file Use the fscanf Role to Read File Word past Discussion in C
Some other useful case to utilize the fscanf function is to traverse the file and parse every space-separated token. Note that the only thing to be inverse from the previous instance is the format specifier to "%[^\north ] ". stat arrangement phone call is to retrieve the file size, and the value is used to pass equally the malloc argument to allocate the buffer. This method may exist wasteful for some scenarios, but it ensures that fifty-fifty the largest unmarried-line files tin exist 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) == -one) { perror("stat"); get out(EXIT_FAILURE); } char *file_contents = malloc(sb.st_size); while (fscanf(in_file, "%[^\n ] ", file_contents) != EOF) { printf("> %south\n", file_contents); } fclose(in_file); leave(EXIT_SUCCESS); } Output:
> Temporary > string > to > be > written > to > file Write for us
DelftStack manufactures are written past software geeks like you. If you too would similar to contribute to DelftStack by writing paid manufactures, you can check the write for usa folio.
Related Article - C File
sanabriagaill1942.blogspot.com
Source: https://www.delftstack.com/howto/c/fscanf-line-by-line-in-c/
Belum ada Komentar untuk "C++ Read File Into String Line by Line"
Posting Komentar