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

本文简述了如何使用C++读取并输出地图文件内容的流程,包括使用fstream读取文件,getline函数读取并保存到字符数组中,最后遍历输出地图信息。

简述:从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

输出:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值