简体   繁体   中英

c++ program not printing 2d vector grid properly but is close

Im currently trying to parse a text file of numbers into a 2d vector that will be modified later, but with my code so far. I'm getting this:

845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
074186093�845630179

Its fine and all, except it repeats line number 9 and puts junk at the end.

If i put an enter at the end of the text, it prints out this:

845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
9203574619

(the 10th element in the 9th row shouldn't be there)

for reference, here is what the text file looks like:

845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
920357461

This is my code so far:

int main(int argc, char* argv[]) {
//parsing the textfile.
vector<vector<char>> grid;
fstream fin; char ch;
string name (argv[1]); //File Name.
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed 
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
while(fin)
{
    ch = fin.get();
    if(ch!='\n') {
        temp.push_back(ch);
        cout << ch;
    }
    else 
    { 
        grid.push_back(temp); 
        temp.clear(); 
        cout << ch;
    }
}
for (int i = 0; i < grid.size();i++) {
    for (int j = 0; j < grid[i].size();j++) {
        cout << grid[i][j];
    }
}
}

Your code works well for me.

If there is an enter at the end of your text file it will also push the last number to your grid. Otherwise you need to push the content of temp to grid after the while loop.

One quick fix gets rid of most of your problem:

int main()
{
//parsing the textfile.
    vector<vector<char>> grid;
    fstream fin;
    char ch;

    // Hardcoded value. Typing the same thing in over and over while debugging is for suckers.
    string name("data.txt"); //File Name. 
// 2D Vector.
    vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
    fin.open(name.c_str(), ios::in);
// Assume name as an arbitary file.
    while (fin.get(ch)) // the change: Slightly different get and getting in the loop
                        // condition. If nothing is gotten the loop doesn't enter
                        // solves almost all of the problems.
    {
        if (ch != '\n')
        {
            temp.push_back(ch);
            cout << ch;
        }
        else
        {
            grid.push_back(temp);
            temp.clear();
            cout << ch; 
        }
    }
    for (int i = 0; i < grid.size(); i++)
    {
        for (int j = 0; j < grid[i].size(); j++)
        {
            cout << grid[i][j];
        }
    }
}

output

845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
920357461845630179032918654190745328683074912457201836219863540361429705074186093

That leaves the garbage on the end. That's not garbage. Thats' your actual output. Everything up to

845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
920357461

is the result of the cout << ch; s inside the while loop that's doing the collection. The

845630179032918654190745328683074912457201836219863540361429705074186093

On the end is the for loops. Since the newline wasn't stored they're printed out as one big block of digits.

To fix that we'll remove the output from the while loop and restore the newline in the for loop.

int main()
{
//parsing the textfile.
    vector<vector<char>> grid;
    fstream fin;
    char ch;
    string name("data.txt"); //File Name.
// 2D Vector.
    vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
    fin.open(name.c_str(), ios::in);
// Assume name as an arbitary file.
    while (fin.get(ch))
    {
        if (ch != '\n')
        {
            temp.push_back(ch);
        }
        else
        {
            grid.push_back(temp);
            temp.clear();
        }
    }
    for (int i = 0; i < grid.size(); i++)
    {
        for (int j = 0; j < grid[i].size(); j++)
        {
            cout << grid[i][j];
        }
        cout << '\n'; // 
    }
}

And if we do this with a vector<string> , about a quarter of the code goes away.

 int main()
{
//parsing the textfile.
    vector<string> grid;
    fstream fin;
// 2D Vector.
    vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
    fin.open("data.txt", ios::in);
// Assume name as an arbitary file.
    string line;
    while (getline (fin, line))
    {
        grid.push_back(line);
        temp.clear();
    }
    for (int i = 0; i < grid.size(); i++)
    {
        for (int j = 0; j < grid[i].size(); j++)
        {
            cout << grid[i][j];
        }
        cout << '\n'; // getline ate the newline. Have to put it back
    }
}

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