File Handling

How can I save results from a C program into a file?

As in other computer programs C uses files to store, write, and read data. C is text oriented language, thus the reading, writing, and storing files involves text based conventions in C.

How can I use data stored in a file in a C program?

Communication is based on conventions. Between two things, conventions or protocols or agreement is need for proper communications to take place. Thus, in order to use data in C program the data must be presented correctly in the data file and the C program must be able to properly handle the data.

What kind of data should I expect to be used in a C program?

That's a difficult question to answer.
  • single character
  • string of characters
  • numbers (integers, floating)
  • mixed: single characters and string of characters

    What are the basic tasks involving files?

  • Reading from a file
  • Writing to a file
  • Reading from and writing to a file

    Are there any other tasks involving files?

  • Creating a file
  • Formatting or deleting all the information in a file
  • Deleting a file

    How do I refer to items in a file?

    In C, you first need to open the file (not literally), and make a file pointer refer to the file. Don't worry much about the pointing scheme, just know that pointer to a file is need to refer to the contents in the file or to write to the file.

    How can I refer to specific items in a file?

    Again, the functions involved in using files in C program are very primitive. Thus, a beginning programmer who is familiar with handling files using GUI (graphical user interface), and features such as cut and past, find or search, and replace will find it tedious or limited in dealing with files in C programming. I am not exactly sure how you can refer to specific items within a file other than to run a 'search and test' through the whole file.

    Can you give an example of reading and using a data file?

    Consider the example of data concerning a web site use by students.

    The first column is the Student Number, the second column is the Number of Logins, and the third column is the Duration for each login (a hypothetical example).


    /* Project:	Lab8 */
    /* File:	files2.C */
    /* Aim:		Reading data file in one line*/
    /* Keywords:	none */
    
    #include 
    
    main()
    {
    
    	/* This pointer is used to refer to a file */
    	FILE  *file_pointer;
    
    	char file_character;
    	char array[16];
    	int i=0, k=0;
    
    	/* Here the pointer is made to refer to the particular file */
    	file_pointer = fopen("DATA1.DAT", "r");
    
    
    	/* Below we are using the file contents */
    	file_character=getc(file_pointer);
    
    	while(file_character != EOF)
    		{
    			printf ("%c", file_character);
    			array[i]=file_character;
    			i = i + 1;
    			file_character=getc(file_pointer);
    		}
    
    
    	for (k=0; k<14; k=k+1)
    	{
    		printf ("%c\n", array[k]);
    	}
    
    	/* This last step is necessary to free up memory and the pointer*/
    	fclose(file_pointer);
    }
    



    How about file with text and data?

    Handling files involves many more functions, and will be considered in another section.