Linux if a File Can Read in C
C File management
A File can be used to store a big volume of persistent data. Like many other languages 'C' provides following file management functions,
- Creation of a file
- Opening a file
- Reading a file
- Writing to a file
- Closing a file
Following are the about of import file management functions available in 'C,'
office | purpose | |
---|---|---|
fopen () | Creating a file or opening an existing file | |
fclose () | Endmost a file | |
fprintf () | Writing a block of data to a file | |
fscanf () | Reading a cake data from a file | |
getc () | Reads a single grapheme 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 current position of a file arrow | |
rewind () | Sets the file pointer at the offset of a file |
In this tutorial, you will learn-
- How to Create a File
- How to Close a file:
- Writing to a File
- fputc() Function:
- fputs () Role:
- fprintf()Function:
- Reading data from a File
- Interactive File Read and Write with getc and putc
How to Create a File
Whenever you desire to work with a file, the first step is to create a file. A file is null but 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", "style");
In the higher up syntax, the file is a data construction which is defined in the standard library.
fopen is a standard role which is used to open a file.
- If the file is not present on the system, then it is created and and then opened.
- If a file is already present on the system, then it is direct opened using this function.
fp is a file pointer which points to the blazon file.
Whenever you open or create a file, you have to specify what you are going to exercise with the file. A file in 'C' programming tin can be created or opened for reading/writing purposes. A fashion is used to specify whether you desire to open up a file for whatsoever of the beneath-given purposes. Following are the different types of modes in 'C' programming which can exist used while working with a file.
File Mode | Description |
---|---|
r | Open a file for reading. If a file is in reading mode, so no data is deleted if a file is already nowadays on a arrangement. |
westward | Open a file for writing. If a file is in writing style, then a new file is created if a file doesn't be at all. If a file is already nowadays on a system, then all the data inside the file is truncated, and it is opened for writing purposes. |
a | Open a file in append manner. If a file is in append mode, and then the file is opened. The content inside the file doesn't alter. |
r+ | open for reading and writing from beginning |
w+ | open for reading and writing, overwriting a file |
a+ | open for reading and writing, appending to file |
In the given syntax, the filename and the mode are specified as strings hence they must ever be enclosed within double quotes.
Instance:
#include <stdio.h> int master() { FILE *fp; fp = fopen ("data.txt", "w"); }
Output:
File is created in the same binder where you have saved your lawmaking.
You can specify the path where you want to create your file
#include <stdio.h> int main() { FILE *fp; fp = fopen ("D://data.txt", "westward"); }
How to Close a file
One should always shut a file whenever the operations on file are over. It means the contents and links to the file are terminated. This prevents accidental damage to the file.
'C' provides the fclose function to perform file closing operation. The syntax of fclose is as follows,
fclose (file_pointer);
Example:
FILE *fp; fp = fopen ("information.txt", "r"); fclose (fp);
The fclose function takes a file pointer as an argument. The file associated with the file pointer is then airtight with the assist of fclose function. It returns 0 if close was successful and EOF (end of file) if there is an error has occurred while file closing.
After endmost the file, the same file pointer tin can also be used with other files.
In 'C' programming, files are automatically close when the program is terminated. Closing a file manually by writing fclose function is a expert programming practice.
Writing to a File
In C, when y'all write to a file, newline characters '\n' 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): It writes a string to the file pointed to past file_pointer.
- fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to past file_pointer. The string tin optionally include format specifiers and a list of variables variable_lists.
The program beneath shows how to perform writing to a file:
fputc() Part:
#include <stdio.h> int principal() { int i; FILE * fptr; char fn[50]; char str[] = "Guru99 Rocks\n"; fptr = fopen("fputc_test.txt", "west"); // "due west" defines "writing way" for (i = 0; str[i] != '\n'; i++) { /* write to file using fputc() part */ fputc(str[i], fptr); } fclose(fptr); render 0; }
Output:
The above programme writes a unmarried character into the fputc_test.txt file until it reaches the side by side line symbol "\n" which indicates that the sentence was successfully written. The procedure is to accept each character of the assortment and write it into the file.
- In the above programme, we accept created and opened a file called fputc_test.txt in a write mode and declare our string which will be written into the file.
- Nosotros do a character by graphic symbol write operation using for loop and put each character in our file until the "\n" graphic symbol is encountered then the file is airtight using the fclose role.
fputs () Function:
#include <stdio.h> int main() { FILE * fp; fp = fopen("fputs_test.txt", "w+"); fputs("This is Guru99 Tutorial on fputs,", fp); fputs("Nosotros don't demand to use for loop\n", fp); fputs("Easier than fputc function\n", fp); fclose(fp); return (0); }
OUTPUT:
- In the above plan, we accept created and opened a file called fputs_test.txt in a write manner.
- Afterwards we practice a write functioning using fputs() role by writing 3 different strings
- So the file is closed using the fclose part.
fprintf()Function:
#include <stdio.h> int chief() { FILE *fptr; fptr = fopen("fprintf_test.txt", "w"); // "due west" defines "writing mode" /* write to file */ fprintf(fptr, "Learning C with Guru99\n"); fclose(fptr); render 0; }
OUTPUT:
- In the above plan we have created and opened a file chosen fprintf_test.txt in a write mode.
- After a write functioning is performed using fprintf() function by writing a string, then the file is closed using the fclose office.
Reading data from a File
There are three different functions dedicated to reading data from a file
- fgetc(file_pointer): It returns the next graphic symbol from the file pointed to by the file pointer. When the terminate of the file has been reached, the EOF is sent back.
- fgets(buffer, n, file_pointer): It reads n-1 characters from the file and stores the cord in a buffer in which the NULL grapheme '\0' is appended as the last character.
- fscanf(file_pointer, conversion_specifiers, variable_adresses): It is used to parse and analyze information. It reads characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion specifiers. Proceed in mind that as with scanf, fscanf stops reading a string when space or newline is encountered.
The following program demonstrates reading from fputs_test.txt file using fgets(),fscanf() and fgetc () functions respectively :
#include <stdio.h> int chief() { FILE * file_pointer; char buffer[thirty], c; file_pointer = fopen("fprintf_test.txt", "r"); printf("----read a line----\n"); fgets(buffer, fifty, file_pointer); printf("%s\n", buffer); printf("----read and parse data----\northward"); file_pointer = fopen("fprintf_test.txt", "r"); //reset the arrow char str1[10], str2[ii], str3[20], str4[2]; fscanf(file_pointer, "%s %s %s %s", str1, str2, str3, str4); printf("Read String1 |%s|\north", str1); printf("Read String2 |%s|\n", str2); printf("Read String3 |%southward|\n", str3); printf("Read String4 |%southward|\north", str4); printf("----read the entire file----\n"); file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer while ((c = getc(file_pointer)) != EOF) printf("%c", c); fclose(file_pointer); return 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 entire file---- Learning C with Guru99
- In the above program, we take opened the file called "fprintf_test.txt" which was previously written using fprintf() function, and it contains "Learning C with Guru99" string. We read information technology using the fgets() function which reads line by line where the buffer size must be enough to handle the entire line.
- We reopen the file to reset the arrow file to signal at the outset of the file. Create various strings variables to handle each word separately. Print the variables to encounter their contents. The fscanf() is mainly used to extract and parse data from a file.
- Reopen the file to reset the arrow file to point at the outset of the file. Read information and impress it from the file grapheme by graphic symbol using getc() function until the EOF statement is encountered
- Subsequently performing a reading operation file using different variants, we over again closed the file using the fclose part.
Interactive File Read and Write with getc and putc
These are the simplest file operations. Getc stands for get character, and putc stands for put character. These two functions are used to handle just a single grapheme at a time.
Following programme demonstrates the file handling functions in 'C' programming:
#include <stdio.h> int main() { FILE * fp; char c; printf("File Handling\due north"); //open a file fp = fopen("demo.txt", "w"); //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); return 0; }
Output:
- In the to a higher place program we accept created and opened a file called demo in a write mode.
- After a write functioning is performed, then the file is closed using the fclose function.
- We take again opened a file which now contains data in a reading mode. A while loop will execute until the eof is plant. In one case the finish of file is found the operation will be terminated and data volition be displayed using printf office.
- Subsequently performing a reading operation file is again airtight using the fclose function.
Summary
- A file is a space in a retentivity where information is stored.
- 'C' programming provides various functions to deal with a file.
- A machinery of manipulating with the files is chosen equally file direction.
- A file must be opened before performing operations on information technology.
- A file can be opened in a read, write or an suspend mode.
- Getc and putc functions are used to read and write a single character.
- The role fscanf() permits to read and parse data from a file
- Nosotros tin read (using the getc function) an entire file by looping to embrace all the file until the EOF is encountered
- We can write to a file afterwards creating its name, by using the office fprintf() and it must have the newline character at the cease of the string text.
Source: https://www.guru99.com/c-file-input-output.html
0 Response to "Linux if a File Can Read in C"
Postar um comentário