从文本文件中读内容(显示到控制台界面上)
来源:互联网 发布:动易网络注册 编辑:程序博客网 时间:2024/05/06 08:03
优化前的代码:
#include <fstream>#include <iostream>#include <string>int main(){using namespace std;// ifstream is used for reading files// We'll read from a file called Sample.txt//char str[256];//cout << "Enter the name of an existing wokflowlog file: " << endl;//cin.get (str,256);//ifstream inf.open(str);ifstream inf("Sample.txt");// If we couldn't open the output file stream for readingif (!inf){// Print an error and exitcerr << "Uh oh, Sample.txt could not be opened for reading!" << endl;exit(1);}// While there's still stuff left to readwhile (inf){// read stuff from the file into a string and print itstring strInput;inf >> strInput;cout << strInput << endl;}return 0;// When inf goes out of scope, the ifstream// destructor will close the file}
输出结果如下图:
优化后的代码:
#include <fstream>#include <iostream>#include <string>int main(){using namespace std;// ifstream is used for reading files// We'll read from a file called Sample.txtifstream inf("Sample.txt");// If we couldn't open the input file stream for readingif (!inf){// Print an error and exitcerr << "Uh oh, Sample.txt could not be opened for reading!" << endl;exit(1);}// While there's still stuff left to readwhile (inf){// read stuff from the file into a string and print itstd::string strInput;getline(inf, strInput);cout << strInput << endl;}return 0;// When inf goes out of scope, the ifstream// destructor will close the file}输出结果如下图:
- 从文本文件中读内容(显示到控制台界面上)
- 将硬盘上的文本文件的内容输出到控制台
- Android 把sd卡文本文件的内容显示到界面
- 将MsFlexGrid控件中显示的内容输出到文本文件
- 将MsFlexGrid控件中显示的内容输出到文本文件
- 从键盘输入若干个字符,逐个存到磁盘文件中,直到输入‘\n’为止,并将建立的文本文件的内容在存入文件的同时,在屏幕上显示出来。。
- 从键盘输入若干个字符,逐个存到磁盘文件data.txt中,直到输入’\n’为止,并将建立的文本文件的内容在存入文件的同时,在屏幕上显示出来:
- 从文件中读数据并显示到控制台
- iOS学习笔记21—从文本文件中读取内容到字符串里
- iOS开发代码:从文本文件中读取内容到字符串里
- iOS开发代码:从文本文件中读取内容到字符串里(+ .json文件)
- java追加内容到文本文件中
- 从AVI中读取每帧数据显示到界面
- 从Oracle中导出数据到文本文件
- 在控制台程序中不显示控制台界面
- Linux显示文本文件内容(cat, less)
- 从数据库中拿数据,显示到listview上
- iOS中如何从应用中跳转到系统的设置界面中的内容
- 【Android】UI界面外的线程,控制刷新UI界面
- Linux批量删除文件
- ios如何实现推送通知
- xcode4中添加多国语言
- VS2010 使用TeeChart绘图控件 - 之一 - 控件和类的导入
- 从文本文件中读内容(显示到控制台界面上)
- LOWORD/HIWORD宏与GET_X_LPARAM/GET_Y_LPARAM宏的注意点
- 使用url访问servlet(一)
- tabview 滚动控制
- YAFFS文件系统简介
- 读取Gmail邮件
- Apio07-Backup
- Java序列化与反序列化【实例】
- Web缓存技术相关简析