[go: up one dir, main page]

0% found this document useful (0 votes)
3 views4 pages

Basics

The document provides an overview of file handling in C, detailing functions for opening, closing, reading, writing, and manipulating files. It includes information on file modes, return values for various functions, and examples of copying files. Additionally, it discusses the use of buffers and memory management with malloc and free.

Uploaded by

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

Basics

The document provides an overview of file handling in C, detailing functions for opening, closing, reading, writing, and manipulating files. It includes information on file modes, return values for various functions, and examples of copying files. Additionally, it discusses the use of buffers and memory management with malloc and free.

Uploaded by

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

File handling in C is Easy, but needs fucss

<stdlib.h>
first thing:
fopen (file_name, mode); to open a file or create it if not existing.

fclose(pointer_to_file);

remove(file_name);

rename(old_file_name, new_file_name);

Reading:
fread(buffer, sizeof, coun, *file pointer);

fgetc(File_pointer)

fgets(buffer, sizeof , file_pointer/stdin(for diret input from user) )

fprintf(buffer, file_pointer)

Writing:
fwrite(buffer, sizeof, count, file_pointer);

fputc(buffer, File_pointer)

fputs(buffer, file_pointer)

others:
ftell(File_pointer)

fseek(File_pointer , NUMBER OF BYTIES TO OFFSET USUALLY (0) , SEEK_SET /


SEEK_CUR / SEEK_END)

rewind(File_pointer)

EORROR:

feof(File_pointer)

ferror(File_ptr)

clearerr(file_ptr)

Modes:
r for reading text files

rb for binary files (images, videos , audios ...)


w for writing files

wb for writing binary files

r+ reading and writing


w+ readin gand writing

a for appending

a+ for reading writing and appending

return values:
*******************************************************
method success fail
*******************************************************
fopen() file NULL
fclose() 0 EOF(-1)
remove() 0 NONE_zero_value(usually -1)
rename() 0 usually (-1)
*********************************************************************

fread() Num_of_successfly elements read

fgetc() char EOF (End Of File)


fgets() str NULL
fscanf() number_of.... EOF
*********************************************************************

fwrite() num_element_sucessfully written

fputc() int_value_of_char EOF

fputs() NON_ngeatvie_number EOF

fprintf() total_num_of_written_chars negative_num

**********************************************************************
fseek() 0 none_zero_value

ftell() current_value(long int) -1(long int)

rewind() Does Not Return anything (void)

feof() none_zero integer 0

ferror() none_zero integer 0

clearerr() Void

**********************************************************************
usually you need a buffer to store the data temporrarly until you print it or
insert it into a file

You can use malloc and free to assign a memorry to your buffer

example:

char *buffer = malloc(sizeof(char)*100);

free(buffer);

If you want to copy files such as (images, videos, audios....)

use rb and wb
Example:
FILE *org = fopen("ali.jpg","rb");
FILE *copy = fopen("copy.jpg","wb");

char buffer[1024];

size_t bytes;

while((bytes = fread(buffer, 1, sizeof(buffer),org)) > 0)


{
fwrite(buffer , 1, bytes, dest);

printf("image copied successfully!\n");

fclose(org);
fclose(copy);

##>>fseek(*f, offset, SEEK_SET/SEEK_CUR/SEEK_END);


You can go to any place in the file using this function

if you want to check the first char:


fseek(*f, 0, SEEK_SET); >>> 0
fseek(*f, 2, SEEK_SET); >>> 2

fseek(*f, 1 , SEEK_CUR); >>> 3 (because the current is 2 and I offset


it by 1 )

fseek(*f, 0, SEEK_CUR); >>> the number of LAST byte in the file

for example:
t.txt = 70 bytes

fseek(*f, 0, SEEK_END)>>> 69 BECAUSE IT STARTS FROM ZERO

You might also like