繁体   English   中英

如何从文本文件中读取一行并将它们分隔为不同的变量?

[英]How to read a line from text file and separate them in different variables?

这是我的示例文本文件, 在此处输入图像描述

1       Class Physics       American      3.6     5     Maria Garcia       1-541-754-3010   
2       Class Chemical      Australian    3.5     4     Maria Hernandez    1-541-754-3233 

我有一组数组和变量。

typedef struct
{
    double current;
    unsigned int subject;
}CGPA;
typedef struct
{
    char program[40];
    char state[50];
    char name[50];
    char contact[50];
    string studentid;
    CGPA a;

}INFOR;

如何将它们存储在其他变量中以供以后处理?

这是我的代码的某些部分,但是它无法获取正确的值并将它们从txt文件存储到我的struct数组中:

for( int i = 0; getline(readfile, line); i++)
{
    //getline(readfile,student[i].id, '\0');
    //getline(readfile,student[i].a.subject, '\0');
    //strcpy(student[i].subject, temporary_s);
    readfile >> student[i].studentid >> student[i].program >> student[i].state >> student[i].a.current >> student[i].a.subject >> student[i].name >> student[i].contact; //This is my code cannot read my text file correctly
    ++line_count;
}

这是我的示例输出: 在此处输入图像说明,我想删除文件的某些行,更新文件的某些行并显示较低或较高的CGPA,这就是为什么我需要将文本文件值恢复到数组以供以后处理的原因。

该代码无法以正确的方式读取我的文件,我不知道如何处理

readfile >> student[i].studentid >> student[i].program >> student[i].state >> student[i].a.current >>student[i].a.subject >> student[i].name >> student[i].contact; //This is my code cannot read my text file correct

我不知道为什么您可以将CGPA数据与其余的数据放到INFOR中时需要一个单独的结构,但我已经离开了。 我确实将所有字符数组都更改为std :: string,因为将它们作为字符数组并没有明显的优势。 我将INFOR的数组更改为std :: vector,因为将数组作为数组没有明显的优势。 如果确实需要按您的要求对结构进行布局,请告诉我,因为需要更改代码。

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

struct CGPA
{
    double current;
    unsigned int subject;
};

struct INFOR
{
    std::string program;
    std::string state;
    std::string name;
    std::string contact;
    std::string studentid;
    CGPA a;
};

bool testing = true;

main() {
    if(testing) {
        std::ofstream writeFile("file.txt");
        writeFile << "1\tClass Physics\tAmerican\t3.6\t5\tMaria Garcia\t1-541-754-3010\n"
                  << "2\tClass Chemical\tAustralian\t3.5\t4\tMaria Hernandez\t1-541-754-3233\n";
        writeFile.close();
    }
    std::vector<INFOR> students;
    std::ifstream readfile("file.txt");
    std::string line;
    while(std::getline(readfile, line))
    {
        std::stringstream ss(line);
        INFOR student;
        std::string column;
        getline(ss, column, '\t'); student.studentid = column;
        getline(ss, column, '\t'); student.program = column;
        getline(ss, column, '\t'); student.state = column;
        ss >> student.a.current;
        ss >> student.a.subject;
        getline(ss, column, '\t'); student.name = column;
        getline(ss, column, '\t'); student.contact = column;
        students.push_back(student);
    }
    readfile.close();
    int line_count = students.size();
    if(testing) {
        std::cout << line_count << " lines";
    }
    return line_count;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM