【发布时间】:2018-07-17 10:34:35
【问题描述】:
我设法从 .txt 文档中提取一行并将其存储在 char 数组中
ifStream inData;
inData.open("test.txt');
char range1[40];
inData.getline(range1, 40);
我得到的输出是:
BaseIdRange=0-8
我想将数字 0 和 8 存储在两种不同的数据类型中。 即 int1 = 0 和 int2 = 8
非常感谢所有帮助。
【问题讨论】:
-
欢迎来到 stackoverflow.com。请花一些时间阅读the help pages,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。也请take the tour 和read about how to ask good questions。最后请学习如何创建Minimal, Complete, and Verifiable Example。
-
另外,请阅读this question checklist 和idownvotedbecau.se 的所有内容,了解您的问题可能被否决的一些原因。最后请learn how to debug your programs.
-
首先,不要再用 C 风格的字符串折磨自己了。将该行读入
std::string,然后将该行放入std::stringstream。您可以使用std::getline()将行拆分为多个部分,并使用std::stoi()解析整数。您也可以在从std::cin读取时直接拆分该行,但这不太容易出错。