C++文件流操作,从文件中读取数据并显示
2013-04-17 18:24
477 查看
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("get.txt");
if(!inFile.is_open()){
cout << "Can't open the File" << endl;
exit(0);
}
int age[7];
int count = 0;
string name[7];
string s_name;
while(!inFile.eof()){
getline(inFile,s_name);
if(s_name[0] == '\0'){
cout << "读取到换行符" << endl;
}
cout<<"Read one line:"<<s_name<<endl;
inFile >> age[count];
inFile >> name[count];
++count;
}
for(count = 0 ; count < 7 ; count ++){
cout << age[count] << endl;
cout << name[count] << endl;
}
inFile.close();
}
//代码分析
//红色部分为为了区分getline函数与cin的区别,而加的额外的代码
//由于get.txt文件中有多行,所以我定义了数组形式
首先引入fstream头文件
其次定义ifstream变量(注意和ofstream的区别)
然后便可以像使用cin一样使用ifstream所定义的变量
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("get.txt");
if(!inFile.is_open()){
cout << "Can't open the File" << endl;
exit(0);
}
int age[7];
int count = 0;
string name[7];
string s_name;
while(!inFile.eof()){
getline(inFile,s_name);
if(s_name[0] == '\0'){
cout << "读取到换行符" << endl;
}
cout<<"Read one line:"<<s_name<<endl;
inFile >> age[count];
inFile >> name[count];
++count;
}
for(count = 0 ; count < 7 ; count ++){
cout << age[count] << endl;
cout << name[count] << endl;
}
inFile.close();
}
//代码分析
//红色部分为为了区分getline函数与cin的区别,而加的额外的代码
//由于get.txt文件中有多行,所以我定义了数组形式
首先引入fstream头文件
其次定义ifstream变量(注意和ofstream的区别)
然后便可以像使用cin一样使用ifstream所定义的变量
相关文章推荐
- 从ApplicationResources.properties中读取数据对文件进行操作.java
- 利用COM组件IPicture读取jpg、gif、bmp图片文件数据和显示图片的两个函数
- 从数据文件中读取一段数据,显示到ImageView中
- VC6.0中使用Stream Object读取数据中流文件并显示Bmp,JPG等图片
- Python文件操作:从文本末尾逆序读取数据
- 复制文件时,如何显示进度条(使用BlockRead函数读取数据,并插入application.ProcessMessages)
- 文件的读写(I/O)操作总结(二)——读取Assets中的文件数据
- 读取并显示dicom文件的图像数据和覆盖层数据
- .NET WinForm读取扫描枪从COM口传过来的数据,显示在TextBox中,并做其他操作。
- MATLAB 对Ply文件进行 读取、显示、连线操作
- 读取Excel文件中的数据,并显示在MSFlexGrid表格中
- 读取excel文件显示在gridview中数据.字符.数字混合时无法全部读取的问题!
- PHP 操作 XML 文件 - 读取、显示
- Ajax的一个简单例子--从一个文件文件中读取数据显示到页面中
- 什么是沙盒模型?怎么写入、读取沙盒路径文件数据,哪些操作是属于私有api范畴
- php excel 读取日期问题 在 php excel 读取 xls 格式的文件时,xls 上面显示的是正常的日期格式 但是读取出来的话,就会是一个万位整形数据,这显然不是我们想要的日
- 文件操作ofstream,open,close,ifstream,fin,按照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件
- Python: 文件操作与数据读取
- 第4章 处理用户输入与显示数据------------(默读与文件读取)
- .NET WinForm读取扫描枪从COM口传过来的数据,显示在TextBox中,并做其他操作。