C++ Reading From File Ending in With Space

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

  1. fgetc() This function is used to read a single graphic symbol from the file.
  2. fgets() This office 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 part is used to read formatted input from a file.

Steps To Read A File:

  • Open up a file using the part fopen() and store the reference of the file in a FILE pointer.
  • Read contents of the file using whatsoever of these functions fgetc(), fgets(), fscanf(), or fread().
  • File shut the file using the function fclose().

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

fgetc()

fgetc() reads characters pointed past the function pointer at that time. On each successful read, information technology returns the grapheme (ASCII value) read from the stream and advances the read position to the side by side grapheme. This role returns a abiding EOF (-1) when at that place is no content to read or an unsuccessful read.

Syntax:

int fgetc(FILE *ptr);

Approach:

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

Using EOF:
Beneath is the C program to implement the above arroyo-

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int primary()

{

FILE * ptr;

char ch;

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

if (Aught == ptr) {

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

}

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

do {

ch = fgetc (ptr);

printf ( "%c" , ch);

} while (ch != EOF);

fclose (ptr);

return 0;

}

Input File:

GeeksforGeeks | A informatics portal for geeks

Output:

output fgetc

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

Using feof():
feof() function takes file pointer every bit argument and returns true if pointer reaches the end of the file.

Syntax:

int feof(FILE *ptr);

Approach:

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

Below is the C programme to implement the above arroyo:

C

#include <stdio.h>

#include <stdlib.h>

#include <cord.h>

int main()

{

FILE * ptr;

char ch;

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

if (Zero == ptr) {

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

}

printf ( "content of this file are \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 1 string at a fourth dimension from the file. fgets() returns a string if it is successfully read by part or returns NULL if tin can not read.

Syntax:

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

Here,
str: It is cord in which fgets() store string after reading information technology from file.
size: It is maximum characters to read from stream.
ptr: It is file arrow.

Approach:

  • In this approach, the contents of the file are read 1 character at a time until we attain the end of the file.
  • When we attain the end of the file fgets() tin't read and returns Goose egg and the program volition stop reading.

Below is the C plan to implement the above approach:

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int chief()

{

FILE * ptr;

char str[50];

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

if (Null == ptr) {

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

}

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

while ( fgets (str, 50, ptr) != Nix) {

printf ( "%south" , str);

}

fclose (ptr);

render 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 data from the files and stores information technology in variables.
  • The data in the buffer is printed on the console till the end of the file is reached.

C++

#include <stdio.h>

int primary()

{

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

if (ptr == NULL) {

printf ( "no such file." );

return 0;

}

char buf[100];

while ( fscanf (ptr, "%*southward %*due south %s " ,

buf)

== 1)

printf ( "%s\due north" , buf);

return 0;

}

Output:

fread()

fread() makes it easier to read blocks of data from a file. For example, in the case of reading a structure from the file, it becomes an easy job 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 first, reads the count number of objects, each one with a size of size bytes from the given input stream.
  • The total corporeality 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, and then the behavior is undefined and if the value of size or count is equal to zilch, then this plan will but return 0.

C++

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Grade {

char cname[xxx];

char sdate[thirty];

};

int main()

{

FILE * of;

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

if (of == Null) {

fprintf (stderr,

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

exit (1);

}

struct Course inp1 = { "Algorithms" ,

"30OCT" };

struct Grade inp2 = { "DataStructures" ,

"28SEPT" };

struct Course inp3 = { "Programming" ,

"1NOV" };

fwrite (&inp1, sizeof ( struct Course),

one, of);

fwrite (&inp2, sizeof ( struct Course),

1, of);

fwrite (&inp3, sizeof ( struct Course),

one, 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 ( "examination.txt" , "r" );

if (inf == NULL) {

fprintf (stderr,

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

go out (ane);

}

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

one, inf))

printf ( "Form Name = %s Started = %southward\n" ,

inp.cname, inp.sdate);

fclose (inf);

}

Output:

output fread


sherlockfainterep.blogspot.com

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

0 Response to "C++ Reading From File Ending in With Space"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel