5 Replies - 467 Views - Last Post: 21 July 2011 - 05:17 PM Rate Topic: -----

#1 Malluce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 05-December 09

Seg fault when program finished reading files

Posted 21 July 2011 - 01:41 PM

I got into this problem that the program will have seg fault when I finish reading all the files.
The program suppose to read a bunch of files in the dir and parse them. After parse the data it will outputs to another file
However, when I finish doing everything (everything is working) I got this seg fault and created a core dump.
Does anyone know why? I have been googling around and most of the time people use FILE *file. I didn't use that so I don't think there's anything wrong with the pointers. I tried free and delete but it doesn't really do anything
Here is my code..I just ignore the output file part and replaced with cout << "yay" since it doesn't matter
for (int i = 0; i < numFiles; i++) {
     ifstream inFile;
     inFile.open (fileName);
     if (inFile.fail()) {
          break;
     } else {
          cout << "yay" << endl;
     }
     inFile.close();
}


Thanks for the help!

Is This A Good Question/Topic? 0
  • +

Replies To: Seg fault when program finished reading files

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: Seg fault when program finished reading files

Posted 21 July 2011 - 02:13 PM

What type of variable is fileName? A std::string or a C-string? What is the size of numFiles?

In the code provided I see nothing wrong, if fileName is a C-string. If fileName is a std::string then you must use the fileName.c_str() function to convert it to a C-string for the call to open.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Malluce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 05-December 09

Re: Seg fault when program finished reading files

Posted 21 July 2011 - 02:33 PM

Sorry..forgot to provide thoese
char fileName[30];
numFile is just an int..so doesn't matter..I'm just trying to make it generic
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: Seg fault when program finished reading files

Posted 21 July 2011 - 02:40 PM

So the code you posted looks correct as long as fileName is less than 30 characters long. You have initialized this variable, correct?

Since you are reusing the same file stream you should probably clear the stream state flags before you try to reopen the stream.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Malluce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 05-December 09

Re: Seg fault when program finished reading files

Posted 21 July 2011 - 02:50 PM

Everything works..it prints out the stuff I want. The only thing that's acting funny is it has this seg fault after everything gets printed out. What do you mean clear the stream state flags?
Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Seg fault when program finished reading files

Posted 21 July 2011 - 05:17 PM

I think you're going to post more code to get help. Alternatively, compile the code in debug mode and use a debugger to find exactly where it's seg faulting and use the information within the debugger to determine what is causing it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1