Các tham s ố cho hàm read và write là đị a

This preview shows page 184 - 191 out of 194 pages.

Các tham s cho hàm read() và write() là đị a ch c a b đệ m d li u và kích th ướ c c a chúng. Đị a ch ph i ép thành ki u char và kích th ướ c đượ c tính b ng byte.
Image of page 184
185 Vào/Ra đố i t ượ ng (objects I/O): dùng ch ế độ m file nh phân, và các hàm read(), write(). Con tr file : m i đố i t ượ ng file k ế t h p v i nó 2 giá tr nguyên g i là get pointer ( con tr đọ c ) và put pointer ( con tr ghi ). Hai giá tr này cho bi ế t v trí đọ c và v trí ghi hi n t i c a con tr trong quá trình làm vi c v i file. Để đ i u khi n con tr file ph c v cho vi c đọ c/ghi file t i v trí tùy ý, ta dùng hàm seekg(), seekp() ( để di chuy n con tr file đế n v trí mong mu n), tellg(), tellp() ( để đọ c v trí hi n hành c a con tr file). Hàm seekg() và tellg() cho phép thi ế t l p và ki m tra con tr đọ c, còn các hàm seekp() và tellp() th c hi n t ươ ng t đố i v i con tr ghi. Hàm seekg() và seekp() có 2 tham s : tham s th 1 bi u di n độ l ch (offset), tính t v trí c th xác đị nh b i tham s th 2 (ios::beg, ios::cur, ios:end) 2.3. Đ óng File Dùng hàm close() c a đố i t ượ ng file. 2.4. Ví d Vào/Ra File đị nh d ng Hàm ghi file s nguyên void WriteIntFile() { ofstream outf( “test.dat”, ios::out ); if ( ! outf ) { cerr << “Cannot open file test.dat . . .\n”; exit( 1 );
Image of page 185
186 } for ( int i = 0; i < 100; i++ ) outf << i + 1 << ‘ ‘; outf.close(); } Hàm đọ c file s nguyên void ReadIntFile() { ifstream inf( “test.dat”, ios::in ); if ( ! inf ) { cerr << “Cannot open file test.dat . . .\n”; exit( 1 ); } int i; while ( ! inf.eof() ) { inf >> i; cout << setw( 4 ) << i; } inf.close();
Image of page 186
187 } Hàm đọ c/ghi file s nguyên void IntFile() { fstream file; file.open( “test.dat”, ios::out | ios::in ); if ( ! file ) { cerr << “Cannot open file test.dat . . .\n”; exit( 1 ); } for ( int i = 0; i < 100; i++ ) outf << i + 1 << ‘ ‘; file.seekg( 0, ios::beg ); // ve dau file while ( ! file.eof() ) { inf >> i; cout << setw( 4 ) << i; } file.close(); }
Image of page 187
188 2.5. Ví d Vào/Ra kí t Hàm ghi kí t ra file void ReadCharFile() { ofstream outf( “test.txt”, ios::out ); if ( ! outf ) { cerr << “Cannot open file test.txt …\n”; exit( 1 ); } char str[] = “KHOA TIN HOC”; int i = 0; while ( s[ i ] ) { outf.put( s[ i ] ); // outf << s[ i ] i++; } outf.close(); } Hàm đọ c kí t t file void ReadCharFile() {
Image of page 188
189 ifstream inf( “test.txt”, ios::in ); if ( ! inf ) { cerr << “Cannot open file test.txt . . .\n”; exit( 1 ); } char ch; while ( ! inf.eof() ) { inf.get( ch ); cout << ch; } inf.close(); } 2.6. Ví d Vào/Ra File nh phân #include <iostream.h> #include <fstream.h> const int MAX = 100; void main() { int buffer[ MAX ] = { 0 };
Image of page 189
190 for ( int i = 0; i < MAX; i++ ) buffer[ i ] = i; ofstream outf( “test.dat”, ios::binary ); if ( ! outf ) { cout << “Cannot open file test.dat . . .\n”; exit( 1 ); } outf.write( ( char* ) buffer, MAX * sizeof( int ) ); for ( i = 0; i < MAX; i++ ) buffer[ i ] = 0; ifstream inf( “test.dat”, ios::binary ); if ( ! inf ) { cout << “Cannot open file test.dat . . .\n”; exit( 1 ); } inf.read( ( char* ) buffer, MAX * sizeof( int ) ); for ( i = 0; i < MAX; i++ )
Image of page 190

Want to read all 194 pages?

Image of page 191

Want to read all 194 pages?

You've reached the end of your free preview.

Want to read all 194 pages?

  • Fall '17
  • C++, C Plus Plus, Programming

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern