如何解析输入文件? [英] How can I parse an input file?

查看:93
本文介绍了如何解析输入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解如何从文件中读取。这是文件:



随机大小:3

人数:2

0 :: Person1: :2 :: 0

1 :: Person2 :: 2 :: 2



有人可以帮帮我吗?

这是我做的垃圾:



我尝试过的事情:



I just cant understand how to read from the file. Here is the file:

Random Size: 3
Person Count: 2
0::Person1::2::0
1::Person2::2::2

Can somebody help me please?
And here is the garbage i did:

What I have tried:

std::pair<int, std::vector<Person *> *> parseFileWithName(const std::string& filename){
	int bsiz;
	int pc;
	std::ifstream fil(filename);
	std::vector<Player *> *ow;
	if(fil.is_open()){
		std::string line,np1,np2,np3,np4;
		int bsiz;
		int pc;
		std::getline(filename,line);
		std::istringstream lili(line);
		lili>>np1>>np2>>bsiz;
		std::getline(filename,line);
		std::istringstream pf(line);
		pf>>np3>>np4>>pc;
		while(pc!=0){
			std::getline(filename,line);
			std::string type,nt,ny,nu;
			uint id;
			int xco;
			int yco;
			std::istringstream lineStream(line);
			std::getline(lineStream,id,':');
			std::getline(lineStream,nt,':');
			std::getline(lineStream,type,':');
			std::getline(lineStream,ny,':');
			std::getline(lineStream,xco,':');
			std::getline(lineStream,nu,':');
			lineStream>>yco;
			Person *p1=new Person(id,xco,yco);
			ow->push_back(p1);
			pc--;
		}
	}
	std::pair<int, std::vector<Person *> *> pp(bsiz,ow);
	return pp;
	
}

推荐答案

您应该与该文件的创建者交谈。看起来你有这样的结构化数据:



1.标题(全局信息)

a)随机值的最大值

b)记录数(人数)



2.数据(序列化人数为1b)



第一步:读第一行=>解析随机数

第二步:读第二行=>解析人数

第三步:为人们使用for循环并将其存储在向量中。



仅打开文件< b>一次一行一行地读取。阅读有关基本文件I / O 的教程,以便更好地理解。在示例应用程序中运行代码并进行调试。
You should speak with the creator of that file. It looks like you have structured data like that:

1. Header (global information)
a) a maximal value for random values
b) the count of records (of Persons)

2. the data (serialized persons with count of 1b)

First step: read first line => parse the random number
Second step: read second line => parse the person count
Third step: use a for loop for the persons and store them in the vector.

Open the file only once to read line after line til eof. Read the tutorial on Basic file I/O for better understanding. Run the code in a sample app and debug it.


这篇关于如何解析输入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆