C programming file management in c tutorial




















For performing the operations on the file, a special pointer called File pointer is used which is declared as. Skip to content. Change Language. Related Articles. Table of Contents. Improve Article. Save Article. Like Article. Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more!

The filename argument can be a literal string enclosed in double quotation marks or a pointer to a string variable. The argument mode specifies the mode in which to open the file. In this context, mode controls whether the file is binary or text and whether it is for reading, writing, or both. The permitted values for mode are listed in Table. The default file mode is text. To open a file in binary mode, you append a b to the mode argument.

Thus, a mode argument of a would open a text-mode file for appending, whereas ab would open a binary-mode file for appending. Remember that fopen returns NULL if an error occurs.

Error conditions that can cause a return value of NULL include the following:. Whenever you use fopen , you need to test for the occurrence of an error. There's no way to tell exactly which error occurred, but you can display a message to the user and try to open the file again, or you can end the program.

Most C compilers include non-ANSI extensions that let you obtain information about the nature of the error; refer to your compiler documentation for information. This program prompts you for both the filename and the mode specified on lines 15 through After getting the names, line 22 attempts to open the file and assign its file pointer to fp.

As an example of good programming practice, the if statement on line 22 checks to see that the opened file's pointer isn't equal to NULL. If fp isn't equal to NULL, a message stating that the open was successful and that the user can continue is printed. If the file pointer is NULL, the else condition of the if loop executes.

The else condition on lines 33 through 42 prints a message stating that there was a problem. It then prompts the user to determine whether the program should continue. You can experiment with different names and modes to see which ones give you an error.

TXT in mode r resulted in an error because the file didn't exist on the disk. If an error occurs, you're given the choice of entering the information again or quitting the program.

To force an error, you could enter an invalid filename such as []. A program that uses a disk file can write data to a file, read data from a file, or a combination of the two. You can write data to a disk file in three ways:. When you want to read data from a file, you have the same three options: formatted input, character input, or direct input.

The type of input you use in a particular case depends almost entirely on the nature of the file being read. Generally, you will read data in the same mode that it was saved in, but this is not a requirement. However, reading a file in a mode different from the one it was written in requires a thorough knowledge of C and file formats.

The previous descriptions of the three types of file input and output suggest tasks best suited for each type of output. This is by no means a set of strict rules. The C language is very flexible this is one of its advantages!

As a beginning programmer, it might make things easier if you follow these guidelines, at least initially. It is directly analogous to formatted keyboard input and screen output done with the printf and scanf functions, as described on Day I'll discuss formatted output first, followed by the input. Formatted file output is done with the library function fprintf.

H, and it reads as follows:. The first argument is a pointer to type FILE. To write data to a particular disk file, you pass the pointer that was returned when you opened the file with fopen. The second argument is the format string. You learned about format strings in the discussion of printf on Day The format string used by fprintf follows exactly the same rules as printf.

Refer to Day 14 for details. The final argument is What does that mean? In a function prototype, ellipses represent a variable number of arguments. In other words, in addition to the file pointer and the format string arguments, fprintf takes zero, one, or more additional arguments. This is just like printf. These arguments are the names of the variables to be output to the specified stream. Remember, fprintf works just like printf , except that it sends its output to the stream specified in the argument list.

In fact, if you specify a stream argument of stdout, fprintf is identical to printf. You might wonder why the program displays This isn't an error in the program. The input output library supports the function to close a file; it is in the following format. This would close the file associated with the file pointer. Observe the following program. The above program opens two files and closes them after all operations on them are completed, once a file is closed its file pointer can be reversed on other file.

The getc and putc functions are analogous to getchar and putchar functions and handle one character at a time. The putc function writes the character contained in character variable c to the file associated with the pointer fp1. The program shown below displays use of a file operations. The data enter through the keyboard and the program writes it. Character by character, to the file input.

The end of the data is indicated by entering an EOF character, which is control-z. These are integer-oriented functions.

They are similar to get c and putc functions and are used to read and write integer values. These functions would be usefull when we deal with only integer data. The general forms of getw and putw are:. The fprintf and fscanf functions are identical to printf and scanf functions except that they work on files.

The first argument of theses functions is a file pointer which specifies the file to be used. The general form of fprintf is. Where fp id a file pointer associated with a file that has been opened for writing.

The control string is file output specifications list may include variable, constant and string. Here name is an array variable of type char and age is an int variable The general format of fscanf is.

Text files Text files are the normal. Binary files Binary files are mostly the. Instead of storing data in plain text, they store it in the binary form 0's and 1's. File Operations In C, you can perform four major operations on files, either text or binary: Creating a new file Opening an existing file Closing a file Reading from and writing information to a file Working with files When working with files, you need to declare a pointer of type file.

The first function creates a new file named newprogram. The writing mode allows you to create and edit overwrite the contents of the file. Now let's suppose the second binary file oldprogram. The second function opens the existing file for reading in binary mode 'rb'. The reading mode only allows you to read the file, you cannot write into the file. If the file does not exist, fopen returns NULL. If the file exists, its contents are overwritten. If the file does not exist, it will be created.

Data is added to the end of the file. Closing a file is performed using the fclose function. Reading and writing to a text file For reading and writing to a text file, we use the functions fprintf and fscanf. Table of Contents Why files? Previous Tutorial:. Next Tutorial:. Share on:. Did you find this article helpful? Sorry about that.



0コメント

  • 1000 / 1000