Creating a sequential file

来源:互联网 发布:网址导航 源码 编辑:程序博客网 时间:2024/05/04 23:44

#include<stdio.h>

int main()

{

(Each open file must have a separately declared pointer of type FILE that is used to refer to the file.)

File *cfPtr; //cfPtr = clients.dat file pointer //cfptr is a pointer to a FILE structure

//fopen opens file. Exit program if unable to create file.

(fopen takes two arguments: a file name and a file open mode.)

(the file open mode "w" : the file is to be opened for writing . If a file does not exist ,fopen creates the file.If an existing file is opened for writing , the contents of the file are discarded without warning.)

if( ( cfPtr = fopen( "clients.dat","w") ) == NULL ){

 printf("File could not be opened/n");} //end if

else{...}

//write information into file with fprintf

(feof to determine whether the end-of -file indicator is set for the file to which stdin refers.

(Windows  <ctrl>z   ; UNIX <return><ctrl> d   end-of-file key combination)

The argument of function feof is a pointer to the file being tested for the end-of-file indicator .The function returns a nonzero(true) value when the end-of-file indicator has beenhas been set; otherwise , the function returns zero.)

while( ! feof(stdin) ) {

(Function fprintf is equivalent to printf except that fprintf also receives as an argument a file pointer for the file to which the data will be written.)

fprintf(cfptr,"%d %s %.2f/n", account , name , balance);

scanf("%d%s%lf", &account , name , &balance);}

( ( fclose receives the file pointer (rather than the file name) as an argument.) If funcion fclose is not called explicitly, the operating system normally will close the file when program execution terminates.)

fclose(cfPtr); // indicates successful termination

return 0 ;

}

原创粉丝点击