简体   繁体   中英

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

This is my sample text file enter image description here

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 

And I have a group of array and variable.

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;

How can I store them in different variable for later processing use?

Here are the some part of my code but it cannot get the correct value and store them to my struct array from my txt file:

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;
}

This is my sample output: enter image description here I want to delete certain line, update certain line of my file and show up the lower or higher CGPA, this is why i need to restore my text file value to the array for later processing use.

This code cannot read my file in right way, I don't know to to deal with it

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

I don't know why you need a separate struct for the CGPA data when you could put it into INFOR with the rest but I have left it. I did change all the character arrays to std::string since there was no obvious advantage to them being character arrays. And I changed the array of INFOR to a std::vector since there was no obvious advantage to it being an array. If the structs really do need to be laid out as you had them let me know because the code will need to be changed.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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