[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Utter Stupidity

Posted on 1997-02-28
1
Medium Priority
?
228 Views
Last Modified: 2010-04-10
I feel utterly stupid asking this question, especially since I have been programming in C for almost 3 years now;  but it's really bugging me.  I am using G++ under FreeBSD 2.1.6; here is the main source to the loop:

  while (!feof(infile1)) {
    fgets(s1, 4096, infile1);
    fgets(s2, 4096, infile2);
    if (strlen(s1)==strlen(s2)) {
      if (strcmp(s1, s2)==0) fputs(s1, outfile);
    }
  }

All of the files are opened correctly, the variables are typecast correctly, etc -- it's nothing to do with simple mechanics. Unfortunately, it seems like this loop NEVER exits.  I've monitored feof(infile) to see when the EOF is set and it's detecting it; however, the loop never exits.  As a result, the program crashes with "Segmentation Fault (Core Dump)".  It doesn't lock the computer and the output file is always 100% correct -- it just doesn't ever want to quit.

The data I am working with is just two simple text files.  This loop is supposed to compare the two files (in text mode), line by line, and write any matching lines to the output file.
0
Comment
Question by:hooptie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
jos010697 earned 100 total points
ID: 1162268
For one thing, the logic of your program fragment is faulty,
i.e. the feof() function does not _predict_ whether or not
the next read would bump into an eof condition, it just
tells you if the last read encountered an eof condition.

If the fgets() function bumps into an eof condition (withouit
having read anything), it returns a NULL pointer value.

Something like the following would do the job for you:

while (fgets(s1, 4096, infile1) && fgets(s2, 4096, inflie2)) {
   /* process the lines read */

if (feof(infile1))
   if (!feof(infile2))
      /* infile1 was shorter ... */
/* etc. */

kind regards,

Jos aka jos@and.nl
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question