File I/O
Files are a fundamental piece of any program as we can take input from and print yield in documents. We can likewise save a great deal of program space by getting to the document's information just when required, making the program more productive and quicker. Documents are put away in non-unstable memory.
Reason for documents in C -
- Files are utilized to store content henceforth lessening the program's size.
- We can peruse or get information from the file.
- The information in Files remains put away even after the program's execution is ended.
Kinds of Files -
- Binary Files -
- This documents stores information in 01 i.e., double organization.
- They are not straightforwardly decipherable.
- An application or programming is needed to peruse twofold documents.
- A model is a .doc file.
- Text Files -
- They store information in basic text design.
- They are straightforwardly decipherable.
- No product is needed to get to them.
- A model is a .txt file.
Modes -
- r - opens a record for perusing.
- w - opens a record for composing. It can likewise make another record.
- a - opens a document for attaching.
- r+ - opens a document for both perusing and composing yet can't make another record.
- w+ - opens a record for both perusing and composing.
Note - There are numerous different modes, yet these are the fundamental and most utilized ones.
Making a File - We can make a record utilizing C language, in any index, without leaving our compiler. We can choose the name or type we need our file to have, alongside its area.
Opening a File -
- We can open a current document and make another file and open it utilizing our program.
- We can perform various procedures on a document after it has been opened.
- We utilize the open() work for opening documents in C.
- ptr = fopen(file_location,mode);
Shutting a File -
- When we are finished with the file, implying that we have played out anything we desire to perform on our document, we can close the document utilizing the nearby capacity.
- Shutting an open record is perhaps the most pivotal step while managing C.
- Documents don't consequently get shut in the wake of working with them. We need to close them physically.
- To close a document, we need to utilize the fclose() work.
- The language structure is direct because we simply need to send the pointer as a boundary to the capacity.
Reading a File -
- To peruse a record in C, we utilize the capacity fscanf(). This capacity is a document variant of scanf(). Like scanf() used to get input from the console, it gets its contribution from a record and prints it onto the screen.
- We need to send the record pointer as a contention for the program to have the option to understand it. The record must be opened in r mode, i.e., read mode, to turn out appropriately for fscanf().
Peruse/Write to a document -
- After opening a file, we can get to its substance and peruse, compose, or update them.
- To keep in touch with a record, we utilize the capacity fprintf(). The capacity is a document rendition of printf(). Same as we used to print text onto the screen utilizing printf(), we use fprintf() to print text inside the record.
- We need to send the document pointer as a contention for the program to have the option to print it into the record. The document must be opened in w mode, for example, compose mode, to turn out appropriately for fscanf().
- fputc() -
- It is utilized to compose characters to the document. The C for the sake of the capacity represents the character. The capacity accepts two boundaries as information.
- The first is the single person that we need to enter into the record. The subsequent boundary is the pointer to the document. On fruitful execution, it returns the person to the screen.
- If it couldn't do as such if there should be an occurrence of some other issue, it would show an EOF special case. EOF represents End of File. You will see a great deal of this exemption while working with records.
- Language structure - type fputc(character,file_pointer);
- fputs() -
- It is utilized to compose an invalid-ended string to the document. The S for the sake of the capacity represents a string.
- It additionally takes two boundaries, as old as(). One is the variable putting away the string and the subsequent one is the pointer to the document.
- An invalid-ended string is a person string that can be ended by an invalid person i.e., \0. You don't need to trouble much with regards to invalid-ended strings as our PC consequently changes over character strings to invalid-ended strings.
- Language structure - fputs(string,file_pointer);
- fgetc() -
- It works precisely something contrary to fputc(). It peruses the person from the document. It peruses just each character in turn.
- We can print it, however, on many occasions as we need to get the following person, etc.
- Its linguistic structure is direct, as we need to send the record pointer as a boundary. We can store the person into one more person to show it on the screen.
- Sentence structure - type = fgetc(file_pointer);
- fgets() -
- It is utilized to peruse a string from a document. It accepts three boundaries as information and stores them in an invalid-ended cluster.
- The first is the capacity cluster we need our string to store. The third one is the document pointer, and the subsequent one is the count of factors we need to get from the records.
- Sentence structure - int fgets(const roast *s, int n, file_pointer);
- Note: Blank space is likewise considered as a character.
Conclusion -
We can perform numerous tasks utilizing documents, as we can peruse, compose, or annex them. We can likewise open, close, or make a file. Utilizing documents in our projects can make them substantially more effective and save a great deal of memory space. We can likewise store our information for a drawn-out period.
0 Comments