When Reading Data in a Data File, the Data Will Be Read _______.

C File direction

A File can be used to store a big volume of persistent data. Similar many other languages 'C' provides following file direction functions,

  1. Creation of a file
  2. Opening a file
  3. Reading a file
  4. Writing to a file
  5. Closing a file

Following are the almost of import file management functions available in 'C,'

role purpose
fopen () Creating a file or opening an existing file
fclose () Closing a file
fprintf () Writing a block of data to a file
fscanf () Reading a block information from a file
getc () Reads a single character from a file
putc () Writes a single character to a file
getw () Reads an integer from a file
putw () Writing an integer to a file
fseek () Sets the position of a file pointer to a specified location
ftell () Returns the electric current position of a file pointer
rewind () Sets the file arrow at the offset of a file

In this tutorial, y'all volition learn-

  • How to Create a File
  • How to Close a file:
  • Writing to a File
  • fputc() Function:
  • fputs () Function:
  • fprintf()Part:
  • Reading data from a File
  • Interactive File Read and Write with getc and putc

How to Create a File

Whenever y'all want to work with a file, the kickoff stride is to create a file. A file is nothing merely space in a memory where data is stored.

To create a file in a 'C' program following syntax is used,

FILE *fp; fp = fopen ("file_name", "mode");          

In the above syntax, the file is a information construction which is defined in the standard library.

fopen is a standard part which is used to open a file.

  • If the file is not nowadays on the system, then it is created and so opened.
  • If a file is already present on the system, then it is straight opened using this function.

fp is a file pointer which points to the type file.

Whenever you open up or create a file, you have to specify what you are going to practise with the file. A file in 'C' programming tin be created or opened for reading/writing purposes. A manner is used to specify whether you desire to open a file for whatsoever of the beneath-given purposes. Following are the dissimilar types of modes in 'C' programming which can be used while working with a file.

File Mode Clarification
r Open a file for reading. If a file is in reading manner, so no data is deleted if a file is already present on a system.
westward Open up a file for writing. If a file is in writing fashion, and so a new file is created if a file doesn't exist at all. If a file is already present on a arrangement, so all the data within the file is truncated, and it is opened for writing purposes.
a Open up a file in
append way. If a file is in append manner, then the file is opened. The content within the file doesn't alter.
r+ open up for reading and writing from starting time
w+ open up for reading and writing, overwriting a file
a+ open up for reading and writing, appending to file

In the given syntax, the filename and the way are specified as strings hence they must always be enclosed within double quotes.

Example:

#include <stdio.h> int primary() { FILE *fp; fp  = fopen ("information.txt", "w"); }          

Output:

File is created in the same folder where y'all have saved your code.

You tin specify the path where you want to create your file

#include <stdio.h> int principal() { FILE *fp; fp  = fopen ("D://information.txt", "due west"); }

How to Close a file

Ane should always close a file whenever the operations on file are over. Information technology ways the contents and links to the file are terminated. This prevents accidental damage to the file.

'C' provides the fclose part to perform file closing operation. The syntax of fclose is as follows,

fclose (file_pointer);          

Example:

FILE *fp; fp  = fopen ("data.txt", "r"); fclose (fp);          

The fclose part takes a file arrow as an argument. The file associated with the file pointer is so airtight with the help of fclose function. Information technology returns 0 if shut was successful and EOF (stop of file) if there is an error has occurred while file endmost.

After closing the file, the same file pointer can also be used with other files.

In 'C' programming, files are automatically close when the plan is terminated. Endmost a file manually by writing fclose part is a proficient programming practice.

Writing to a File

In C, when you write to a file, newline characters '\northward' must exist explicitly added.

The stdio library offers the necessary functions to write to a file:

  • fputc(char, file_pointer): It writes a character to the file pointed to by file_pointer.
  • fputs(str, file_pointer): Information technology writes a string to the file pointed to by file_pointer.
  • fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to past file_pointer. The string can optionally include format specifiers and a list of variables variable_lists.

The program below shows how to perform writing to a file:

fputc() Role:

#include <stdio.h> int main() {         int i;         FILE * fptr;         char fn[50];         char str[] = "Guru99 Rocks\n";         fptr = fopen("fputc_test.txt", "westward"); // "w" defines "writing fashion"         for (i = 0; str[i] != '\northward'; i++) {             /* write to file using fputc() function */             fputc(str[i], fptr);         }         fclose(fptr);         return 0;     }

Output:

The above program writes a single character into the fputc_test.txt file until it reaches the side by side line symbol "\due north" which indicates that the judgement was successfully written. The process is to accept each character of the array and write information technology into the file.

  1. In the above program, we take created and opened a file called fputc_test.txt in a write way and declare our string which will exist written into the file.
  2. We do a character by character write functioning using for loop and put each character in our file until the "\n" character is encountered then the file is airtight using the fclose function.

fputs () Function:

#include <stdio.h> int primary() {         FILE * fp;         fp = fopen("fputs_test.txt", "west+");         fputs("This is Guru99 Tutorial on fputs,", fp);         fputs("We don't need to use for loop\northward", fp);         fputs("Easier than fputc function\n", fp);         fclose(fp);         render (0);     }

OUTPUT:

  1. In the above plan, we take created and opened a file called fputs_test.txt in a write manner.
  2. After we do a write functioning using fputs() function past writing three different strings
  3. Then the file is airtight using the fclose function.

fprintf()Part:

#include <stdio.h>     int main() {         FILE *fptr;         fptr = fopen("fprintf_test.txt", "westward"); // "west" defines "writing manner"         /* write to file */         fprintf(fptr, "Learning C with Guru99\north");         fclose(fptr);         return 0;     }

OUTPUT:

  1. In the above programme nosotros accept created and opened a file chosen fprintf_test.txt in a write fashion.
  2. After a write operation is performed using fprintf() function by writing a string, then the file is closed using the fclose role.

Reading data from a File

In that location are iii different functions dedicated to reading data from a file

  • fgetc(file_pointer): It returns the side by side grapheme from the file pointed to by the file pointer. When the stop of the file has been reached, the EOF is sent back.
  • fgets(buffer, due north, file_pointer): Information technology reads northward-i characters from the file and stores the string in a buffer in which the Goose egg character '\0' is appended equally the last character.
  • fscanf(file_pointer, conversion_specifiers, variable_adresses): It is used to parse and analyze data. It reads characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion specifiers. Keep in mind that every bit with scanf, fscanf stops reading a cord when space or newline is encountered.

The following plan demonstrates reading from fputs_test.txt file using fgets(),fscanf() and fgetc () functions respectively :

#include <stdio.h> int main() {         FILE * file_pointer;         char buffer[30], c;          file_pointer = fopen("fprintf_test.txt", "r");         printf("----read a line----\due north");         fgets(buffer, 50, file_pointer);         printf("%southward\n", buffer);          printf("----read and parse data----\n");         file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer         char str1[10], str2[2], str3[xx], str4[2];         fscanf(file_pointer, "%s %s %s %southward", str1, str2, str3, str4);         printf("Read String1 |%south|\due north", str1);         printf("Read String2 |%s|\north", str2);         printf("Read String3 |%s|\n", str3);         printf("Read String4 |%south|\northward", str4);          printf("----read the entire file----\northward");          file_pointer = fopen("fprintf_test.txt", "r"); //reset the arrow         while ((c = getc(file_pointer)) != EOF) printf("%c", c);          fclose(file_pointer);         render 0;     }

Result:

----read a line---- Learning C with Guru99  ----read and parse data---- Read String1 |Learning| Read String2 |C| Read String3 |with| Read String4 |Guru99| ----read the unabridged file---- Learning C with Guru99

  1. In the higher up plan, we have opened the file called "fprintf_test.txt" which was previously written using fprintf() office, and information technology contains "Learning C with Guru99" string. We read it using the fgets() office which reads line by line where the buffer size must be enough to handle the entire line.
  2. We reopen the file to reset the pointer file to point at the outset of the file. Create various strings variables to handle each word separately. Impress the variables to see their contents. The fscanf() is mainly used to extract and parse data from a file.
  3. Reopen the file to reset the pointer file to point at the starting time of the file. Read information and print it from the file character past character using getc() function until the EOF statement is encountered
  4. Later performing a reading operation file using different variants, nosotros once more airtight the file using the fclose office.

Interactive File Read and Write with getc and putc

These are the simplest file operations. Getc stands for get graphic symbol, and putc stands for put character. These 2 functions are used to handle only a unmarried graphic symbol at a time.

Following plan demonstrates the file treatment functions in 'C' programming:

#include <stdio.h> int main() {         FILE * fp;         char c;         printf("File Handling\n");         //open a file         fp = fopen("demo.txt", "due west");         //writing operation         while ((c = getchar()) != EOF) {             putc(c, fp);         }         //close file         fclose(fp);         printf("Data Entered:\n");         //reading         fp = fopen("demo.txt", "r");         while ((c = getc(fp)) != EOF) {             printf("%c", c);         }         fclose(fp);         render 0;     }          

Output:

  1. In the above program we accept created and opened a file called demo in a write way.
  2. After a write performance is performed, then the file is airtight using the fclose function.
  3. We have over again opened a file which now contains data in a reading mode. A while loop will execute until the eof is found. Once the end of file is institute the operation volition be terminated and information will be displayed using printf function.
  4. Afterward performing a reading operation file is again closed using the fclose part.

Summary

  • A file is a space in a memory where data is stored.
  • 'C' programming provides various functions to deal with a file.
  • A mechanism of manipulating with the files is called as file direction.
  • A file must exist opened before performing operations on it.
  • A file tin be opened in a read, write or an suspend mode.
  • Getc and putc functions are used to read and write a single graphic symbol.
  • The function fscanf() permits to read and parse data from a file
  • We tin can read (using the getc function) an entire file by looping to embrace all the file until the EOF is encountered
  • We tin can write to a file afterwards creating its name, by using the office fprintf() and it must have the newline character at the end of the cord text.

hendersonwhoser.blogspot.com

Source: https://www.guru99.com/c-file-input-output.html

0 Response to "When Reading Data in a Data File, the Data Will Be Read _______."

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel