读取/解析文本文件输入C ++ [英] Reading/parsing text file input c++

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

问题描述

一些背景知识:我正在为一个学校项目开发一个滑块拼图,这是我们第一次使用C ++而不是Java.这是我第一次必须实现从文件中读取数据的东西.

A little background: I am working on a sliding block puzzle for a school project and this is our first using C++ instead of Java. This is the first time I have had to implement something that reads data from a file.

关于从文本文件读取输入,我有一个简单的问题. 我了解如何逐行读取文件并保持字符串中的每一行,我想知道在读取文件时是否可以将字符串解析为不同的数据类型.

I have a simple question regarding reading input from a text file. I understand how to read the file line by line and hold each line in a string, I want to know if I can parse the string into different data types as the file is read.

当前,我正在读取每一行并将它们作为字符串存储在向量中,以便稍后进行解析,而且我知道必须有一种更简单的方法来实现这一点

Currently I am reading each line and storing them as strings in a vector for parsing later, and I know there must be a much simpler way to implement this

第一行包含2个整数,这些整数将指示网格的长度和宽度,接下来的行将具有4个整数和一个char(在创建块时用作参数).

The first line holds 2 integers which will indicate the length and width of the grid, the following lines will have 4 integers and a char for use as arguments when creating the blocks.

我的问题是,如果我改为逐个字符地读取文件,是否可以使用一个函数来检测字符是整数还是字符(并忽略空格),以便我可以立即存储它们并在读取文件时创建块对象?在这种情况下,我该如何处理大于10的整数?

My question is this, if I read the file character by character instead, is there a function I can use that will detect if the character is an integer or a char (and ignore the spaces) so I can store them immediately and create the block objects as the file is read? How would i deal with integers >10 in this case?

只是注意到我正在使用fstream来读取文件,我不熟悉其他输入法

Just noting I am using fstream to read the files, I am unfamiliar with other input methods

示例输入:

4  4
3  1  2  1  b
1  1  1  1  a 

推荐答案

要检测某个字符串是否可以解析为整数,只需解析它,看看是否成功.最好的功能可能是std::strtoul(),因为它可以告诉您消耗了多少个字符,以便您可以在此之后继续解析. (有关详细信息,请参见手册页.)

To detect whether a piece of string can be parsed as an integer, you just have to parse it and see if you succeed. The best function for that would probably be std::strtoul(), since it can be made to tell you how many characters it consumed, so that you can continue parsing after that. (See the man page for details.)

但是,如果您已经知道文件的格式,则可以使用iostream格式的提取.这很简单:

However, if you already know the format of your file, you can use iostream formatted extraction. This is quite straightforward:

#include <fstream>


std::ifstream infile("thefile.txt");

int n1, n2, x1, x2, x3, x4;
char c;

if (!(infile >> n1 >> n2)) { /* error, could not read first line! Abort. */ }

while (infile >> x1 >> x2 >> x3 >> x4 >> c)
{
    // successfully extracted one line, data is in x1, ..., x4, c.
}

另一种替代方法是将每一行读入一个字符串(使用std::getline),然后从该行创建一个字符串流,然后使用>>解析该字符串流.这样做的好处是,您可以发现并跳过不良行并进行恢复,而在上面介绍的直接格式化提取中,您将无法从任何错误中恢复.

Another alternative is to read every line into a string (using std::getline), then creating a stringstream from that line, and parsing the stringstream with >>. This has the added benefit that you can discover and skip bad lines and recover, while in the direct formatted extraction I presented above, you cannot recover from any error.

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

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