0% found this document useful (0 votes)
2 views

08_Files_7113d00e6ea5d7136ca7029de837dbe0

Uploaded by

Svsn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

08_Files_7113d00e6ea5d7136ca7029de837dbe0

Uploaded by

Svsn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

22MT240

Problem Solving using C

Files

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 1


Introduction
• Need to record the temperature for every 1 hour and analyze later ?

• Files….

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 2


Basic File Operations
• Create a new file or open an existing file
• Read from / Write to a file
• Close the file

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 3


File Pointer
• For performing the operations on the file, a special pointer called File pointer
is uses
• File pointer is a pointer which is used to handle and keep track on the files
being accessed
• A new data type called “FILE” is used to declare file pointer
• This data type is defined in stdio.h file

File Pointer Declaration

FILE *filePointer;

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 4


File open
• Create a new file or open an existing file

FILE *filePointer;
filePointer = fopen(“fileName.txt”, “mode”);

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 5


File open - mode FILE *filePointer;
filePointer = fopen(“fileName.txt”, “mode”);

• r – read mode
• Searches for the file, if the file exists then fopen( ) loads it into memory and sets up a
pointer which points to the first character in it
• If the file cannot be opened or file does not exist, then fopen( ) returns NULL

• w – write mode
• Searches for the file, if the file exists, then its contents are overwritten
• If the file doesn’t exist, a new file is created
• Returns NULL, if unable to open file

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 6


File open - mode FILE *filePointer;
filePointer = fopen(“fileName.txt”, “mode”);

• a – append mode
• Searches for the file, if the file is opened successfully fopen() loads it into memory and
sets up a pointer that points to the last character in it
• If the file doesn’t exist, a new file is created and fopen() loads it into memory and sets
up a pointer which points to the first position in it
• Returns NULL, if unable to open file

• r+ – read / write mode


• w+ – read / write mode
• a+ – read / write mode
Sheik Masthan SAR | AP Mechatronics | TCE | MDU 7
File write

File write function declaration

int fputs( const char *s, FILE *fp );


Or
fprintf(FILE *fp, const char *s);

• It returns a non-negative value on success, otherwise EOF is returned in case


of any error

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 8


File close
• One must always close the file after using it

File write function declaration

int fclose( FILE *fp );

• The fclose() function returns zero on success, or EOF if there is an error in


closing the file
• fclose() flushes any data still pending in the buffer to the file, closes the file,
and releases any memory used for the file

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 9


Example

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 10


File read
File read function declaration

char fgets( char *buf, int n, FILE *fp );


Or
int fscanf(FILE *fp, const char *format);

• The functions fgets() reads up to n-1 characters from the input stream
referenced by fp. It copies the read string into the buffer buf, appending
a null character to terminate the string

• fscanf() function read strings from a file, but it stops reading after
encountering the first space character

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 11


Example

Output

1 : This
2 : is testing for fprintf...

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 12


Example

Output

1 : This is

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 13


Other File Operations
• feof () - finds end of file
• fseek () - moves file pointer position to given location
• SEEK_SET moves file pointer position to the beginning of the file
• SEEK_CUR moves file pointer position to given location
• SEEK_END moves file pointer position to the end of file
• ftell () - gives current position of file pointer
• rewind () - moves file pointer position to the beginning of the file

Sheik Masthan SAR | AP Mechatronics | TCE | MDU 14

You might also like