sahajjain01

File handling notes

Jul 17th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. Data file handling
  2.  
  3. File:-
  4. -A file is stream of bytes stored on some secondary storage device.
  5. -Files are of 2 types:
  6. Text file- A text file stores information in readable and printable form. Each line of text is terminated with an EOL (End of line) character.
  7. Binary File- A binary file conatins information in the same format iun which it is held in a memory.
  8.  
  9. File stream:-
  10. -Stream is a general term to used to state flow of data.
  11. -ifstream: Used for read operations
  12. -ofstream: Used for write operations
  13. -fstream: Used for both read and write operations
  14. -fstream.h: This header file includes definations for the stream classes ifstream, ofstream and fstream. It also contains pre-defined set of operations for input output.
  15. -A file stored in secondary storage can be opened by 2 methods: 1-By constructor method-This will use default streamns for file input or output. This method is preffered when file is opened in input or output mode only, e.g- ofstream file("student.dat"); or ifstream file("student.dat");
  16. -By the open() member function of the stream. It is prefferd when file is opened in various modes i.e ios::in, ios::out, ios::append, ios::ate. E.g fstream.open("book.dat",ios::in|ios::out|ios::binary);
  17.  
  18. File Modes:- (Read from book)
  19. -out mode
  20. -in mode
  21. -append mode
  22. -truncate mode
  23. -no create mode
  24. -no replace mode
  25. -binary mode
  26.  
  27. File Functions:-
  28. -eof() function: This function dertermines the end of files by returning 'true'
  29. -close(): This functions termantes the connection between the file and stream associated e.g file.close();
  30.  
  31. Text-File Functions:-
  32. -get(): It reads a single charcter from text file and stores it in a buffer. E.g file.get(ch);
  33. -put(): It writes a single character in text file. E.g file.put(ch);
  34. -getline(): It reads a line of text from text-file. E.g file.getline(ch,80); We can also use 'file>>ch' for reading and 'file<<ch' for writing but this operator does not accept white spaces.
Advertisement
Add Comment
Please, Sign In to add comment