Use eof() to read and display a text file.
2012-11-28 14:45
459 查看
#include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv[]) { char ch; ifstream fin("text.txt"); if(!fin) { cout << "Cannot open file.\n"; return 1; }/** while(!fin.eof()) { char bufline[256]; fin.getline(bufline, 100); } */
do { fin.get(ch); if(!fin.eof() && (fin.fail() || fin.bad())) { cout << "Input Error\n"; fin.close(); return 1; } if(!fin.eof()) cout << ch; } while(!fin.eof()); fin.clear(); fin.close(); if(!fin.good()) { cout << "Error closing file."; return 1; } return 0; }
相关文章推荐
- QT27 how to read text file and display file to a textbrowser or textedit
- Use eof() to read and display a text file.
- Java源码——读取顺序存取文件中的数据(read text file and display each record)
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet
- Configure file like format of ini and read data from ini file or write data to ini file
- Use the SQL Server CLR to Read and Write Text Files
- Create, Read, Write, Copy, Move and Delete a Text File using C# and VB.NET
- [Shell]How to display text on specific line of text file
- Create, Read, Write, Copy, Move and Delete a Text File using C#
- How to Include ASM File and to Highlight Display in Source Insight
- Create, Read, Write, Copy, Move and Delete a Text File using C#
- ffmpeg示例一:how to use libavformat and libavcodec to read video from a file.
- How to mount partition with ntfs file system and read write access
- How to read and parse CSV file in Java
- how to display value and text in query(query显示值和文本)
- ReadCompareExcelToDictionary (Resource file export and import)
- Read and Write NSArray, NSDictionary and NSSet to a File
- [ERROR] Unable to read and import the source file : 'C:\Users\Administrator\.hudson\jobs\Deeplan\wor
- How to read a text file attached to a Lotus Notes Document WITHOUT detaching it?
- open and read a file content to a variable