Huwebes, Pebrero 2, 2012

File Handling In C

~~~~~~~~~~ Overview ~~~~~~~~~~

Part of any useful computer program is often going to be manipulation of external data sources. The file is the basic unit of storage for many operating systems, from Unix to Mac. Any C development environment on these platforms will include functions allowing the programmer to:

Open & Close files;
Read from & Write to files;
Delete files.

Commands for all of the above are found in the stdio.h header file.
 

~~~~~~~~~~ Opening and Closing Files ~~~~~~~~~~

The value returned is a handle to a file, defined in stdio.h, as FILE *. A null pointer is returned and can be tested for in case the call fails. So, the following is a good test for presence of a file:

FILE * hFile;
hFile = fopen( filename, "r");
if (hFile == NULL)
{
// Error, file not found
}
else
{
// Process & close file//
fclose (hFile);
}
 

~~~~~~~~~~ File Modes ~~~~~~~~~~

The mode value in the above example is set to 'r', indicating that we want to read from the file. Other possible values are:
w - write to the file, overwriting existing data
a - append to an existing file
r+ - read and write to a file
Using 'r+' means that all writing will take place at the end of the file, and that reading is sequential. 

The fclose function closes a valid file handle.

~~~~~~~~~~ Reading a file ~~~~~~~~~~

When an r is used, the file is opened for reading
Using the r indicates that the file is assumed to be a text file.

/* Program to display the contents of a file on screen */ 
#include <stdio.h> 
void main() 
{ 
FILE *fp; 
char c; 
fp = fopen ( “sample.txt" , "r" ); 
c = getc (fp) ; 
while (c != EOF) 
{ 
putchar (c); 
c = getc (fp); 
} 
fclose (fp); 
}
~~~~~~~~~~ Writing a file ~~~~~~~~~~

When a file is opened for writing, it will be created if it does not already exist and it will be reset if it does, resulting in the deletion of any data already there

#include <stdio.h> 
int main() 
{ 
FILE *fp; 
fp = fopen("file.txt","w");          /*Create a file and add text*/ 
fprintf(fp,"%s","This is just an example :)");          /*writes data to the file*/ 
fclose(fp);          /*done!*/ 
return 0; 
}
~~~~~~~~~~ Appending a file ~~~~~~~~~

When a file is opened for appending, it will be created if it does not already exist and it will be initially empty
If it does exist, the data input point will be positioned at the end of the present data so that any new data will be added to any data that already exists in the file.
#include <stdio.h> 
int main() 
{ 
FILE *fp 
fp = fopen ( “sample.txt" , "a" );
fprintf (fp,"%s","This is just an example :)");          /*append some text*/
fclose (fp); 
return 0; 
}
~~~~~~~~~~ Reading from a File ~~~~~~~~~~

#include <stdio.h> 
main( ) 
{ 
FILE *fp; char c; 
fp = fopen( “SAMPLE.TXT", "r" ); 
if (fp == NULL) 
printf ( "File doesn't exist\n" ); 
else 
{ 
do
{ 
c = getc (fp);          /* get one character from the file */ 
putchar (c);           /* display it on the monitor */ 
}
while (c != EOF); /* repeat until EOF (end of file) */ 
}
fclose(fp); 
}

Walang komento:

Mag-post ng isang Komento