File Read Write
2012-11-28 15:00
302 查看
Reads information from the file and outputs it onto the screen
Opening Files for Read and Write
Use get() and getline() to read characters.
#include <fstream> #include <iostream> using namespace std; int main () { char data[80]; ofstream outfile; outfile.open("file.dat"); cout << "Writing to the file" << endl; cout << "Enter your name: "; cin.getline(data, 80); outfile << data << endl; cout << "Enter your id: "; cin >> data; cin.ignore(); outfile << data << endl; outfile.close(); ifstream infile; cout << "Reading from the file" << endl; infile.open("file.dat"); infile >> data; cout << data << endl; infile >> data; cout << data << endl; infile.close(); return 0; }Another example of read() and write() and illustrates the use of gcount( )
#include <iostream> #include <fstream> using namespace std; int main() { double doubleNumberArray[4] = {99.75, -34.4, 1776.0, 200.1}; int i; ofstream out("numbers", ios::out | ios::binary); if(!out) { cout << "Cannot open file."; return 1; } out.write((char *) &doubleNumberArray, sizeof doubleNumberArray); out.close(); for(i=0; i<4; i++) // clear array doubleNumberArray[i] = 0.0; ifstream in("numbers", ios::in | ios::binary); in.read((char *) &doubleNumberArray, sizeof doubleNumberArray); cout << in.gcount() << " bytes read\n"; // see how many bytes have been read for(i=0; i<4; i++) // show values read from file cout << doubleNumberArray[i] << " "; in.close(); return 0; }
Opening Files for Read and Write
#include <fstream> #include <iostream> using namespace std; int main() { char buffer[255]; ofstream fout("text.txt"); fout << "This line written directly to the file...\n"; cout << "Enter text for the file: "; cin.ignore(1,'\n'); cin.getline(buffer,255); fout << buffer << "\n"; fout.close(); ifstream fin("text.txt"); char ch; while (fin.get(ch)) cout << ch; fin.close(); return 0; }
Use get() and getline() to read characters.
#include <iostream> #include <fstream> using namespace std; int main() { char ch; char str[256]; ofstream fout("test.dat"); if(!fout) { cout << "Cannot open file for output.\n"; return 1; } fout << "This is a line of text.\n"; fout << "This is another line of text.\n"; fout << "This is the last line of text.\n"; fout.close(); if(!fout.good()) { cout << "An error occurred when writing to the file.\n"; return 1; } ifstream fin("test.dat", ios::in); if(!fin) { cout << "Cannot open file for input.\n"; return 1; } cout << "Use get():\n"; cout << "Here are the first three characters: "; for(int i=0; i < 3; ++i) { fin.get(ch); cout << ch; } cout << endl; fin.get(str, 255); cout << "Here is the rest of the first line: "; cout << str << endl; fin.get(ch); cout << "\nNow use getline():\n"; fin.getline(str, 255); cout << str << endl; fin.getline(str, 255); cout << str; fin.close(); if(!fin.good()) { cout << "Error occurred while reading or closing the file.\n"; return 1; } return 0; }
相关文章推荐
- oUpFileStream.Write Request.BinaryRead (Request.TotalBytes)上传提示错误 0104
- db file parallel write和db file parallel read
- read\write in C++ from file
- Qt Read and Write Csv File
- nodejs read/write file
- Win32 WriteFile and ReadFile
- oUpFileStream.Write Request.BinaryRead (Request.TotalBytes)上传提示错误 0104
- Build Network Dataset failed. File read/write error occurred
- How to open/read/write a local file from an applet
- Database 'xxx' cannot be upgraded because it is read-only or has read-only file Make the database or files writeable, and rerun recovery.
- python.pandas read and write CSV file
- read and write excel file as a table
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet
- C++ file read write
- Read and Write a /proc File
- IniFile Read Write
- [Win32]ReadFile/WriteFile 的文件同步读写
- <Win32 API> 文件操作CreateFile/ReadFile/WriteFile
- C++ - Read / Write std::map to a file
- CreateFile ReadFile WriteFile 详解