How to Read From a Txt File C++

Read a File in C

  1. Apply fopen and fread Functions to Read Text File in C
  2. Use fopen and getline Functions to Read Text File in C

This article will introduce multiple methods most how to read a text file in C.

Use fopen and fread Functions to Read Text File in C

fopen and fread functions are office of the C standard library's input/output facilities.

fopen is used to open the given file equally a stream and give the programme the handle to dispense it equally needed. Information technology takes two parameters, the name of the file as const char* string and mode in which to open up the file specified with predefined values (r, west, a, r+, w+, a+). When we demand to read a file, nosotros pass r as the second parameter to open the file in read-only mode.

On the other hand, the fread function is the main thespian in conducting the read operation from an already opened file stream. It takes a pointer to buffer where the read bytes should be stored as the start statement. The 2nd and third arguments specify how many items to read from the stream and what size does each of them has. The terminal argument is the FILE* arrow to the opened file stream itself.

Discover that we are also using the stat function to think the file size and read the whole contents in a single fread call. The file size is passed to the malloc function as well for allocating plenty space to store the whole file contents. Heed though, dynamic memory allocations should be freed by the free part and open files airtight with fclose.

              #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h>  int main(void) {     const char* filename = "input.txt";      FILE* input_file = fopen(filename, "r");     if (!input_file)         exit(EXIT_FAILURE);      struct stat sb;     if (stat(filename, &sb) == -ane) {         perror("stat");         go out(EXIT_FAILURE);     }      char* file_contents = malloc(sb.st_size);     fread(file_contents, sb.st_size, 1, input_file);      printf("%s\n", file_contents);      fclose(input_file);     gratis(file_contents);      exit(EXIT_SUCCESS); }                          

Utilise fopen and getline Functions to Read Text File in C

Alternatively, we can skip retrieving the file size with the stat function and instead iterate over each line of the file until the end is reached using the getline function. getline reads the string input from the given file stream. Equally the name suggests, the function retrieves all bytes until the newline character is found.

getline takes three parameters, the first of which stores the read bytes. It tin be alleged as char* and set to Nada. Meanwhile, if the 2nd argument is the address of integer 0, the getline automatically allocates dynamic retentivity for the buffer, which should exist freed by the user, hence the call to the free function at the end of the plan.

Note that these parameters can have different values if needed, all of which are explained in this function manual. The third parameter is the FILE* pointer of the open file stream.

              #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <fcntl.h>  int main(void) {     const char* filename = "input.txt";      FILE* input_file = fopen(filename, "r");     if (!input_file)         exit(EXIT_FAILURE);      char *contents = NULL;     size_t len = 0;     while (getline(&contents, &len, input_file) != -1){         printf("%s", contents);     }      fclose(input_file);     free(contents);      exit(EXIT_SUCCESS); }                          

Write for us

DelftStack manufactures are written past software geeks like you. If you lot too would like to contribute to DelftStack by writing paid articles, you can bank check the write for us page.

Related Article - C File

  • Check if a File Exists in C
  • Become Extended Attributes of File in C
  • Ezoic

    fitzpegrew58.blogspot.com

    Source: https://www.delftstack.com/howto/c/read-file-c/

    0 Response to "How to Read From a Txt File C++"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel