C++ Tutorial/File Stream/ifstream — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 13:31, 25 мая 2010
Содержание
- 1 Display a file from a given starting point
- 2 Display a file using ifstream.get()
- 3 ifstream: read buffer and seek file position
- 4 Ignore up to 10 characters or until first space is found
- 5 Read and display a text file line by line.
- 6 12. 8. 5. Reading a text file
- 7 Reading numbers from a file
- 8 Show a file from starting offset
- 9 Use ifstream.read() and ofstream.write()
Display a file from a given starting point
<source lang="cpp">#include <iostream>
- include <fstream>
- include <cstdlib>
using namespace std;
int main(int argc, char *argv[]) {
char ch; ifstream in("test.txt", ios::in | ios::binary); if(!in) { cout << "Cannot open file.\n"; return 1; } in.seekg(1, ios::beg); while(in.get(ch)) cout << ch; return 0;
}</source>
0 123.23 This is a text.
Display a file using ifstream.get()
<source lang="cpp">#include <iostream>
- include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
char ch; ifstream in("test", ios::in | ios::binary); if(!in) { cout << "Cannot open file.\n"; return 1; } while(in) { // in will be false when eof is reached in.get(ch); if(in) cout << ch; } in.close(); return 0;
}</source>
hello
ifstream: read buffer and seek file position
<source lang="cpp">/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
// header files for file I/O
- include <iostream>
- include <fstream>
void printFileTwice (const char* filename) {
// open file std::ifstream file(filename); // print contents the first time std::cout << file.rdbuf(); // seek to the beginning file.seekg(0); // print contents the second time std::cout << file.rdbuf();
} int main (int argc, char* argv[]) {
// print all files passed as a command-line argument twice for (int i=1; i<argc; ++i) { printFileTwice(argv[i]); }
}</source>
Ignore up to 10 characters or until first space is found
<source lang="cpp">#include <iostream>
- include <fstream>
using namespace std; int main() {
ifstream in("test"); if(!in) { cout << "Cannot open file.\n"; return 1; } in.ignore(10, " "); char c; while(in) { in.get(c); if(in) cout << c; } in.close(); return 0;
}</source>
Read and display a text file line by line.
<source lang="cpp">#include <iostream>
- include <fstream>
using namespace std; int main(int argc, char *argv[]) {
ifstream in("test.txt"); if(!in) { cout << "Cannot open input file.\n"; return 1; } char str[255]; while(in) { in.getline(str, 255); // delim defaults to "\n" if(in) cout << str << endl; } in.close(); return 0;
}</source>
R 9.9 T 9.9 M 4.8
12. 8. 5. Reading a text file
<source lang="cpp">
- include <iostream.h>
- include <fstream.h>
- include <stdlib.h>
int main () {
char buffer[256]; ifstream examplefile ("test.txt"); if (! examplefile.is_open()) { cout << "Error opening file"; exit (1); } while (! examplefile.eof() ) { examplefile.getline (buffer,100); cout << buffer << endl; } return 0;
}</source>
This is a line. This is another line.
Reading numbers from a file
<source lang="cpp">#include <fstream>
- include <iostream>
- include <iomanip>
using std::cout; using std::endl;
int main() {
const char* filename = "test.txt"; std::ifstream inFile(filename); // Make sure the file stream is good if(!inFile) { cout << endl << "Failed to open file " << filename; return 1; } long n = 0; while(!inFile.eof()) { inFile >> n; cout << std::setw(10) << n; } cout << endl; return 0;
}</source>
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
Show a file from starting offset
<source lang="cpp">#include <iostream>
- include <fstream>
- include <cstdlib>
using namespace std; int main(int argc, char *argv[]) {
char ch; ifstream in("test.txt", ios::in | ios::binary); if(!in) { cout << "Cannot open file."; return 1; } in.seekg(2, ios::beg); while(in.get(ch)) cout << ch; return 0;
}</source>
9.9 T 9.9a M 4.8
Use ifstream.read() and ofstream.write()
<source lang="cpp">#include <iostream>
- include <fstream>
using namespace std;
int main() {
int n[5] = {1, 2, 3, 4, 5}; register int i; ofstream out("test", ios::out | ios::binary); if(!out) { cout << "Cannot open file.\n"; return 1; } out.write((char *) &n, sizeof n); out.close(); for(i=0; i<5; i++) // clear array n[i] = 0; ifstream in("test", ios::in | ios::binary); if(!in) { cout << "Cannot open file.\n"; return 1; } in.read((char *) &n, sizeof n); for(i=0; i<5; i++) // show values read from file cout << n[i] << " "; in.close(); return 0;
}</source>
1 2 3 4 5