Write a Code to Read a File in Hex
C File management
A File tin be used to store a large 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 most of import file direction functions available in 'C,'
function | 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 block data from a file | |
getc () | Reads a single graphic symbol from a file | |
putc () | Writes a unmarried 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 arrow to a specified location | |
ftell () | Returns the current position of a file arrow | |
rewind () | Sets the file pointer at the get-go of a file |
In this tutorial, you will learn-
- How to Create a File
- How to Close a file:
- Writing to a File
- fputc() Office:
- fputs () Function:
- 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 nada but infinite in a memory where data is stored.
To create a file in a 'C' programme following syntax is used,
FILE *fp; fp = fopen ("file_name", "mode");
In the above syntax, the file is a data structure which is defined in the standard library.
fopen is a standard function which is used to open a file.
- If the file is not present on the system, and then information technology is created and then opened.
- If a file is already nowadays on the system, and then it is directly opened using this function.
fp is a file pointer which points to the type file.
Whenever you lot open or create a file, you have to specify what you lot are going to practice with the file. A file in 'C' programming tin exist created or opened for reading/writing purposes. A fashion is used to specify whether you desire to open a file for whatsoever of the below-given purposes. Following are the different types of modes in 'C' programming which can be used while working with a file.
File Fashion | Clarification |
---|---|
r | Open a file for reading. If a file is in reading manner, then no data is deleted if a file is already present on a system. |
westward | Open a file for writing. If a file is in writing way, then a new file is created if a file doesn't exist at all. If a file is already present on a system, then all the information inside the file is truncated, and information technology is opened for writing purposes. |
a | Open a file in append manner. If a file is in append mode, and so the file is opened. The content inside the file doesn't alter. |
r+ | open for reading and writing from start |
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 way are specified as strings hence they must e'er be enclosed within double quotes.
Example:
#include <stdio.h> int main() { FILE *fp; fp = fopen ("data.txt", "w"); }
Output:
File is created in the aforementioned folder where you have saved your code.
You tin can specify the path where you want to create your file
#include <stdio.h> int main() { FILE *fp; fp = fopen ("D://data.txt", "w"); }
How to Close a file
One should always shut a file whenever the operations on file are over. Information technology means 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 equally follows,
fclose (file_pointer);
Example:
FILE *fp; fp = fopen ("data.txt", "r"); fclose (fp);
The fclose part takes a file pointer every bit an argument. The file associated with the file pointer is then closed with the aid of fclose role. It returns 0 if close was successful and EOF (cease of file) if there is an mistake has occurred while file endmost.
After closing the file, the same file pointer tin can likewise be used with other files.
In 'C' programming, files are automatically shut when the program is terminated. Endmost a file manually past writing fclose function is a good programming practice.
Writing to a File
In C, when you write to a file, newline characters '\northward' must be 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 past 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 cord to the file pointed to past file_pointer. The string tin optionally include format specifiers and a listing of variables variable_lists.
The plan below shows how to perform writing to a file:
fputc() Function:
#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 manner" for (i = 0; str[i] != '\n'; i++) { /* write to file using fputc() part */ fputc(str[i], fptr); } fclose(fptr); return 0; }
Output:
The above program writes a unmarried character into the fputc_test.txt file until it reaches the next line symbol "\north" which indicates that the judgement was successfully written. The process is to take each grapheme of the array and write it into the file.
- In the to a higher place program, we have created and opened a file called fputc_test.txt in a write mode and declare our string which volition be written into the file.
- We practice a character by grapheme write performance using for loop and put each character in our file until the "\n" grapheme is encountered then the file is closed using the fclose role.
fputs () Function:
#include <stdio.h> int main() { FILE * fp; fp = fopen("fputs_test.txt", "due west+"); fputs("This is Guru99 Tutorial on fputs,", fp); fputs("We don't need to utilise for loop\n", fp); fputs("Easier than fputc function\n", fp); fclose(fp); render (0); }
OUTPUT:
- In the above program, we accept created and opened a file called fputs_test.txt in a write manner.
- Later on we exercise a write performance using fputs() function by writing iii dissimilar strings
- And so the file is airtight using the fclose function.
fprintf()Part:
#include <stdio.h> int principal() { FILE *fptr; fptr = fopen("fprintf_test.txt", "w"); // "west" defines "writing way" /* write to file */ fprintf(fptr, "Learning C with Guru99\northward"); fclose(fptr); return 0; }
OUTPUT:
- In the in a higher place program we have created and opened a file called fprintf_test.txt in a write mode.
- After a write operation is performed using fprintf() function past writing a string, then the file is closed using the fclose role.
Reading data from a File
At that place are three different functions dedicated to reading data from a file
- fgetc(file_pointer): It returns the next grapheme from the file pointed to by the file pointer. When the end of the file has been reached, the EOF is sent back.
- fgets(buffer, n, file_pointer): It reads n-ane characters from the file and stores the string in a buffer in which the NULL character '\0' is appended as the concluding grapheme.
- fscanf(file_pointer, conversion_specifiers, variable_adresses): Information technology is used to parse and clarify 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 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 principal() { 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\north", buffer); printf("----read and parse data----\due north"); file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer char str1[ten], str2[ii], str3[20], str4[2]; fscanf(file_pointer, "%s %s %s %due south", str1, str2, str3, str4); printf("Read String1 |%south|\n", str1); printf("Read String2 |%southward|\n", str2); printf("Read String3 |%southward|\n", str3); printf("Read String4 |%s|\n", 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); render 0; }
Issue:
----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 to a higher place program, we have opened the file called "fprintf_test.txt" which was previously written using fprintf() office, and it contains "Learning C with Guru99" cord. We read it using the fgets() function which reads line by line where the buffer size must be plenty to handle the unabridged line.
- We reopen the file to reset the pointer file to point at the beginning of the file. Create diverse strings variables to handle each word separately. Print the variables to run into their contents. The fscanf() is mainly used to extract and parse data from a file.
- Reopen the file to reset the pointer file to bespeak at the beginning of the file. Read data and print information technology from the file character by grapheme using getc() part until the EOF statement is encountered
- Later on performing a reading operation file using unlike variants, we again airtight the file using the fclose function.
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 only a unmarried character at a time.
Following program demonstrates the file handling functions in 'C' programming:
#include <stdio.h> int chief() { FILE * fp; char c; printf("File Handling\n"); //open a file fp = fopen("demo.txt", "west"); //writing functioning while ((c = getchar()) != EOF) { putc(c, fp); } //close file fclose(fp); printf("Data Entered:\due north"); //reading fp = fopen("demo.txt", "r"); while ((c = getc(fp)) != EOF) { printf("%c", c); } fclose(fp); render 0; }
Output:
- In the above program we have 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 role.
- We take again opened a file which at present contains data in a reading mode. A while loop will execute until the eof is institute. One time the cease of file is found the functioning will be terminated and information will be displayed using printf function.
- After performing a reading performance file is over again closed using the fclose function.
Summary
- A file is a space in a memory where data is stored.
- 'C' programming provides diverse functions to deal with a file.
- A mechanism of manipulating with the files is chosen as file direction.
- A file must be opened before performing operations on it.
- A file can be opened in a read, write or an append manner.
- Getc and putc functions are used to read and write a single character.
- The function fscanf() permits to read and parse data from a file
- Nosotros tin can read (using the getc function) an unabridged file by looping to cover all the file until the EOF is encountered
- We can write to a file after creating its name, by using the function fprintf() and it must have the newline graphic symbol at the cease of the cord text.
Source: https://www.guru99.com/c-file-input-output.html
0 Response to "Write a Code to Read a File in Hex"
Post a Comment