C++ Read in Name and Numbers From File

C programming linguistic communication supports iv pre-defined functions to read contents from a file, divers 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 up a file using the function fopen() and shop the reference of the file in a FILE pointer.
  • Read contents of the file using any of these functions fgetc(), fgets(), fscanf(), or fread().
  • File shut the file using the office fclose().

Allow's begin discussing each of these functions in detail.

fgetc()

fgetc() reads characters pointed by the office pointer at that time. On each successful read, it returns the grapheme (ASCII value) read from the stream and advances the read position to the next graphic symbol. This part returns a constant EOF (-i) when there is no content to read or an unsuccessful read.

Syntax:

int fgetc(FILE *ptr);

Approach:

  • This programme reads the whole content of the file, using this function by reading characters one by one.
  • Do-While loop will exist used which will read character until it reaches and of file.
  • When information technology reaches terminate it returns  EOF graphic symbol (-1).

Using EOF:
Below is the C program to implement the higher up arroyo-

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int master()

{

FILE * ptr;

char ch;

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

if (Goose egg == ptr) {

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

}

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

exercise {

ch = fgetc (ptr);

printf ( "%c" , ch);

} while (ch != EOF);

fclose (ptr);

return 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 information technology and if it is and so terminate reading.

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

Syntax:

int feof(FILE *ptr);

Approach:

  • In this approach, a character is read using fgetc().
  • Using feof() role check for end of file. since feof() returns true afterward it reaches the end.
  • Use logical NOT operator(!) so that when it reaches cease condition become false and loop stop.

Beneath is the C program to implement the above approach:

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int master()

{

FILE * ptr;

char ch;

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

if (Naught == ptr) {

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

}

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

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 past function or returns Zilch if can not read.

Syntax:

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

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

Arroyo:

  • In this approach, the contents of the file are read 1 character at a time until we reach the end of the file.
  • When nosotros attain the end of the file fgets() can't read and returns Null and the plan will finish reading.

Below is the C program to implement the above approach:

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int master()

{

FILE * ptr;

char str[l];

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

if (NULL == ptr) {

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

}

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

while ( fgets (str, l, ptr) != Nothing) {

printf ( "%s" , str);

}

fclose (ptr);

return 0;

}

Input File:

GeeksforGeeks | A informatics portal for geeks

Output:

Output fgets

fscanf()

fscanf() reads formatted input from a stream.

Syntax:

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

Approach:

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

C++

#include <stdio.h>

int master()

{

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);

render 0;

}

Output:

fread()

fread() makes information technology easier to read blocks of data from a file. For instance, in the case of reading a structure from the file, information technology becomes an easy task to read using fread.

Syntax:

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

ptr: This is the pointer to a block of memory with a minimum size of size*nmemb bytes.
size: This is the size in bytes of each chemical 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.

Approach:

  • It first, reads the count number of objects, each one with a size of size bytes from the given input stream.
  • The full 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 non trivially copy-able, so the behavior is undefined and if the value of size or count is equal to nil, then this program will only return 0.

C++

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Form {

char cname[30];

char sdate[xxx];

};

int main()

{

FILE * of;

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

if (of == NULL) {

fprintf (stderr,

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

exit (1);

}

struct Course inp1 = { "Algorithms" ,

"30OCT" };

struct Grade inp2 = { "DataStructures" ,

"28SEPT" };

struct Course inp3 = { "Programming" ,

"1NOV" };

fwrite (&inp1, sizeof ( struct Form),

one, of);

fwrite (&inp2, sizeof ( struct Class),

i, of);

fwrite (&inp3, sizeof ( struct Form),

i, of);

if ( fwrite != 0)

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

else

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

fclose (of);

FILE * inf;

struct Course inp;

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

if (inf == NULL) {

fprintf (stderr,

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

exit (1);

}

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

1, inf))

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

inp.cname, inp.sdate);

fclose (inf);

}

Output:

output fread


joneshingive.blogspot.com

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

0 Response to "C++ Read in Name and Numbers From File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel