0

I am writing a C++ console application. After creating an Matrix with size(int) rowSize and columnSize, I wanted to write the letters in text file to matrix, but while loop never runs because reader's location is -1, and I couldn't take it to 0. Any ideas?

void writeText2Vec(ifstream & reader, tmatrix<char> & table){
    string s;
    int length, row = 0, column = 0; //beginning: column and
                                     //row ids are set to 0.
    reader.seekg(0); //reset the pointer to start

    cout << reader.tellg(); // it results in -1.
    while (getline(reader,s)){
        //for each line while loop occurs.
        length = s.length();
        for(column = 0; column < length; column++)
        {
            table[row][column] = s.at(column);
            //writing the letter to matrix.
        }
        row++;
    }

bool openFile(ifstream & reader, string filename){
    reader.open(filename.c_str());

    if (reader.fail()){
        exit("File cannot be found.");
        return false;
    }
    else {
        return true;
    }
}

bool check(ifstream & reader, int & rowSize, int & columnSize){
    //checks for valid characters, if they are
    //alphabetical or not and for the length.

    string s;
    int len = 0, max = 0;
    while (getline(reader,s)){
        //runs for every line.
        rowSize++; //calculation of row size

        while (len < s.length()){
            if (!(isalpha(s.at(len)))){
                // Check to see if all characters are alphabetic.
                exit("Matrix contains invalid characters!");
                return false;
            }
            len++;
        }
        if (max == 0){
            //if max is not set.
            max = len;
            len = 0;
        }
        else if (!(max == len)){
            //if they are different, then appropriate
            //error message is returned.
            exit("Matrix is not in a correct format!");
            return false;
        }
        else {
            //else it resets.
            len = 0;
        }
    }
    rowSize -= 1;
    columnSize = s.length(); //the last length is equal to column size
    return true;
}
hmjd
  • 120,187
  • 20
  • 207
  • 252
Yagiz
  • 1,033
  • 2
  • 21
  • 47
  • 1
    `-1` return value from `tellg()` indicates a failure, not a position. Can you post the calling code of `writeText2Vec()`? – hmjd Mar 06 '12 at 16:12
  • Try `reader.seekg(0, std::ios::beg)` instead. – Kerrek SB Mar 06 '12 at 16:17
  • tried ios::beg but didn't change the result. tmatrix table(rowSize, columnSize); //creation of the matrix table. writeText2Vec(reader, table); //write the txt file to matrix. – Yagiz Mar 06 '12 at 16:24
  • @Alex, can you the code to question that opens the `reader` and calls `writeText2Vec()`? It may be a problem with opening the `reader`. – hmjd Mar 06 '12 at 16:30
  • @hmjd added functions to the question. the code that calls these functions are: if ((openFile(reader, filename) == true) && (check(reader, rowSize, columnSize) == true)){ //if the checks are true, the program continues tmatrix table(rowSize, columnSize); //creation of the matrix table. writeText2Vec(reader, table); //write the txt file to matrix. – Yagiz Mar 06 '12 at 16:33

1 Answers1

2

tellg() returns -1 when there was an error. The stream was probably in an error state when you called the function. If you've read to the end once, then there was an input which failed, and the stream is in an error state, which must be cleared before any other operations will work: reader.clear().

James Kanze
  • 150,581
  • 18
  • 184
  • 329