Reading in Data From a File C++ Program With Multiple Things

C programming language supports four pre-defined functions to read contents from a file, defined in stdio.h header file:

  1. fgetc() This role is used to read a single character from the file.
  2. fgets() This function is used to read strings from files.
  3. fscanf() This function is used to read the block of raw bytes from files. This is used to read binary files.
  4. fread() This function is used to read formatted input from a file.

Steps To Read A File:

  • Open a file using the function fopen() and store the reference of the file in a FILE arrow.
  • Read contents of the file using any of these functions fgetc(), fgets(), fscanf(), or fread().
  • File close the file using the function fclose().

Let's begin discussing each of these functions in particular.

fgetc()

fgetc() reads characters pointed by the role arrow at that time. On each successful read, it returns the character (ASCII value) read from the stream and advances the read position to the adjacent graphic symbol. This role returns a constant EOF (-1) when at that place is no content to read or an unsuccessful read.

Syntax:

int fgetc(FILE *ptr);

Approach:

  • This plan reads the whole content of the file, using this office past reading characters one by one.
  • Do-While loop will be used which will read character until it reaches and of file.
  • When it reaches finish it returns  EOF grapheme (-i).

Using EOF:
Below is the C plan to implement the above arroyo-

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int primary()

{

FILE * ptr;

char ch;

ptr = fopen ( "test.txt" , "r" );

if (Nil == ptr) {

printf ( "file can't be opened \n" );

}

printf ( "content of this file are \north" );

practise {

ch = fgetc (ptr);

printf ( "%c" , ch);

} while (ch != EOF);

fclose (ptr);

render 0;

}

Input File:

GeeksforGeeks | A information science portal for geeks

Output:

output fgetc

In the above code, the approach is to read one character from the file and check if it is not EOF, if it is not then print it and if it is then stop reading.

Using feof():
feof() office takes file arrow as argument and returns true if pointer reaches the finish of the file.

Syntax:

int feof(FILE *ptr);

Approach:

  • In this approach, a graphic symbol is read using fgetc().
  • Using feof() function bank check for stop of file. since feof() returns true after it reaches the end.
  • Apply logical NOT operator(!) so that when it reaches end condition get faux and loop cease.

Below is the C program to implement the above approach:

C

#include <stdio.h>

#include <stdlib.h>

#include <cord.h>

int main()

{

FILE * ptr;

char ch;

ptr = fopen ( "test.txt" , "r" );

if (Aught == ptr) {

printf ( "file tin't be opened \n" );

}

printf ( "content of this file are \n" );

while (! feof (ptr)) {

ch = fgetc (ptr);

printf ( "%c" , ch);

}

fclose (ptr);

return 0;

}

Input File:

GeeksforGeeks | A computer science portal for geeks

Output:

output feof

fgets()

fgets() reads one cord at a time from the file. fgets() returns a string if it is successfully read by function or returns NULL if can non read.

Syntax:

char * fgets(char *str, int size, FILE * ptr);

Here,
str: It is string in which fgets() store string later on reading it from file.
size: Information technology is maximum characters to read from stream.
ptr: Information technology is file pointer.

Approach:

  • In this approach, the contents of the file are read one grapheme at a time until we reach the end of the file.
  • When we achieve the stop of the file fgets() can't read and returns NULL and the program volition finish reading.

Below is the C program to implement the above approach:

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main()

{

FILE * ptr;

char str[50];

ptr = fopen ( "test.txt" , "a+" );

if (NULL == ptr) {

printf ( "file can't be opened \due north" );

}

printf ( "content of this file are \north" );

while ( fgets (str, fifty, ptr) != NULL) {

printf ( "%south" , str);

}

fclose (ptr);

return 0;

}

Input File:

GeeksforGeeks | A computer scientific discipline portal for geeks

Output:

Output fgets

fscanf()

fscanf() reads formatted input from a stream.

Syntax:

int fscanf(FILE *ptr, const char *format, …)

Arroyo:

  • fscanf reads formatted information from the files and stores it in variables.
  • The data in the buffer is printed on the panel till the end of the file is reached.

C++

#include <stdio.h>

int main()

{

FILE * ptr = fopen ( "abc.txt" , "r" );

if (ptr == NULL) {

printf ( "no such file." );

render 0;

}

char buf[100];

while ( fscanf (ptr, "%*s %*s %s " ,

buf)

== 1)

printf ( "%s\n" , buf);

return 0;

}

Output:

fread()

fread() makes it easier to read blocks of data from a file. For instance, in the instance of reading a structure from the file, it becomes an like shooting fish in a barrel chore to read using fread.

Syntax:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)

ptr: This is the arrow to a block of memory with a minimum size of size*nmemb bytes.
size: This is the size in bytes of each element to be read.
nmemb: This is the number of elements, each one with a size of size bytes.
stream: This is the pointer to a FILE object that specifies an input stream.

Arroyo:

  • It kickoff, reads the count number of objects, each one with a size of size bytes from the given input stream.
  • The total amount of bytes reads if successful is (size*count).
  • According to the no. of characters read, the indicator file position is incremented.
  • If the objects read are not trivially copy-able, then the behavior is undefined and if the value of size or count is equal to null, then this program will only return 0.

C++

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Form {

char cname[30];

char sdate[thirty];

};

int main()

{

FILE * of;

of = fopen ( "test.txt" , "west" );

if (of == Zippo) {

fprintf (stderr,

"\nError to open up the file\n" );

exit (1);

}

struct Form inp1 = { "Algorithms" ,

"30OCT" };

struct Course inp2 = { "DataStructures" ,

"28SEPT" };

struct Form inp3 = { "Programming" ,

"1NOV" };

fwrite (&inp1, sizeof ( struct Grade),

1, of);

fwrite (&inp2, sizeof ( struct Course),

1, of);

fwrite (&inp3, sizeof ( struct Grade),

1, of);

if ( fwrite != 0)

printf ( "Contents to file written successfully !\n" );

else

printf ( "Mistake writing file !\n" );

fclose (of);

FILE * inf;

struct Grade inp;

inf = fopen ( "exam.txt" , "r" );

if (inf == Nil) {

fprintf (stderr,

"\nError to open up the file\n" );

get out (1);

}

while ( fread (&inp, sizeof ( struct Course),

one, inf))

printf ( "Course Name = %s Started = %s\northward" ,

inp.cname, inp.sdate);

fclose (inf);

}

Output:

output fread


quirionunfinamess.blogspot.com

Source: https://www.geeksforgeeks.org/c-program-to-read-contents-of-whole-file/

0 Response to "Reading in Data From a File C++ Program With Multiple Things"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel