C++从文件中读取
C++ read from files
你好,我是 c++ 的初学者,现在只是参加一些考试来学习,但现在我遇到了一个问题,它缺少 C++ 的知识,所以我有一个 U2 的文件.txt他的内容是
3 5
Petras Rasa // name of dancer
3 1 5 8 10 // scores for a technique
5 6 7 8 9 // scores for art
Rita Jurgis
6 5 8 5 8
9 8 7 6 5
Rasa Linas
10 10 10 10 10
8 8 8 8 8
所以任务说,前两个数字显示舞者和评委的数字,第二行显示舞者的名字,下面的一行显示分数
技术和下面的一行显示了艺术性的分数,因此它的3个舞者得分低于。
所以现在我需要编写一个函数女巫读取该文件并将分数和名称存储到变量中,我正在使用类和数组系统,所以我将将它们存储在那里,问题是我真的不知道如何读取这样的文件,因为直到现在我正在学习真正的文件,如 Name,54 左右,我正在使用 getline 并以","结束该行,但没有逗号兔子,所以我遇到了一个问题,这是我到目前为止的功能:
const string U2 = "U2.txt";
const string U2rez = "rez.txt";
class Results
{
public:
string name;
int scorestech;
int scoresart;
};
int main()
{
Results v[5]
return 0;
}
void Freading(const string fn,Results v[])
{
int alldancers;
int alljudge;
ifstream fin(fn.c_str());
fin >> alldancers >> alljudge;
for(int a =0; a < 3; a++){
}
}
所以现在我需要阅读这个文件并计算每个舞者的艺术和技巧
Rezultatai v[5]
除了缺少分号外,还定义了一个旧式的固定长度数组。这是不正确的,文件中的第二个数字告诉你有多少法官。
请改用 std::vector<Type>
。这可以改变其长度。
我猜(因为你没有使用英语)对于这个例子,你实际上应该有 3 个Rezultatai
,每个舞者一个。其中每个都应该有 2x5 的分数。
在问这样的问题之前,您应该对此进行更多搜索,无论如何请尝试下面给出的方法。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main ()
{
string line_one_by_one;
ifstream inputfile;
inputfile.open ("names.txt");
while(!inputfile.eof) // To get you all the lines.
{
getline(infile, line_one_by_one); // Saves the line in line_one_by_one
}
inputfile.close();
system ("pause");
}
你要做的是识别每一行并存储你需要的东西。 进一步读取 C++ 中的字符串到 int 的转换。
读:
http://www.cplusplus.com/reference/istream/istream/read/
http://www.cplusplus.com/reference/string/string/?kw=string
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const string U2 = "U2.txt";
const string U2rez = "rez.txt";
class Results
{
public:
string name;
int scorestech;
int scoresart;
};
void Freading(const string fn,Results v[]);
int main()
{
Results v[5];
Freading(U2,v);
return 0;
}
void Freading(const string fn,Results v[])
{
int alldancers;
int alljudges;
int biggest = 0;
int smallest = 99;
ifstream fin(fn.c_str());
fin >> alldancers >> alljudges;
fin.ignore();
for(int a =0; a < alldancers; a++){
int biggest = 0;
int smallest = 99;
int totaltech = 0;
int totalart = 0;
getline(fin, v[a].name);
for(int b = 0; b < alljudges; b++)
{
int scores;
fin >> scores;
totaltech += scores;
}
for(int b =0; b < alljudges; b++)
{
int scores;
fin >> scores;
totalart += scores;
}
v[a].scorestech = totaltech;
v[a].scoresart = totalart;
cout << v[a].name << endl;
cout << v[a].scorestech <<endl;
cout << v[a].scoresart <<endl;
}
fin.close();
}
这是我到目前为止所做的,但我得到的结果是:
Petras Rasa
27
35
0
45
0
45
我做错了或我做错了什么,因为不知何故,当循环第二次时,它没有捕获第二个舞者的名字
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 读取文件的最后一行并输入到链接列表时出错
- 为什么在读取文件大小时文件IO速度会发生变化
- 读取文件时运行时的未知行为
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- C++ 读取文件读取文件不正确
- 读取文件并将其存储在unordered_map中时出现问题
- 读取文件时无法使用 OpenMP 获得加速
- 使用istringstearm和get行缓慢读取文件
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 读取文件时引发异常
- 从标准输入读取文件后读取用户输入
- 在读取文件后重置句柄
- 如何在C++编译时读取文件?
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- 读取文件在第二次调用时返回INVALID_HANDLE
- 通过指针读取文件
- 逐行读取文件,并将数据插入变量和数组中