D.Ward
D.Ward

Reputation: 13

Does std::ifstream.get capture everything including \n?

I am working on a project to solve the Banker's Algorithm. I have read input from a file that defines an available array, a matrix for allocated resources and maximum resources. The input file is structured as:

3 3 2

0 1 0
2 0 0
3 0 2
2 1 1
0 0 2

7 5 3
3 2 2
9 0 2
2 2 2
4 3 3

My code to read in the matrices is:

// get input for available resources
for (int r = 0; r < R; ++r) {
    std::string numStr;
    char in;
    do {
        input.get(in);
        numStr += in;
    } while (in != ' ');
    avail_[r] = std::stoi(numStr);
}

// get input for allocated vector
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        std::string numStr;
        char in;
        do {
            input.get(in);
            numStr += in;
        } while (in != ' ');
        alloc_[p][r] = std::stoi(numStr);
    }
}

// get input for max
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        std::string numStr;
        char in;
        do {
            input.get(in);
            numStr += in;
        } while (in != ' ');
        max_[p][r] = std::stoi(numStr);
    }
}

I have loops that output the value of the matrices after they are read. The output is

Available:
3 3 2 
Allocated:
1 0 0 
0 0 2 
1 1 0 
2 5 3 
2 2 0 

It stops after reading the allocation matrix and it doesn't even read it right. I assume I am using std::ifstream.get() wrong, but I don't see how.

I have found that if I put all of my data in 1 line, it reads the allocation matrix correctly, but my program still stalls when trying to read in the max matrix (even though I used the same code I used for the allocation matrix when reading in max).

Upvotes: 0

Views: 59

Answers (1)

kiner_shah
kiner_shah

Reputation: 4641

Please see my comments marked with // CHANGE HERE

const int R = 3;
const int P = 5;
int avail_[3];
int alloc_[5][3];
int max_[5][3];
std::string line;
    
// get input for available resources
for (int r = 0; r < R; ++r) {
    // std::string numStr;
    // char in;
    // do {
    //     input.get(in);
    //     numStr += in;
    // } while (in != ' ');
    // avail_[r] = std::stoi(numStr);

    // CHANGE HERE: read int directly from input
    if (!(input >> avail_[r])) {
        std::cout << "Error reading\n";
        return 1;
    }
}
// CHANGE HERE: read the blank line
//std::getline(input, line);
// get input for allocated vector
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        // std::string numStr;
        // char in;
        // do {
        //     input.get(in);
        //     numStr += in;
        // } while (in != ' ');
        // alloc_[p][r] = std::stoi(numStr);

        // CHANGE HERE: read integer directly from input
        if (!(input >> alloc_[p][r])) {
            std::cout << "Error reading\n";
            return 1;
        }
    }
}
// CHANGE HERE: read the blank line
//std::getline(input, line);
// get input for max
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        // std::string numStr;
        // char in;
        // do {
        //     input.get(in);
        //     numStr += in;
        // } while (in != ' ');
        // max_[p][r] = std::stoi(numStr);

        // CHANGE HERE: read integer directly from input
        if (!(input >> max_[p][r])) {
            std::cout << "Error reading\n";
            return 1;
        }
    }
}

std::cout << "Available\n";
for (int r = 0; r < R; r++)
{
    std::cout << avail_[r] << " ";
}
std::cout << endl;

std::cout << "Allocated\n";
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        std::cout << alloc_[p][r] << " ";
    }
    std::cout << std::endl;
}

std::cout << "Max\n";
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        std::cout << max_[p][r] << " ";
    }
    std::cout << std::endl;
}

Upvotes: 0

Related Questions