Saturday, January 14, 2012

Handling Files


Written by Erez Wenger - January 2012

In this post we will show how to read and write files.


Although there are many tutorials and references online dealing with C language file handling, we think it will still be beneficial to provide you with a short review of our own.

How to write to a file:

FILE *fp; // declaring a FILE type pointer to handle the file

fp=fopen("myfilename.csv","w"); // open the file for writing

for (i=0;i<10;i++)
fprintf(fp,"%d, %d \n",i,i*i); // write 10 lines of data

fclose(fp); // close the file and save it

Remarks
  • while using fopen with the 'w' argument, it starts from the beginning of the file, so if the file already had data in it, it will be deleted. you can use 'a' (append) if you want to write at the end of the file which will save the data that was there.
  • Files in the 'csv' format (Comma Separated Values) are very useful - the are actually simple text files which can be opened in MS Excel and automatically be arranged in columns, to make it work place commas between segments of data.

How to read a file:

FILE *fp; // declaring a FILE type pointer to handle the file
char linebuffer[300]; // declaring a string
int x[1000], y[1000], counter; // two int arrays to store the data
                                                  //(easy way to work with under 1000 sets of values
                                                 //but can be more efficient with memry allocation)

fp=fopen("myfilename.csv","r"); // open the file for writing

counter=0;

while (fgets(linebuffer, 300, fp)!=0) // read an entire line (of up to 300 characters)
                                                         // from file to the linebuffer string until end of file
{
sscanf(linebuffer, "%d, %d \n", &x[counter], &y[counter]);
          // reads the values from the string and stores them into the arrays
counter++; // promotes the counter
}

fclose(fp); // close the file and save it

Notes
  • Remember that sscanf (like scanf and fscanf) scans strings (%s) by running until it gets to a space character or end of line (or '\0'). If you are reading a string followed by another piece of data coming immediately after, with no space between them (such as "Tom,12.3" where the string is "tom" and the second piece of data is the double12.3), the second piece of data will be scanned as part of the string. The best way to deal with it is to write your own function or line of code to separate the string from the rest of the data (like PHP's explode function).

File Pointer and Movement

Whenever you open a file for reading or writing (using the fopen function) the file pointer is set to a specific position in the file, usually it will point at the beginning of the file, but it can also point to the end of the file if you open the file for appending. 

As you read from or write to the file, the pointer moves along with your commands. For example after opening a file for reading and using the fgets function, the file pointer will move and eventually point to the address of the first character in the second line of the file (assuming fgets is configured to acquire an entire line). 

If you want to manipulate the file pointer, moving it to a new line or finding a specific location to read, you should use the fseek function:

The prototype of the function is:

int fseek(FILE *stream, long offset, int whence);

It takes three parameters as follows:
  1. The file pointer
  2. How many Bytes to move from the location specified at the next parameter (can also be negative).
  3. What is the origin of the movement, there are 3 choices:

    • SEEK_CUR - Current position.
    • SEEK_SET - The beginning of the file.
    • SEEK_END - The end of the file.
for example to move 4 characters ahead from current position, on a standart text file will be:


fseek(fp, 4*sizeof(char), SEEK_CUR);

3 comments:

  1. tell me how i reset numeric box. I can rest textbox same i cannot rest numeric box.

    ReplyDelete
  2. Highly energetic blog, I enjoyed that a lot. Will there be a
    part 2?

    ReplyDelete
  3. Hello excellent website! Does running a blog such as this take a massive amount work?
    I've virtually no understanding of computer programming however I had been hoping to start my own blog soon. Anyways, should you
    have any ideas or techniques for new blog owners please share.
    I understand this is off subject but I simply wanted to ask.
    Kudos!

    ReplyDelete