在C++中从文件中读取整数和字符
Reading integers and chars from a file in C++
因此,我正在尝试编写一个可以从文件中读取数据的程序。该文件由整数、字符和双字符组成。我必须能够将它们分配给不同的变量进行进一步的计算(这实际上只是一个更大程序的一部分)。我在网上搜索过(这里也有),看到了不同的解决方案。字符串流、矢量等等。我最熟悉的一个是在阅读时使用"skipws"。但我似乎读不懂整数。
我更喜欢一个需要使用尽可能少的方法(流、get等)的解决方案。但如果我需要更多,我可以接受。
#include <iostream>
#include <fstream>
#include <cstring>
using namespace::std;
int main()
{
int j = 0, kill = 0, i;
char initials[10];
int results[10];
double avg;
ifstream read("P1.txt");
if (!read) //test to see if file can be opened.
{
cerr << "Error" << endl;
exit(1);
}
while(kill != 1) //kill = 1 same as eof. Just my weird way of coding.
{
switch (j)
{
case 0: //read the first line of data, which is the chars.
read >> skipws >> initials;
j++;
break;
case 1: //read the second line of data, which is 10 integers and whitespace
for (i=0;i<10;i++)
read >> skipws >> results[i];
j++;
break;
case 2: //read the last line which is a single double.
read >> skipws >> avg;
j++;
break;
case 3: //check for end of file.
if(read.eof() == 0)
kill = 1;
break;
}
}
//the 3 lines below are just writing the contents of the file to the prompt.
//It's to check that values are stored correctly for further use.
cout << initials << endl;
cout << results << endl;
cout << avg << endl;
return 0;
}
我很清楚y输入文件"P1.txt"会是什么样子,因为我自己在程序的另一部分创建了它。它看起来像这样:
ccs
2 4 5 3 1 4 6 1 1 1
2.8
这3行给了我一个球员的首字母缩写,他10场比赛的成绩和他的平均得分。简而言之,就是一个玩家的数据。在最后的程序中,我需要能够读取大约10名玩家的值。
我期待着最后的3条时装线能显示出这一点:
ccs
2453146111
2.8
现在,这是我所能做到的。如果我将结果[10]声明为char,我可以在控制台中获得:
ccs
2453146111ccs
2.8
如果我把它声明为int,我就会得到这个
ccs
0x7fff5fbff6c0
2.8
很明显,我可以看到值没有正确存储,有些事情已经失控,但我需要其他人关注这一点。我没有线索了。
我知道这是一种混乱的方法,而且可以在更少的空间内完成,但我是C++的新手,所以这对我来说很有意义,直到现在,我都很容易找到我的错误。
我尽力解释清楚。这是我在这里的第一篇文章,所以如果你需要更多信息,请告诉我。
提前感谢!!克里斯。
当结果为int[10]并且您使cout<lt;结果<lt;endl它将打印数组内存地址,您应该在每次迭代中循环打印结果[i]。
for(i=0;i<10;i++){
cout<< result[i];
}
它将给出正确的结果
如果您使用C++,我建议您使用以下方法:
- 创建三个
stringstream
- 将每一行读入一个
stringstream
- 对于每一行使用(当您的
stringstream
被称为ss
时):ss >> c
(c
是您的角色)ss >> i
(其中i
是您的整数)ss >> d
(d
为双人间)
- 当一行上可以给出多个输入时,将值放入向量中
一些代码:
string s[3] = {"ccs",
"2 4 5 3 1 4 6 1 1 1",
"2.8"
};
stringstream ss[3];
/*
Open file, get the first line into s[0], second into s[1] and third into s[2]
*/
ss[0] << s[0];
ss[1] << s[1];
ss[2] << s[2];
char c;
int i;
double d;
vector<char> initials;
vector<int> scores;
vector<double> averages;
while (ss[0] >> c) {
cout << c << " ";
initials.push_back(c);
}
cout << endl;
while (ss[1] >> i) {
cout << i << " ";
scores.push_back(i);
}
cout << endl;
while (ss[2] >> d) {
cout << d << " ";
averages.push_back(d);
}
cout << endl;
此外,当您从文件中读取值时,它不是"int"数据类型。你不能只是把它推回到一个int数组中并期望它工作,你可能需要使用std::stoi
之类的东西将它转换为int
不要重新发明轮子,可以使用Boost::tokenizer来标记字符串,并使用Lexical_cast将字符串转换为数字。
如果你想使用纯c,你可以使用strtok来标记字符串
由于您的数据采用已知的格式,这个问题变得非常容易。不要浪费这些。
string initials;
vector<int> r(NUM_SCORES);
double avg;
现在获取数据:
ifstream infile;
infile.open(...);
while(infile >> initials){ // i.e. "while there's another player"
for(int i=0; i<r.size(); i++) infile >> r.at(i);
infile >> avg;
}
工作完成了。
cout << "Player " << intials << " scored:" << endl;
for(int i=0; i<r.size(); i++) cout << r.at(i);
cout << endl << "giving him an average of " << avg << endl;
- 如何在c++中从文本文件中逐行读取整数
- Arduino从Txt读取整数
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 从二进制文件中读取整数数组
- 如何从字符串C++读取多个整数
- 使用字符串流读取整数并将它们相加
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 如何使用字符和整数C 读取文件
- 从文件到短 /长整数读取字节
- do整数读取需要受到关键部分的保护
- 此项目不会将整数读取为二进制并将其作为二进制输出到新文件
- 将连续字节作为一个整数读取
- 如何将文件中的整数读取到动态数组中
- 如何将字符串读取为字符串向量或将整数读取为整数向量
- 将文件中的间隔整数读取到C++数组中
- 将文件中的整数读取到C++中的向量中
- 如何在C++中将空格和换行符分隔的整数读取到2D数组中
- 将一组整数读取为C++中的字符串
- 我如何得到多行字符串和整数读取和输入文件,并在c++中显示
- 通过无符号整数读取无符号字符数组未定义,因此不安全