0

So I need to read a grid from a file,the grid's width and lengths is always the same.The problem is when I try to cout it on the last line it only shows about a half of it.

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;
ifstream TestIn("test");


int main()
{

    char Grid[1000][1000],s[1000];

    int LungimeX,LungimeY,i,j;

    TestIn.getline(s,1000);
    //finding the length
    LungimeX=strlen(s);
    cout<<LungimeX<<endl;

    //finding the width
    while (!TestIn.eof()) {
        TestIn.getline(s,1000);
        LungimeY++;
    }
    cout<<LungimeY;

    //reset .eof
    TestIn.clear();
    TestIn.seekg(0, TestIn.beg);

    //get the grid into the array
    for(i=1;i<=LungimeY;i++) {
    for(j=1;j<=LungimeX;j++) {
        TestIn.get(Grid[i][j]);
    }}

    for(i=1;i<=LungimeY;i++){
    for(j=1;j<=LungimeX;j++){
        cout<<Grid[i][j];
    }}

    return 0;
}

So yeah,any ideas how to fix this?

Lapshin Dmitry
  • 1,084
  • 9
  • 28
  • can you upload the file you are trying to read? – Minato Oct 14 '15 at 10:11
  • 1
    http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – πάντα ῥεῖ Oct 14 '15 at 10:13
  • just a guess: does the input file contain end-of-line characters ('\n') ? Probably yes, so they get read into Grid, which you don't want. Another thing is that your use arrays as 1'based, the indices in C++ begin with 0. Grid[0][1000] is wrong. – marek.jancuska Oct 14 '15 at 10:19

3 Answers3

0
  1. LungimeY isn't initialized
  2. Need to read (to skip) the header line after the file rewind
  3. Need to skip CR and/or LF characters after each line read when filling the array
AndreyS Scherbakov
  • 2,674
  • 2
  • 20
  • 27
0

You were not ignoring the newline character LungimeX is the length of the line not including the new line character. a simple solution could be when reading the file if newline character is encountered read the next character. #include #include #include

using namespace std;
ifstream TestIn("test");


int main()
{

    char Grid[1000][1000],s[1000];

    int LungimeX,LungimeY,i,j;

    TestIn.getline(s,1000);
    //finding the length
    LungimeX=strlen(s);
    cout<<LungimeX<<endl;

//finding the width
            while (!TestIn.eof()) {
                TestIn.getline(s,1000);
            LungimeY++;}
cout<<LungimeY;

//reset .eof
TestIn.clear();
TestIn.seekg (0, TestIn.beg);

//get the grid into the array
for(i=1;i<=LungimeY;i++){
for(j=1;j<=LungimeX;j++){
    TestIn.get(Grid[i][j]);
    if(Grid[i][j] == '\n') //check new line character
       TestIn.get(Grid[i][j]);
}}

for(i=1;i<=LungimeY;i++){
for(j=1;j<=LungimeX;j++){
    cout<<Grid[i][j];
}
cout<<endl;
}



    return 0;
}

And yes Please use 0 indexing in C++ you are wasting memory this way.

Minato
  • 4,383
  • 1
  • 22
  • 28
0

What about this kind of more C++ approach ?

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string fname("test.txt");
    std::ifstream f(fname.c_str());

    std::vector<std::string> lines;
    std::string line;
    while(std::getline(f, line))
        lines.push_back(line);

    unsigned long int num_rows = lines.size();
    unsigned long int num_cols = 0;
    if(num_rows > 0)
        num_cols = lines[0].length();

    std::cout << "num_rows = " << num_rows << std::endl;
    std::cout << "num_cols = " << num_cols << std::endl;

    for(unsigned long int i = 0; i < num_rows; ++i)
    {
        for(unsigned long int j = 0; j < num_cols; ++j)
            std::cout << lines[i][j];
        std::cout << std::endl;
    }

    return 0;
}
Caduchon
  • 4,574
  • 4
  • 26
  • 67