在C++中从文件中读取整数和字符

Reading integers and chars from a file in C++

本文关键字:整数 读取 字符 文件 C++      更新时间:2023-10-16

因此,我正在尝试编写一个可以从文件中读取数据的程序。该文件由整数、字符和双字符组成。我必须能够将它们分配给不同的变量进行进一步的计算(这实际上只是一个更大程序的一部分)。我在网上搜索过(这里也有),看到了不同的解决方案。字符串流、矢量等等。我最熟悉的一个是在阅读时使用"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;