1 / 16

Text Files

Text Files. Files. Files are used to store data permanently on secondary memory (floppy disk, hard disk, …). There are two types of files: text files (AKA ascii files and sequential files) Data of voarious types is usually stored as a sequence of characters

yadid
Download Presentation

Text Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Text Files

  2. Files • Files are used to store data permanently on secondary memory (floppy disk, hard disk, …). • There are two types of files: text files (AKA ascii files and sequential files) Data of voarious types is usually stored as a sequence of characters the file is treated as a sequence of characters or as a sequence of values. Data access is usually processed sequentially. binary files (AKA random access files) the file is treated as a sequence of bytes representing the data. char values are stored in 1 byte, float values in 4 bytes, … Elements in the file are usually homogenous Data is usually accessed randomly: a location can be accessed directly without accessing preceding bytes in the file.

  3. Creating Text Files Text files can be created and opened using: • a text editor (note pad, Borland C++, visual C++) • a C program

  4. Example: creating a simple text file. #include <stdio.h> void main(){ FILE * fptr=NULL; fptr=fopen(“c:\\students.dat”,”w”); if (fptr==NULL) cout<<”file could not be opened”; else{ fprintf(fptr, “%s %d\n”, “ali”, 70); fprintf(fptr, “%s %d”, “ahmad”, 80); fclose(fptr); }//else }//main

  5. Example: reading a text file #include <stdio.h> #include <iostream.h> void main(){ char name[20]; int score; FILE * fptr=NULL; fptr=fopen(“students.dat”, ”r”); if (fptr==NULL) cout<<”file could not be opened”; else{ fscanf(fptr, “%s %d”, name, &score); cout<<name<<” ”<<score; fscanf(fptr, “%s %d”, name, &score); cout<<name<<” ”<<score; fclose(fptr); }//else }//main

  6. Example: reading data from KB into a text file. #include <stdio.h> #include <iostream.h> void main(){ char name[20]; int i; int score; FILE * fptr=NULL; fptr=fopen(“students.dat”, ”w”); if (fptr==NULL) cout<<”file could not be opened”; else{ for(i=1;i<=100;i++){ cin<<name<<score; fprintf(fptr, “%s %d\n”, name, score); }//for fclose(fptr); }//else }//main

  7. Example: printing data from text file into screen version 1. #include <stdio.h> #include <iostream.h> void main(){ char name[20]; int i; int score; FILE * fptr=NULL; fptr=fopen(“students.dat”, ”r”); if (fptr==NULL) cout<<”file could not be opened”; else{ for(i=1;i<=100;i++){ fscanf(fptr, “%s %d”, name, &score); cout<<name<<” ”<<score<<endl; }//for fclose(fptr); }//else }//main

  8. Example: printing data from text file into screen version 2. #include <stdio.h> #include <iostream.h> void main(){ char name[20]; int i; int score; FILE * fptr=NULL; fptr=fopen(“students.dat”, ”r”); if (fptr==NULL) cout<<”file could not be opened”; else{ fscanf(fptr, “%s %d”, name, &score); while(!feof(fptr)){ cout<<name<<” ”<<score<<endl; fscanf(fptr, “%s %d”, name, &score); }//while fclose(fptr); }//else }//main

  9. Exercize Write a program to find the student with the maximum score in the file.

  10. Exercize Write a program to increase the sores in the file by 5. You should copy the data into an array, update the array, and then write it back to the file. Divide this program into functions as follows: int fileSize(char* filename){…}//returns number of students void read(char* filename, student *A, int size){…} void update(student *A, int size){…} void write(char* filename, student *A, int size){…} void main(){ int sz=fileSize(“students.dat”); student * G=new student[sz]; read(“students.dat”,G,sz); update(G,sz); write(“students.dat”,G,sz); }

  11. Exercize Write a function to delete the contents of a file given its name.

  12. rewind Statement • The rewind statement is used to reset the file position to the beginning of the file. • Example: a program that gives scores for students given their names. This program keeps running until the user enters zzz. • Example: read a student name and display his rank.

  13. Text Files as Sequence of Characters • write a function to print a text file given its name on the screen. • write a function to return the number of digits in a text file given its name • write a function to count the number of lines in a text file • write a function to copy a text file into another file. • write a function to delete all space characters from a text file. • write a function to delete extra spaces from a text file. • write functions to encrypt and decrypt a file by incrementing characters by 1.

  14. EOF Character • Unix: <return><ctrl>d • Dos: <ctrl>z • Macintosh: <ctrl>d • Vax vms: <ctrl>z

  15. stdio.h Functions • int fgetc(FILE* stream); returns the next character on the named input stream. On the end-of-file or error it returns EOF. • int fputc(int c, FILE*stream); prints character c to the named screen. On success returns c. on error returns EOF.

  16. stdio.h Functions • char *fgets(char *s, int t, FILE* stream); gets a string from a stream into s. The function stops reading when it reads either n-1 characters or a new line character whichever comes first. fgets retains the new line character at the end of the string and a NULL character is also added. On success returns s and on fail or end-of-file returns NULL. • int fputs(cons char* s, FILE* stream); copies s to the file. It does not append new line character. NULL character is not copied.

More Related