Вы находитесь на странице: 1из 17

Gandhinagar Institute of

Technology

Computer programming and utilization


Active learning assignment
topic: File management
Prepared by: shainilvyas(140120119252)

Ankit Yadav(140120119254)

vijay Yadav(140120119255)

Guided by : Prof.

Basic file operations

C provides several different file operations

fopen - open a file- specify how its opened


(read/write) and type (binary/text)

fclose - close an opened file

fread - read from a file

fwrite - write to a file

fseek/fsetpos - move a file pointer to somewhere


in a file.

ftell/fgetpos - tell you where the file pointer is


located.

Different
modes
mode

Writing
if

file already exists then contents are deleted,

else

new file with specified name created

Appending
if

file already exists then file opened with contents safe

else

new file created

Reading
if

mode

mode

file already exists then opened with contents safe

else

error occurs.

Additional
r+

modes

open to beginning for both reading/writing

w+

same as w except both for reading and writing

a+

same as a except both for reading and writing

Opening a FILE

Syntax

-FILE *fp; /*variable fp is pointer to type FILE*/


fp = fopen(filename, mode); /*opens file with name
filename , assigns
identifier to fp */
fp
contains all information about file
Communication link between system and program
The file mode tells C how the program will use the file.
The filename indicates the system name and location for
the file.
We assign the return value of fopen to our pointer variable:
fp = fopen(MYFILE.TXT, w);
fp = fopen(A:\\MYFILE.TXT, w);

Closing a FILE

When

we finish with a mode, we need to close the file


before ending the program or beginning another
mode with that same file.

To close a file, we use fclose and the pointer variable:


fclose(spData);

Example:
FILE *p1;
p1 = fopen(INPUT.txt, r);
..
..
fclose(p1);

pointer can be reused after closing

Input/output
operations
on FILES
C provides several different functions for reading/writing

getc() read a character

putc() write a character

fprintf() write set of data values

fscanf() read set of data values

getw() read integer

putw() write integer

getc() & putc()


handle

one character at a time like getchar() and


putchar()

syntax:
c

: a character variable

fp1

: pointer to file opened with mode w

syntax:
c

putc(c,fp1);

c = getc(fp2);

: a character variable

fp2

: pointer to file opened with mode r

file

pointer moves by one character position after every


getc() and putc()

getc()

returns end-of-file marker EOF when file end


reached

Program to read/write
using getc() & putc()
#include

<stdio.h>

main()
{
FILE *fp1;
char c;
f1= fopen(INPUT, w); /* open file for writing */
while((c=getchar()) != EOF) /*get char from keyboard until CTL-Z*/
putc(c,f1); /*write a character to INPUT */
fclose(f1); /* close INPUT */
f1=fopen(INPUT, r); /* reopen file */
while((c=getc(f1))!=EOF) /*read character from file INPUT*/
printf(%c, c); /* print character to screen */
fclose(f1);
} /*end main */

C program using getw, putw,fscanf, fprintf


#include <stdio.h>
main()
{ int i,sum1=0;
FILE *f1;
/* open files */
f1 = fopen("int_data.bin","w");
/* write integers to files in binary and
text format*/
for(i=10;i<15;i++) putw(i,f1);
fclose(f1);
f1 = fopen("int_data.bin","r");
while((i=getw(f1))!=EOF)
{ sum1+=i;
printf("binary file: i=%d\n",i);
} /* end while getw */
printf("binary sum=%d,sum1);
fclose(f1);
}

#include <stdio.h>
main()
{ int i, sum2=0;
FILE *f2;
/* open files */
f2 = fopen("int_data.txt","w");
/* write integers to files in binary and
text format*/
for(i=10;i<15;i++) printf(f2,"%d\n",i);
fclose(f2);
f2 = fopen("int_data.txt","r");
while(fscanf(f2,"%d",&i)!=EOF)
{ sum2+=i;
printf("text file: i=%d\n",i);
} /*end while fscanf*/
printf("text sum=%d\n",sum2);
fclose(f2);
}

fprintf() & fscanf


similar

to scanf() and

printf()
Syntax:- fprintf()
fprintf
(fp,"string",variables);
Example:
int i = 12;
float x = 2.356;
char ch = 's';
FILE *fp;
fp=fopen(out.txt,w);
fprintf (fp, "%d %f %c", i,
x, ch);

Syntax:-

fscanf()

fscanf
(fp,"string",identifiers);
Example:
FILE *fp;
Fp=fopen(input.txt,r
);
int i;
fscanf (fp,%d",i);
fscanf() returns EOF
when end-of file reached

getw() & putw()

handle one integer at a time

syntax: putw(i,fp1);

i : an integer variable

fp1 : pointer to file ipened with mode w

syntax: i = getw(fp2);

i : an integer variable

fp2 : pointer to file opened with mode r

file pointer moves by one integer position, data stored


in binary format native to local system

getw() returns
file end reached

end-of-file marker EOF when

fread()

Declaration:- fread(void *ptr, size, n, FILE


*stream);
Remarks:
fread reads a specified number of equal-sized
data items from an input stream into a block.
ptr
= Points to a block into which data is
read
size
= Length of each item read, in bytes
n
= Number of items read
stream = file pointer
Example
char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs;
fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);

fwrite()
Declaration:

fwrite(const void *ptr, size, n, FILE*stream);


Remarks:
fwrite appends a specified number of equal-sized data items to
an output file.
ptr
= Pointer to any object; the data written begins at ptr
size
= Length of each item of data
n
=Number of data items to be appended
stream = file pointer
Example
char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs;
fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);
fclose(fs);

Fseek()

Fseek will position the file pointer to a particular byte


within the file the file.The file pointer is a parameter
maintained by the operating system and determines
where the next read will come from or to where the
next write will go

Ftell() & frewind()

Ftell():Ftell function gives the current position of file


pointer in file

Frewind(): frewind() function moves control to


beginning of file

Program using
fseek,ftell and frewind

#include

Void main()

FILE*fp;

int I;

clrscr();

fp=fopen(CHAR.txt,r);

for(i=1;i&lt;=10;i++);

prinf(%c : %d \n,getc(fp),ftell(fp));

fseek(fp,ftell(fp),0);

if(i==5)

rewind(fp); }

fclose(fp); }

Command line arguments

can give input to C program from command line


E.g. > prog.c 10 name1 name2 .

how to use these arguments?


main ( int argc, char *argv[] )

argc gives a count of number of arguments (including


program name)

char *argv[] defines an array of pointers to character (or


array of strings)

argv[0] program name

argv[1] to argv[argc -1] give the other arguments as


strings

Вам также может понравиться