1

I'm having a problem reading in from a file, I feel disoriented and I cannot seem to find the error within my code(I'm sure it's a minor mistake because I've done this before) This a homework assignment. The assignment calls for the constructor to load in contents from a file. However, the program halts after reading in the first line.

tree::tree()
    {

    root = NULL;
    load();
}

int tree::load()
{
    ifstream inFile;

    definition anEntry;

    char title[TITLE], info[INFO];

    inFile.open("CS_terms.txt");

    if (inFile.is_open())
    {
        cin.get(title, TITLE, ':');
        cin.ignore(TITLE, ':');
        cin.get(info, INFO, '\n');
        cin.ignore(INFO, '\n'); 

        anEntry.createEntry(title, info);
        insert(anEntry);

        while (inFile.is_open() && !inFile.eof())
        {
            cin.get(title, TITLE, ':');
            cin.ignore(TITLE, ':');
            cin.get(info, INFO, '\n');
            cin.ignore(INFO, '\n'); 
            anEntry.createEntry(title, info);
            insert(anEntry);
        }
        inFile.close();
        return 1;
    }
    else
    {
        cout << "No File" << endl;
        return 0;
    }
}
some dude
  • 13
  • 4
  • 1
    Your root is NULL, and you are doing root->entry... wont work – Sniper Mar 15 '17 at 19:26
  • Sorry, it should be this anEntry.createEntry(title, info); insert(anEntry); – some dude Mar 15 '17 at 19:29
  • `while (inFile && !inFile.eof())` http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Mar 15 '17 at 19:30
  • Looks like you dont need that redundant bit before the while loop, because its already inside the while loop. That won't solve the problem, but will make it more straightforward. – Bango Mar 15 '17 at 19:35
  • Yeah, it still persists, I've updated my code slightly. and drescherjm, I understand that it is bad to use, however, it doesn't even step into the loop. Program stops at cin.get(title, TITLE, ':'); the very first read, doesn't step to next line of code – some dude Mar 15 '17 at 19:35
  • Totally get that Bango, I've just been following coding style from my instructor it's kind of a "safety precaution", apparently, according to her. – some dude Mar 15 '17 at 19:37
  • See if it steps into the loop when you take out the part outside the loop. Looks like you've narrowed down the problem to your cin.get() statements. – Bango Mar 15 '17 at 19:37
  • Bango, it opens the file, however still same problem persists. – some dude Mar 15 '17 at 19:42
  • I don't know why your prof wants you to put that bit before the while loop. Seems like the opposite of safety precaution to me. – Bango Mar 15 '17 at 19:44

2 Answers2

1

You should replace this line:

if (inFile)

by this

if (inFile.is_open())

and this:

while (inFile.is_open() && !inFile.eof())

by

while (inFile.good()) 
Rama
  • 3,222
  • 2
  • 11
  • 26
  • True, but it is open, because they are getting the first line. Why would the file close unless close() is being called? – Bango Mar 15 '17 at 19:32
  • The problem exists with the first attempt to read in from the file.. could my text file be corrupt? If so, how can I check? OH and btw I've had a similar issue in the past, when transferring files from windows to linux, the new line character is associated as \r\n in windows – some dude Mar 15 '17 at 19:39
  • @HaiNguyen use cmd command: `dos2unix CS_terms.txt` to pre process your text file in the linux console – Rama Mar 15 '17 at 19:44
1

try replacing cin.get() with inFile.get()

Bango
  • 971
  • 6
  • 18