0

I have a courses0.dat file with a single 4 on line 1 that I want to extract with my ifstream program:

void processEnrollments (std::istream& courseFile);

int main (int argc, char** argv)
{

// Take input and output file names from the command line
ifstream coursesIn (argv[1]);

return 0;
}

void processEnrollments (istream& courseFile)
{
int numCourses;
courseFile >> numCourses;

cout << numCourses;

// Create the arrays we need
//!! Insert your code here
}

when I run

program courses0.dat

my test is cout'ing a 32767 instead of a 4. My .dat file is in the same directory as my executable.

any clue as to what is going on?

thanks

  • Given that we don't even know what's in courses0.dat, how should we be able to tell what's wrong... – Mats Petersson Jan 30 '13 at 15:07
  • 1
    I do not see how you get any output because `processEnrollments` is not even called. – Nobody moving away from SE Jan 30 '13 at 15:08
  • Yeah, well, you're relying too much on `operator >>` to do "the right thing". Just consider extra gibberish in the file, blank lines, etc... Go for `std::getline` and start from there. – Mihai Todor Jan 30 '13 at 15:08
  • My first sentence was: "I have a courses0.dat file with a single 4 on line 1 that I want to extract with my ifstream program:" –  Jan 30 '13 at 15:09
  • @nobody I'm sorry, I mean to include: processEnrollments (coursesIn); In my main; it's in my program, I just forgot to include it in the post. –  Jan 30 '13 at 15:10
  • @MihaiTodor I am not allowed to edit the program except after where it says "insert your code here" –  Jan 30 '13 at 15:11
  • @Joey In this case, you might consider it useful to go ask your teacher for help. On a more serious note, just open courses0.dat in a smart text editor, let's say Notepad++, make all characters visible and see exactly what it has inside. You may even encounter some strange issues with UTF8, if it is in that format... – Mihai Todor Jan 30 '13 at 15:14
  • Your code has no error checking or reporting, which makes it very hard to tell what's wrong. Did `courseFile >> numCourses` succeed? Was `argc` 2? Did opening `coursesIn` succeed? Who knows. – David Schwartz Jan 30 '13 at 15:19

1 Answers1

0

Check for errors! Try to use the full path to the file when you pass it as an argument.

My guess is courseFile >> numCourses; fails because ifstream coursesIn (argv[1]) doesn't find or can't access the file.

Try this

if( courseFile >> numCourses )
    cout << numCourses;

Does it output anything then?

Alex
  • 7,728
  • 3
  • 35
  • 62
  • 1
    Thanks for being helpful :) Your reply was the first who actually didn't sound they were talking down to me. I appreciate it. My problem was xcode. I compiled using g++ from the terminal, all in the same directory, and it seems to all be going fine. –  Jan 30 '13 at 15:43