C++从文件中读取

C++ read from files

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

你好,我是 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
我做错了

或我做错了什么,因为不知何故,当循环第二次时,它没有捕获第二个舞者的名字