File IO

File IO

How to write programs to read from or write to files

So far, we've only looked at how to read/write to the console

File IO

Open files

Files are opened with fopen

The fopen function is part of stdio.h

It takes two args: a string (the name of a file) and a mode tring (how the file is opened)

It returns a file pointer

FILE* fptr = fopen("data.txt", "rt");
			
File IO

Declare and asign file pointer

fopen return a NULL pointer if it cannot perform the operation

FILE *f;
f = fopen("file.txt", "rt");
if(f == NULL){
	//bad file opening
}
			
File IO

File modes

ModeDescription
"r" or "rt"Read from the beginning of an existing file
"w"Make an empty file (wipe out old contents), start writing
"a"find an existing file, starting writing at the end of that file
File IO

Close files

Files are close with fclose

The fclose function is part of stdio.h

It takes one argument: pointer to the file to be closed

close(fptr);
			
File IO

The "f" function

Most of the I/O functions we know have an equivalent "f" version to work with a file.

The first argument is the file pointer value of an opened file:

fprintf(fptr, "format string", values)

fscanf(fptr, "format string", address)

feof(fptr) end of file test

File IO

Function fgetc

The C library function int fgetc(FILE *stream) gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream

Parameters

stream - This is the pointer to a FILE object that identifies the stream on which the operation is to be performed.

Return Value

This function returns the character read as an unsigned char cast to an int or EOF on end of file or error.

Example

FILE *f = fopen("data.text", "rt");
while(!feof(f)){
	int ch = fgetc(f);
	putc(ch, stdout);
}
fclose(f);
    		
File IO

Function fputc

The C library function int fputc(int char, FILE *stream) writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream.

Parameters

char − This is the character to be written. This is passed as its int promotion.

stream − This is the pointer to a FILE object that identifies the stream where the character is to be written.

Return Value

If there are no errors, the same character that has been written is returned. If an error occurs, EOF is returned and the error indicator is set.

Example
FILE *f = fopen("data.txt", "wt");
unsigned char ch = 'A';
fputc(ch, f);
fclose(f);
    		
File IO

Function fread

The C library function size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) reads data from the given stream into the array pointed to, by ptr.

Parameters

ptr − This is the pointer to a block of memory with a minimum size of size*nmemb bytes.

size − This is the size in bytes of each element to be read.

nmemb − This is the number of elements, each one with a size of size bytes.

stream − This is the pointer to a FILE object that specifies an input stream.

Return Value

The total number of elements successfully read are returned as a size_t object, which is an integral data type. If this number differs from the nmemb parameter, then either an error had occurred or the End Of File was reached.

File IO

Function fread

Example
FILE *f = fopen("data.txt", "rt");
unsigned char buf[1024];
fread(buf, sizeof(buf[0]), 1024, f);
printf("%s\n", buf);
fclose(f);
    		
File IO

Function fwrite

The C library function size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) writes data from the array pointed to, by ptr to the given stream.

Parameters

ptr − This is the pointer to the array of elements to be written.

size − This is the size in bytes of each element to be written.

nmemb − This is the number of elements, each one with a size of size bytes.

stream − This is the pointer to a FILE object that specifies an output stream.

Return Value

This function returns the total number of elements successfully returned as a size_t object, which is an integral data type. If this number differs from the nmemb parameter, it will show an error.

File IO

Function fwrite

The C library function size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) writes data from the array pointed to, by ptr to the given stream.

Example
FILE *f = fopen("data.txt", "wt");
char str[] = "This is course data structure and algorithm";
fwrite(str, sizeof(char), sizeof(str)/ sizeof(char), f);
fclose(f);

		    
File IO

Function fseek

The C library function int fseek(FILE *stream, long int offset, int whence) sets the file position of the stream to the given offset

Parameters

stream − This is the pointer to a FILE object that identifies the stream.

offset − This is the number of bytes to offset from whence.

whence − This is the position from where offset is added. It is specified by one of the following constants:

Sr.NoConstantDescription
1SEEK_SETBeginning of file
2SEEK_CURCurrent position of the file pointer
3SEEK_ENDEnd of file

Return Value

This function returns zero if successful, or else it returns a non-zero value

File IO

Function fseek

Example
FILE * f = fopen("data.text", "wt");
fputs(f, "This is subject data structure and algorithm");
fseek(f, 7, SEEK_SET);
fputs(f, "class");
fclose(f);

			
File IO

Function ftell

long ftell(FILE *stream)

Parameters

stream − This is the pointer to a FILE object that identifies the stream

Return Value

This function returns the current value of the position indicator. If an error occurs, -1L is returned, and the global variable errno is set to a positive value.

Example
FILE * f = fopen("data.text", "rb");
fseek(f, 0, SEEK_END);
long size = ftell(f);
fclose(f);
printf("Size of the file %ld", size);
			
File IO

Remove function

int remove(const char* filename)

remove file from path

Rename function

int rename(const char* oldname, const char* newname)

rename file from path oldname to path newname