简体   繁体   中英

Problems with reading a text-file and putting the tokens into a 2D matrix

I have a problem reading a text file and putting in the tokens from the text file into a 2-D matrix.

Here are my codes:

   std::vector< vector <std::string> > my_matrix(10, vector <std::string>(10));

ifstream myReadFile;
myReadFile.open("class_data.txt", ios_base::in);
char output[100];
if (myReadFile.is_open()) {
    if (!myReadFile.eof()) {
        myReadFile >> output;

        char* token = NULL;
        char* context = NULL;
        char delims[] = " ,\t\n";

        token = strtok_s(output, delims, &context);

        if (token != NULL)
        {
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    my_matrix[i][j] = *token;
                    cout << "token = " << token << endl;
                    cout << "matrix = " << my_matrix[i][j] << endl;
                    token = strtok_s(NULL, delims, &context);
                }
            }
        }
    }
}

std::cout.width(3); std::cout << left << "ID";
std::cout.width(9); std::cout << left << "Project1";
std::cout.width(9); std::cout << left << "Project2";
std::cout.width(9); std::cout << left << "Project3";
std::cout.width(9); std::cout << left << "Project4";
std::cout.width(9); std::cout << left << "Project5";
std::cout.width(9); std::cout << left << "Project6";
std::cout.width(9); std::cout << left << "Project7";
std::cout.width(9); std::cout << left << "Midterm";
std::cout.width(9); std::cout << left << "Final" << std::endl;
std::cout << "-------------------------------------------------------------------------------" << std::endl;

for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        cout.width(10); std::cout << my_matrix[i][j];
    }
    std::cout << " " << std::endl;
}

I have no errors, it compiles, but further in the program when I would like to display the 2-D matrix, it does not display it. It holds no value in to whole matrix.

Can anyone help me out and tell me what is the problem ? Is it with the way I did the 2 for loops or is it the way I read the text file ? If problem is found, could anyone suggest a solution.

PS Sorry if I do not have the best professional codes, but I am a beginner in C++ and I did not take any University level programming courses.

You already have a 10x10 matrix and strtok returns a char * to the current token, just pass it to the std::string operator= in the matrix like this:

my_matrix[i][j] = token;  

You also need to rethink the logic in your loop. After the strtok call in the inner for, add a check for the return value and break from both loops if it is NULL. Replace the while loop with an if test.

Edit:

I see you changed the while to a if, However, the 2 for loops need a little change too. I suggest something like this:

for (int i = 0; i < 10 && token; i++) {
   for (int j = 0; j < 10 && token; j++) {
       my_matrix[i][j] = token;   // NOTE: not *token here
       cout << "token = " << token << endl;
       cout << "matrix = " << my_matrix[i][j] << endl;
       token = strtok_s(NULL, delims, &context);
   }
}

This change also makes the if test before the loops unnecessary.

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