C Datafiles

Wouldn't it be nice to have a particular type of data and be able to calculate the numbers anytime you like? If you wanted the mean, average,standard deviation or anything else, simply write a program that pulls data from a text file on your own computer. This is possible through reading and writing data files with a program.


Your data file must be written as a text file using something like notepad or wordpad, if you were to use Microsoft Word or something similar it wouldn't work right.This is because word has data invisible to the naked eye that would interfere with a program. First let me show you an example of a data file, imagine that the first column is time in seconds and the other column is feet, lets write a program that can take input in for a given amount for time and give us the corresponding length. For example, if we put 4 in for time we would expect our program to print out 33, you can copy and paste this if you wish.
1 13
2 18
3 29
4 33
5 47

The first thing you must do when programming with a data file is name it, here is how.

FILE *myfile;

This name does not have to much the file name that is stored on your computer, FILE must be capitalized, notice the asterisk? myfile is a pointer, this means we must give it an adress.

myfile=fopen("C:\\Program Files\\sample.txt","r");

I received the location of my file by right clicking the icon,scrolling down to properties and copying and pasting the location. Notice that when you do this there is only one slash not two how there is above, you must include another \ wherever there is one. Also, include two more slashes before you type in the name of your file, what you type in has to be the same name of your file stored in your computer. For instance my document was named sample, you must then add the .txt since it is text document. The "r" means that it is a read file, your program will read data from the file. If it were a "w" it would be a write file, your program will then write to the file. Now that we have opened the file, the safe thing to do is to check whether or not the file was opened successfully, this is done with a condition.

      printf("Error in opening file");

This is very useful if your program is giving trouble. Now that the the file is opened we want to scan in the data from the file to the program.

fscanf(myfile,"%i %i",&time,&feet);

Notice how similar this is to a normal scanf, the extra f is for file. Since a data file is read just as you read from left to right, we must scan it in that way. We know that both time and feet are integers so %i is appropriate (assuming we have declared them as integers). WAIT! This will scan in the very first row of data the 1 for time and the 13 for feet right, but what about the rest of the data? Simple, all we have to do is tell the program that we are expecting two values, time and feet every time we scan the file, so once we scan the file and two values aren't scanned we know that this must be the end of the data file. We accomplish this with a while loop.

while(fscanf(myfile,"%i %i",&time,&feet)==2)

Keep scanning while there are two values if not, end of file. The last special thing we must do is close the file, this is just safe practice.


There you have it, would you like to see a sample program?

#include <stdio.h>
#include <stdlib.h>
int main()
    int time,feet;

  FILE *myfile; /*declare myfile a pointer to a file*/
  myfile=fopen("C:\\Program Files\\sample.txt","r"); /*myfile points to the address of your text document*/
  if(myfile==NULL) /*if your file didn't open print an error message*/
       printf("Error in opening file");

    else{ /*if it did open do this stuff*/

    while(fscanf(myfile,"%i %i",&time,&feet)==2) /*while reading in two values every scan keep scanning*/
        printf("%i %i\n",time,feet); /*print out the values every scan*/
        } /*closes the else statement*/
fclose(myfile); /*close myfile*/
    return 0;

This program scans and prints out the data in the text document you wrote with either notepad or wordpad.