0

Here's the relevant code. The whole project compiles with no issues.

list<Line> loadText(const string &textFile)
{
   ifstream txt; 
   string temp;
   char ch;
   list<Line> fullText;
   unsigned length;

   txt.open(textFile.c_str());
   if (!txt) 
   {
       cout << "Can't open " << txtFile << " \n";
       exit(1);
   }

   // process file line by line
   txt.seekg (0, ios::end);
   length = txt.tellg();
   txt.seekg (0, ios::beg);
   cout << length;
   for(int j = 0; j < length; j++)
   {
       cout << j;
       txt.get(ch);
       temp += ch;
       cout << ch;
       if (ch == '\n')
       {
          cout << temp;
          Line line(temp);
          row.printLine();
          fullText.push_back(row);
          cout<< "line done \n";
       }
   }

So this function is meant to take a text file, and make an List of Lines(a custom class that stores a list of characters). All of the 'cout' is for debugging purposes.

If i input a text file like this:

Line 1
Line 2
Line 3
Line 4

I get an output like this:

loading maze...
 30 0L1i2n3e4 516
 Line 1
 Line 1
 line done
destroyed

Note that destroyed is just the output given when a Lines destructor is called.

So obviously its having issue after the first iteration, but after hours of trying to sort this out, i haven't figured this out.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
user3593486
  • 47
  • 2
  • 7
  • http://stackoverflow.com/questions/12240010/how-to-read-from-a-text-file-character-by-character-in-c – Leon May 02 '14 at 06:36
  • The posted function is incomplete. It's missing the `return` statement as well as the closing `}`. – R Sahu May 02 '14 at 06:36
  • Use `getline` to read line by line. – Marius Bancila May 02 '14 at 06:36
  • 2
    After finishing reading each line, you should clear the `temp` variable. – Rakib May 02 '14 at 06:37
  • Also, on Windows, there is a carriage return character right before the line feed character, which means that the "insertion pointer" moves to the start of the line in the console, which can lead to interesting things in the printout. Better use `getline()` and then concatenate strings with `std::endl` instead of ending them with `'\n'`. – Tobias Wärre May 02 '14 at 08:34

2 Answers2

0

The entire read part of the code can be simplified by the following:

while ( getline( txt, temp ) )
{
    cout << temp;
    Line line(temp);
    //row.printLine(); what is this?
    line.printLine(); // ???
    fullText.push_back(line); //???
    cout<< "line done \n";
}
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
  • Thanks. This cleans up the code a lot. I feel like a bit of a twat for not using getline in the firstplace. However it does not fix my problem. Now it outputs: line 1 line 1 line done destroyed. Perhaps its an issue with my destructor? ~Line(){ delete &lineChars; cout << "destroyed"; } – user3593486 May 02 '14 at 07:26
  • Dont worry, the issue was with my destructor. For some reason it wont let me edit my comment though. – user3593486 May 02 '14 at 07:33
-1

You can get all the lines using :

while(!txt.eof()){
    char buffer [256] ;
    txt.getline(buffer , sizeof(buffer) ) ; // default delimiter : '\n'
    if(!txt.fail()){
        //todo
    }
}
ColonelMo
  • 134
  • 1
  • 9