• Computers & Internet >
  • Programming & Design
  • Computers & Internet >
  • Programming & Design

C++ Segfault Error - Out of ideas!?

  • Follow publicly
  • Follow privately
  • Unfollow
I'm working on a c++ code project which requires the ability to read 1 file & write another simultaniously. I have created a class File (not the actual name but who cares) that ...show more
Best AnswerAsker's Choice
TempString is uninitialized, so when file.getline() tries to write where it points . . . segfault.

As far as why file.tellg() would segfault after fixing that (as you mentioned in your other question), I don't know, especially not seeing how line is declared (as an int perhaps?). Maybe you should check that file is still good() before callint file.tellg again? Although offhand I don't know how a getline call would cause it to no longer be good().

Asker's rating & comment

5 out of 5
Thank you very much (for both questions). If I ask the question a little bit later, I'll be sure to include the updated code.
  • 0
    0
  • Comment
C++ Segfault Error - Out of ideas!?
I'm working on a c++ code project which requires the ability to read 1 file & write another simultaniously. I have created a class File (not the actual name but who cares) that contains 1 ofstream object & 1 ifstream object. My class handles the classes by ensuring the good() & is_open() functions returns true before any operations occur. The ofstream object works flawlessly when i tested it. The ifstream however keeps raising a segmentation fault when i try 2 read the file. Any ideas? Please & thank you. Here's the function for getting code with the ifstream object:

char getline (unsigned int length = 100000, char delim = '\n')
{
if (file.good() && file.is_open())
{
preline = file.tellg();
char* TempString;
file.getline(TempString, length, delim);
line = file.tellg();
return *TempString;
}
else throw ConnectionError;
}
Sign In 

to add your answer

Who is following this question?

    %
    BEST ANSWERS
    Member Since:
    Points: Points: Level
    Total Answers:
    Points this week:
    Follow
     
    Unfollow
     
    Block
     
    Unblock