-1

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
NewbieC
  • 13
  • 4
  • 1
    `typedef struct` in C++? Why? – melpomene Apr 04 '18 at 00:16
  • 3
    what does 'but it's not work' mean? – pm100 Apr 04 '18 at 00:18
  • 1
    well for a start you have not opened a file. – pm100 Apr 04 '18 at 00:19
  • Each line read is stored into the string `line`. You have to use it to get your data. – O'Neil Apr 04 '18 at 00:19
  • Please include a [mcve] and be clear about what your code does, and what you want it to do. Saying "it doesn't work" is not at all helpful, because maybe it doesn't compile, or maybe it stores all but the third line, or maybe it throws an exception after 2 seconds, or maybe the numbers are all incremented by 1, we have no idea unless you tell us. – Tas Apr 04 '18 at 00:21
  • And don't use a C-array, use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). – O'Neil Apr 04 '18 at 00:21
  • [Option 2 in the linked answer should help put you on the right path](https://stackoverflow.com/a/7868998/4581301) – user4581301 Apr 04 '18 at 00:21
  • @O'Neil I have do that before, but I realize that I need to update certain variable like id(It's number line) after delete certain line, and show the higher CGPA. So I hope to restore back each line and separate them like a table to my array for processing use. – NewbieC Apr 04 '18 at 01:33
  • You are calling getline to read the line from the file into a variable called line and then if it is successful reading another different line from the file into the struct. So if the file contains only one line you get nothing. But even if you had two lines the >> separates on whitespace and your second and sixth column contain a space. To fix the first problem: inside the loop put line into a stringstream and then extract data from that stream instead of from readFile. – Jerry Jeremiah Apr 04 '18 at 04:31
  • Is your text file separated by tabs? Otherwise how do you know that the spaces in column 2 and 6 are part of the data? Or is there always exactly two pieces separated by a single space in those two columns? – Jerry Jeremiah Apr 04 '18 at 04:43

1 Answers1

0

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;
}
Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32