您的位置:首页 > 其它

fstream 输入的基本用法(供个人备忘)

2012-05-27 14:45 253 查看
简述:从map.txt读取一幅地图信息,然后输出

内容:

1)fstream读取文件

2)getline(fin,strLine)读取文件,并且赋值给字符数组保存

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv)
{
ifstream fin("map.txt");
int width = 21;
int height = 19;
char *mapInfo = new char[width * height];
int cntLine = 0;
while(!fin.eof()){
string line;
getline(fin,line);
for(int j = 0;j < width;j++)
mapInfo[cntLine * width + j] = line[j];
cntLine++;
}
cout << "MapInfo : " << endl;
for(int i = 0;i < height;i++){
for(int j = 0;j <width;j++){
cout << mapInfo[i * width + j];
}
cout << endl;
}
fin.close();
return 0;
}


map.txt

111111111111111111111
100000000000000101001
1000010g0000101000001
100000000000000000001
1000000000000000000s1
100010000000000000001
1000000g0001000s00001
100000100001000000001
1000000g0001000100001
100000000000010000011
100000100000000000001
100000000000000000011
100100100000100000001
100000000000000000001
100010000000000000001
100000000000000011001
100000100010000000001
101000000100000010001
111111111111111111111


输出:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: