1 / 17

File I/O

File I/O. Stream. A very important concept in C is the stream a stream is a logical interface to a file A stream is linked to a file using an open operation A stream is disassociated from a file using a close operation. Types of Stream. There are two types of streams

elie
Download Presentation

File I/O

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. File I/O

  2. Stream • A very important concept in C is the stream • a stream is a logical interface to a file • A stream is linked to a file using an open operation • A stream is disassociated from a file using a close operation.

  3. Types of Stream There are two types of streams 1. text2. Binary Text used with ASCII characters some character translation takes place, may not be one-to-one correspondence between stream and what's in the file. Binary used with any type of data, no character translation, one-to-one between stream and file.

  4. Open a File • To open a file and associate it with a stream, use fopen() • FILE *fopen(char *fname,char *mode); • The fopen() function, like all the file-system functions, uses the header stdio.h . The name of the file to open is pointed to by fname (must be a valid name). The string pointed at for mode determines how the file may be accesed. • If fopen() fails it returns a NULL pointer

  5. File Mode • ModeMeaning • r Open a text file for reading • w Create a text file for writing • a Append to a text file • rb Open a binary file for reading • wb Open a binary file for writing • ab Append to a binary file • r+ Open a text file for read/write • w+ Create a text file for read/write • a+ Append or create a text file for read/write • r+b Open a binary file for read/write • w+b Create a binary file for read/write • a+b Append a binary file for read/write

  6. Open/Close a File • FILE *fp; if ((fp = fopen("myfile", "r")) ==NULL) { printf("Error opening file\n"); exit(1); } • intfclose(FILE *fp); The fclose() function returns 0 if successful and EOF (end of file) if an error occurs. • intfgetc(FILE *fp); • intfputc(intch, FILE *fp); • The fgetc() function reads the next byte from the file and returns its as an integer and if error occurs returns EOF. The fgetc() function also returns EOF when the end of file is reached. Your routine can assign fget()'s return value to a char you don't have to assign it to an integer. • The fput() function writes the bytes contained in ch to the file associated with fp as an unsigned char. Although ch is defined as an int, you may call it using simply a char. The fput() function returns the character written if successful or EOF if an error occurs.

  7. Read From File • intfputs(char *str,FILE *fp); • char *fgets(char *str, int num, FILE *fp); • The fputs() function writes the string pointed to by str to the file associated with fp. It returns EOF if an error occurs and a non-negative value if successful. The null that terminates str is not written and it does not automatically append a carriage return/linefeed sequence. • The fget() function reads characters from the file associated with fp into a string pointed to by str until num-1 characters have been read, a new line character is encountered, or the end of the file is reached. The string is null-terminated and the new line character is retained. The function returns str if successful and a null pointer if an error occurs.

  8. Read From Filefread() and fwrite(). • size_tfread(void *buffer, size_t size, size_tnum,FILE *fp); • size_tfwrite(void *buffer, size_t size, size_t num, FILE *fp); • /* Program#file1.c Example of C file handling */ #include /* header file */ #include void main(void) { FILE *fp; /* file pointer */ inti; /* open file for output */ if ((fp = fopen("myfile", "w"))==NULL) { printf("Cannot open file \n"); exit(1); } i=100; if (fwrite(&i, 2, 1, fp) !=1) { printf("Write error occurred"); exit(1); } fclose(fp); /* open file for input */ if ((fp =fopen("myfile", "r"))==NULL) { printf("Read error occurred"); exit(1); } printf("i is %d",i); fclose(fp); }

  9. File System Functions • You can erase a file using remove(). • Its prototype is int remove(char *file-name); • You can position a file's current location to the start of the file using rewind(). • Its prototype is void rewind(FILE *fp); • Hopefully I have given you enough information to at least get you started with files. Its really rather easy once you get started.

  10. Stream in c++ • // basic file operations #include <iostream> #include <fstream> using namespace std; int main () { ofstreammyfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; }

  11. Open/Close in c++ • ios::in Open for input operations. • ios::out Open for output operations. • ios::binary Open in binary mode. • ios::ate Set the initial position at the end of the file.If this flag is not set to any value, the initial position is the beginning of the file. • ios::app All output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations. • ios::trunc If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one. • All these flags can be combined ofstreammyfile; myfile.open ("example.bin", ios::out | ios::app | ios::binary); if (myfile.is_open()) { /* ok, proceed with output */ } myfile.close();

  12. Data Types and FilesWriting to a new file. #include <fstream.h> #include <iostream.h> #include <string..h> using namespace std; Int main() { char ch = ‘x’; int j = 77; double d=6.02; string str1 = “Allama”; // Without Spaces string str2 = “Iqbal”; ofstream outfile(“fdata.text”); outfile << ch << j << ‘ ‘ //need space between numbers << d << str1 << ‘ ‘ //need space b/w strings << str2; cout << “File written \n”; return 0; }

  13. Data Type and FileReading from File #include <fstream.h> #include <iostream.h> #include <string..h> using namespace std; Int main() { char ch; int j; double d; string str1; string str2; ifstreaminfile(“fdata.text”); infile >> ch >> j >> d >> str1 >> str2; cout << ch <<endl << j <<endl << d <<endl << str1 << endl << str2 << endl; return 0; }

  14. Writing a Text File // writing on a text file #include <iostream> #include <fstream> using namespace std; int main () { ofstreammyfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile << "This is another line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0; }

  15. Reading a Text File // reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstreammyfile ("example.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; }

  16. Determine Size of File // obtaining file size #include <iostream> #include <fstream> using namespace std; int main () { long begin,end; ifstreammyfile ("example.txt"); begin = myfile.tellg(); myfile.seekg (0, ios::end); end = myfile.tellg(); myfile.close(); cout << "size is: " << (end-begin) << " bytes.\n"; return 0; }

  17. Reading in Memory // reading a complete binary file #include <iostream> #include <fstream> using namespace std; ifstream::pos_type size; char * memblock; int main () { ifstream file ("example.txt", ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); memblock = new char [size]; file.seekg (0, ios::beg); file.read (memblock, size); file.close(); cout << "the complete file content is in memory"; delete[] memblock; } else cout << "Unable to open file"; return 0; }

More Related