C Programming Using Fgets for Reading From a File
The fgets part reads characters from the specified stream and shop into the character assortment. Information technology reads only n-1 graphic symbol, where northward is the specified number of characters.
It stops the reading when read newline character or (n-1) graphic symbol, or come across end-of-file. The good matter is that it writes cypher character just after written the terminal character into the array.
In this article, I volition explain to you, how to read the character array or string from the given file using fgets in C programming with case.
Note: Yous accept to include the stdio.h ( header file) earlier using fgets in C Programming.
Syntax of fgets:
char *fgets(char * restrict s, int n,FILE * restrict stream);
Where,
s: Pointer to a grapheme array with a minimum size of n bytes.
n: Maximum number of characters to be copied into s (including the terminating null character).
stream: Input stream (file arrow of an opened file).
Note: stdin can be used as an argument to read from the standard input.
Return value of fgets():
On success, the fgets role returns the string (same s parameter). On error render null pointer.
Note: If terminate-of-file is encountered and no characters take been read into the "s" (graphic symbol assortment), the contents of the "southward" remain unchanged and a null arrow is returned.
Instance lawmaking to explain the working of fgets in C,
In this example, I am reading a file "aticleworld.txt" using the c fgets which contains a string "I am using fgets.".
#include <stdio.h> //Maximum size of the assortment #define MAX_SIZE 32 int main() { //file pointer FILE *fp = NULL; char readFileData[MAX_SIZE] = {0}; //open the file fp = fopen("aticleworld.txt", "r"); if(fp == Nada) { printf("Fault in creating the file\north"); get out(1); } //Read file using fgets if(fgets(readFileData,MAX_SIZE,fp) == Goose egg) { printf("Error in reading the file\n"); //shut the file fclose(fp); exit(1); } //Brandish read data puts(readFileData); //shut the file fclose(fp); printf("Read file successfully\n"); return 0; }
Output:
Code Assay:
In the in a higher place c fgets instance, showtime, we opened the already created text ("aticleworld.txt") file in reading mode and become the file pointer. Using the if condition I am verifying that file is opened successfully or not.
//open the file fp = fopen("aticleworld.txt", "r"); if(fp == Zero) { printf("Mistake in creating the file\due north"); exit(1); }
After opening the file successfully, I take used c fgets to read the grapheme array from the file ("aticleworld.txt"). I accept also used the if condition to check whether fgets reads a character array without mistake or non.
//Read file using fgets if(fgets(readFileData,MAX_SIZE,fp) == NULL) { printf("Fault in reading the file\due north"); //close the file fclose(fp); exit(ane); }
In the last, I have used puts to display reads character assortment on console and fclose to close the opened file.
//Display read data puts(readFileData); //close the file fclose(fp); printf("Read file successfully\n");
You can check this Article.
- How to use fopen in C.
- How to apply if in C programming.
- When to use while loop in C.
Difference between fgets and gets in C:
There is the following departure between the fputs and puts in C.
1. fgets function takes three arguments first is the pointer to character array second maximum number of characters to be read (including terminating naught grapheme) and third is an input stream (file pointer).
char *fgets(char * restrict s, int n,FILE * restrict stream);
In another manus, gets takes only one argument pointer to a character array.
char * gets ( char * s);
2. fgets part can read from any specified file stream while gets but read from stdin.
three. Use of fgets is safe in comparing to gets function considering there is no purlieus checking in gets. And then might be buffer overflow occurs.
Notation: gets() is depreciated in C99 and no longer function of C11 and introduces gets_s.
Consider the example:
In the below example, I accept created a character array of size x. Using the gets function I am reading the stdin and store all reads characters in the graphic symbol assortment. If I volition give the input more and so 10 characters then the gets part reads all characters and store it in the character array, it is incorrect because can crusade a buffer overflow.
#include <stdio.h> #define ARRAY_SIZE 10 int main() { char buf[ARRAY_SIZE]; printf("Enter a cord: "); gets(buf); printf("cord is: %southward\n",buf); return 0; }
Output:
Also that, if you will use c fgets. it only reads ix character and copies into the character array. Then information technology avoids the adventure of buffer overflow.
#include <stdio.h> #ascertain ARRAY_SIZE 10 int main() { char buf[ARRAY_SIZE]; printf("Enter a cord: "); fgets(buf,ARRAY_SIZE,stdin); printf("string is: %s\n",buf); return 0; }
Output:
How to remove abaft newline character from fgets input?
Below I am writing a C programme to remove the abaft newline graphic symbol from the fgets input
#include <stdio.h> #include <string.h> #define BUFFER_SIZE 24 int main(void) { char buf[BUFFER_SIZE]; printf("Enter the data = "); if (fgets(buf, sizeof(buf), stdin) == Zip) { printf("Fail to read the input stream"); } else { char *ptr = strchr(buf, '\due north'); if (ptr) { *ptr = '\0'; } } printf("Entered Data = %s\northward",buf); render 0; }
Explanation: In the above C plan strchr() (string part) replace the newline grapheme in the string with '\0' if it exists.
You lot can see Commodity for more detail, How to remove trailing newline character from fgets input?
Recommended Articles for you:
- How to use fgetc() in C?
- Pause Statements in C.
- Continue argument in C.
- File Handling in C, In Just A Few Hours!
- How to employ fputs() in C?
- Format specifiers in C.
- How to create an employee record system.
- A brief description of the arrow in C.
- Dangling, Void, Cipher and Wild Pointers.
- How to use fread() in C?
- How to use fwrite() in C?
- Function pointer in c, a detailed guide
- How to utilise the structure of function pointer in c linguistic communication?
- Function pointer in structure.
- How to apply fopen() in C?
- How to create a library management system project in C
Source: https://aticleworld.com/fgets-in-c/
0 Response to "C Programming Using Fgets for Reading From a File"
Post a Comment