1

I'm trying to build a matrix in C++ by reading the a file called matrix.txt and storing their values so it's easier for me to run my other algorithm. I'm pretty new to using fstream, so this is probably not the most efficient code, but this is my attempt so far (note, most of the classes are for other algoritms).

I decided to test so I could see if it works fine. But I seem to be bumping into this problem when I get to the end of a line. The text file values are separated by a delimiter ',' but it's rows are separated by line breaks. When I execute this code (with comments at the bottom) I get 2 values. The last value on the line and the first of the next line. But when I remove the comments I don't get that problem. Is there a way to fix this?

My code:

    #include <iostream>
    #include <list>
    #include <math.h>
    #include <iomanip>
    #include <algorithm>
    #include <fstream>
    #include <string>
    using namespace std; 

int main(){
    string str[80][80];
    ifstream eulereightyone;
    int a = 0;
    int b = 0;
    eulereightyone.open("matrix.txt");
    if (!eulereightyone)
    {
        cout << "couldn't open file" << endl;
        system("pause");
        return 0;
    }
    while (eulereightyone.good())
    {
        getline(eulereightyone, str[a][b], ',');
        if (a > 78)
        {
            a = 0;
            ++b;    
        }
        a++;
    }
  //cout << str[78][1] << endl;
    cout << str[79][1] << endl;
  //cout << str[0][2] << endl;
    system("pause");
    return 0;
}
Brien
  • 5,983
  • 1
  • 34
  • 38
  • possible duplicate of [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user657267 Jun 26 '15 at 04:37
  • Can you change the format of the file? – Beta Jun 26 '15 at 04:43
  • have you considered this a[0][b] is never stored. When a=79 a is intialised to 0 after input and right after that it is incremented to 1 so a[0][b] is never stored – Sourav Kanta Jun 26 '15 at 04:43
  • Are the dimensions of the matrix known beforehand? It would be better to use a smaller matrix until the function works, and post the text file. – Beta Jun 26 '15 at 04:46
  • My matrix is initialized to be 80 by 80. – chainyn1234 Jun 26 '15 at 15:21
  • Please check the outputs which you have commented.I am sorry but my compiler is not working.Please post those as well.@chainyn1234 – Sourav Kanta Jun 26 '15 at 16:57
  • I get 9353, 9377, 9607, which are all correct, but when I comment 2 of them out I get an incorrect output. For example, commenting the first 2 out gives me nothing. Commenting the first and last out gives me 2 outputs (9377, 9607). Commenting the last 2 gives me the correct output (9353). It's really strange... It's important because I want to take each individual entry, turn them into ints, then compare them. – chainyn1234 Jun 26 '15 at 17:10

1 Answers1

1

If you are doing this to learn how to use static tables, it is acceptable. However, it isn't a good practice if you are processing your data like this for a bigger project (or after gaining some experience in C++), you should definitely use the standard library.

You will probably get this answer somewhat often but it is an extremely good practice to not recode containers for matrix/list/vectors/multimaps in C++. Use the standard library so you won't have to re-implement all the sorting methods/templates and you will have useful operators such as move for very large subsets of data.

It is also a security concern, if your code goes into production with static tables, you will have possible overflows.

In the case of this particular problem, you could do a vector<string> so that your matrix is re-sizable and defined by you and not static and will fail properly if you call outside the defined range.

vector<string> > vec;
const int rows = 80;
const int columns = 80;
vec.resize(rows * columns);
for (int row = 0; row < rows; ++row) {
    for (int col = 0; col < columns; ++col) {
        getline(eulereightyone, vec[row * columns + col], ',');
    }
}
Armfoot
  • 4,137
  • 2
  • 39
  • 58
Ashley M.
  • 26
  • 1
  • could I use vector > vec; instead? That way I have a matrix instead of a vector? – chainyn1234 Jun 26 '15 at 16:36
  • I tried using that idea while reading a similar problem on this site that utilized vectors. I think your idea of using vectors is much better. I just tried it and I think I got it working really well. – chainyn1234 Jun 26 '15 at 17:29