Files: File Operations Function Used in Writing To A File
Files: File Operations Function Used in Writing To A File
bit(binary digit)-smallest data item in a computercharactersfieldsrecordsfilesdatabase To facilitate the retrieval of specific records from a file at least one field in a record is chosen. This field must be unique from the others called record key/primary key C views each file as a sequential stream of bytes. Each file ends with an end of file(EOF) marker. When a file is opened, a stream is associated with the file. Streams provide communication channels between files and programs. Three files and their associated streams are automatically opened when program execution beginsstandard input, standard output, and the standard error. Opening a file returns a pointer to a FILE structure that contains information used to process the file. The standard input, standard output and standard error are manipulated using the pointers stdin, stdout, and stderr. FILE OPERATIONS Function used in writing to a file: char c; putc(c, filepointer); fputc(c, filepointer); char *s; fputs(s, filepointer); //fputs copies a null terminated string to an output stream. It does not append a newline character and the terminating null character is not copied. char *s; fwrite(s,size,n,filepointer);// spointer to an object, the data written begins here sizelength of each item of data nnumber of data items to be written int number; //fprintf(fp,format, arguments); fprintf(fp, %d, number); Reading from files char c; c = getc(filepointer); c = fgetc(filepointer); ungetc(c,filepointer); char s[80]; int n; fgets(s, n, filepointer);//retains a newline character at the end of s and appends a null byte to s to mark the end of the string. char s[80]; int n; fread(s, size, n, filepointer);//Get n number of elements each of size size from a file and store it to s. fscanf(filepointer, format, &arguments..);
SAMPLE PROGRAMS A) #include<stdio.h> #include<conio.h> main(){ FILE *fp; char s[5]={'A','B','C','D','E'}; int i; char c; clrscr(); if((fp=fopen("C:\\text.txt","w"))!=NULL){ fputs(s,fp); rewind(fp); fseek(fp,-4,SEEK_END); i=ftell(fp); //for (i=0;i<5;i++) c = getc(fp); printf("%c",c); }else{ printf("file not created"); } fclose(fp); getch(); } B)Creating and writing a sequential file #include<stdio.h> main(){ int account; char name[30]; double balance; FILE *cfPtr; /*cfPtr=clients.dat file pointer*/ if((cfPtr = fopen(clients.dat, w)) == NULL) printf(File counld not be opened\n); else{ printf(Enter the account, name, and balance.\n); printf(Enter EOF to end input.\n); printf(?); scanf(%d,%s,%1f, &account, name, &balance); while (!feof(stdin)){ fprintf(cfPtr, %d, %s, %.2f\n, account, name, balance); printf(?); scanf(%d%s%lf, &account, name, &balance); } fclose(cfPtr); } }
Enter the account, name, and balance. Enter EOF to end input. ? 100 Jones 24.98 ? 200 Doe 345.67 ? 300 White 0.00 ? 400 Stone -42.16 ? 500 Rich 224.62 ?
Program B creates a simple sequential-access file that might be used in an accounts receivable system to help keep track of the amounts owed by a companys credit clients. For each client, the program obtains an account number, clients name, and the clients balance. It will continuously asks the user to input data until an end-of-file indicator is entered. Here are some key combinations for different systems to indicate an eof: Computer System Key Combination Unix systems <return><control>d IBM PC and compatibles <ctrl>z Macintosh <ctrl>d C) Reading and printing data from a sequential-access file #include<stdio.h> main(){ int account; char name[30]; double balance; FILE *cfPtr; /*cfPtr = clients.dat file pointer*/
if((cfPtr = fopen(clients.dat, r)) == NULL) printf(File could not be opened\n); else{ printf(%-10s%-13s%s\n, Account, Name, Balance); fscanf(cfPtr, %d%s%lf, &account, name, &balance);
while(!feof(cfPtr)){ printf(%-d%-13s%7.2f\n, account, name, balance); fscanf(cfPtr, %d%s%lf, &account, name, &balance); } fclose(cfPtr); } } To retrieve data sequentially from a file, a program normally starts reading from the beginning of the file and reads all data consecutively until the desired data are found. D) Credit inquiry program #include<stdio.h> main(){ int request, account; double balance; char name[30]; FILE *cfPtr; if((cfPtr = fopen(clients.dat, r)) == NULL) printf(File could not be opened\n); else{ printf(Enter request \n 1-List accounts with zero balances\n 2-List accounts with credit balances\n 3-List accounts with debit balances\n 4-End of run\n?); scanf(%d, &request); while(request != 4){ fscanf(cfPtr, %d%s%lf, &account, name, &balance);
switch(request!=4){ case 1: printf(\nAccounts with zero balances:\n); while(!feof(cfPtr)){ if(balance == 0) printf(%-10d%-13s%7.2f\n, account, name, balance); fscanf(cfPtr, %d%s%lf, &account, name, &balance); } break; case 2: printf(\nAccounts with credit balances:\n); while(!feof(cfPtr)){ if(balance < 0) printf(%-10d%-13s%7.2f\n, account, name, balance); fscanf(cfPtr, %d%s%lf, &account, name, &balance); } break; case 3: printf(\nAccounts with debit balances:\n); while(!feof(cfPtr)){ if(balance > 0) printf(%-10d%-13s%7.2f\n, account, name, balance); fscanf(cfPtr, %d%s%lf, &account, name, &balance); } break; } rewind(cfPtr); printf(\n?); scanf(%d, &request); } printf(End of run.\n); fclose(cfPtr); } return 0; } Enter request 1-List accounts with zero balances 2-List accounts with credit balances 3-List accounts with debit balances 4-End of run ?1 Accounts with zero balances: 300 White 0.00 ?2 Accounts with credit balances: 400 Stone -42.16 ?3 Accounts with debit balances: 100 Jones 24.98 200 Doe 345.67 500 Rich 224.62 ?4 End of run.
In program C, it may be desirable to process the data sequentially in a file several times (from the beginning of the file) during the execution of a program. A statement such as rewind(cfPtr); causes a programs file position pointer-indicating the number of the next byte in the file to be read or written-to be repositioned at the beginning of the file( byte 0) pointed to by cfPtr. The file position pointer is not really a pointer. Rather it is an integer value that specifies the byte location in the file at which the next read or write is to occur. This is sometimes referred to as the file offset. The file position pointer is a member of the FILE structure associated with each file.