Hi,

I am getting a segmentation fault at the following line in the code. I am reading a file using ifstream. The file contains some initial junk data followed by useful data. I wish to read and discard the junk data from the file, before I read the useful data. When i read the following string "cls" i know that i have read all the junk data and now i will read the useful data.

I am using "compare" operator to compare the value read from the file and "cls". I am getting a segmentation fault at line number 4. Following is my code:

string junk;
      string junk1 = "cls";
      inFile >> junk;
      while(!(junk.compare(junk1)))
      {
               inFile >> junk;
      }

I am unable to figure the cause. any help is appreciated.

Thanks

Dani AI

Generated

A segmentation fault on the line that compares strings is very rarely caused by std::string::compare itself. More often the root causes are: the input file was never opened; the loop logic becomes inverted so extraction keeps running into EOF/fail state; or there is unrelated memory corruption elsewhere in the program. In this thread posted the original loop, pointed out the logic inversion, and suggested guarding the read with the extraction — both directions are useful, but additional safety checks are needed to avoid reading past the useful data or leaving the stream in a bad state.

A safer pattern is to read deterministically (line-by-line if the file has mixed junk) and stop when the sentinel is found. For example:

std::ifstream inFile("data.txt");
if (!inFile) {
    std::cerr << "failed to open file\n";
    return;
}

std::string line;
while (std::getline(inFile, line)) {
    // trim or normalize 'line' if needed
    if (line == "cls") {
        break; // delimiter found; subsequent reads are useful data
    }
}
// check inFile state and continue reading useful data

Notes and troubleshooting:

  • Verify inFile.is_open() and test stream state (if (!inFile)) before doing comparisons.
  • Token extraction (operator>>) and line reads (std::getline) behave differently — pick the one that matches the file format and trim whitespace when needed.
  • If the segfault persists, it is often caused by memory corruption elsewhere. Run under a debugger or memory checker (gdb, valgrind) to capture a backtrace and locate the true faulting site.

Reference for exact std::string::compare semantics: std::string::compare.

Recommended Answers

All 3 Replies

I don't see why it's a segfault, but you are using compare incorrectly. Remember that it returns 0 if the strings are equal, so you want to keep looping while it is not 0. You are doing the opposite, looping as long as the string IS "cls". You could just use != if you want.

do {
    inFile >> junk;
} while (junk != junk1);

Hi,

I have tried this also:

while((junk.compare(junk1)) != 0)
{
inFile >> junk;
}

Still getting a segmentation fault. Am i using compare correctly now?

try this: the compare function isn't needed.

string junk;
      string junk1 = "cls";
      while( inFile >> junk  && junk != junk1)
             ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.