Thread: More Reading in problems

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    16

    More Reading in problems

    Ok I'm having problems reading in a file again. Its the same CD Database but I decided to add a track listing to it. So, since different cds have different numbers of tracks, I added a loop in there to read in the tracks until it reads in a string that I compare to break it out of the loop. However, everything seems to be working fine except I get a segmenation fault. I tried messing around with cout's and found that everything works fine but it wont print my last cout once its supposed to break out of the loop once it reaches the end of the file, meaning it most not be reaching the end of the file.

    I don't get it... Here is the code:

    Code:
    int main()
    {
      ifstream infile;
      char* line = new char[512];
      char* temp = new char[100];
      char* garbage = new char[50];
      char* artist = new char[50];
      char* title = new char[50];
      char* year = new char[50];
      char* song1 = new char[50];
      char test[7] = "------";
    
      infile.open("databaseFile.txt");
    
      while ( infile.peek() != EOF )
        {
          infile.getline(garbage,100);
    
          infile.getline(line,512);
    
          temp = strtok(line, " ");
          strcpy(garbage,temp);
    
          temp = strtok(NULL, "\n");
          strcpy(artist, temp);
    
          cout << "Artist: " << artist << endl;
    
          infile.getline(line,512);
    
          temp = strtok(line, " ");
          strcpy(garbage,temp);
    
          temp = strtok(NULL, "\n");
          strcpy(title,temp);
    
          cout << "Title: " << title << endl;
    
          infile.getline(line,512);
    
          temp = strtok(line, " ");
          strcpy(garbage,temp);
    
          temp = strtok(NULL, "\n");
          strcpy(year,temp);
    
          cout << "Year: " << year << endl;
    
          infile.getline(line,512);
    
          //reading in Track Lising:
          temp = strtok(line, "\n");      strcpy(garbage,temp);
    
    
          do
            {
              infile.getline(line,512);
    
              temp = strtok(line, "\n");
              strcpy(song1,temp);
    
              cout << "Printing out song: ";
              cout << song1 << endl;
    
            } while ( strcmp (song1,test) != 0 );
    
          //read in blank line
          infile.getline(garbage,100);  
    
        }
    
      cout << "After breaking out of reading file loop" << endl;
    
    }
    This is the databaseFile.txt:
    Code:
    --CD--
    Artist: Outkast
    Title: ATLiens
    Year: 1996
    Track Listing:
          1. You May Die (Intro)
          2. Two Dope Boys (In a Caddilac)
          3. ATLiens
          4. Wheelz of Steel
          5. Jazzy Belle
          6. Elevators (Me & You)
          7. Ova Da Wudz
          8. Babylon
          9. Wailin'
          10. Mainstream
          11. Decatur Psalm
          12. Millenium
          13. E.T. (Extraterrestrial)
          14. 13th Floor/Growing Old
          15. Elevators (ONP 86 Mix)
    ------
    
    --CD--
    Artist: Outkast
    Title: Aquemini
    Year: 1998
    Track Listing:
          1. Hold On Be Strong
          2. Return of the 'G'
          3. Rosa Parks
          4. Skew it on the Bar-B
          5. Aquemini
          6. Synthesizer
          7. Slump
          8. West Savannah
          9. Da' Art of Storytellin (Part 1)
          10. Da' Art of Storytellin (Part 2)
          11. Mamacita
          12. SpottieOttieDopalicious
          13. Y'All Scared
          14. Nathaniel
          15. Liberation
          16. Chonkyfire
    ------

    It wont print the last cout that says After breaking out of reading file.

    Anyone got any ideas?

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Does the program just end? Or does it exit with some sort of error?

    Also, you should free all that memory you allocated at the end of the program. Try running it through a debugger and see what's going on with the variables.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    No it doesnt end at all. It has a segmentation fault before it breaks out of the reading in file loop:
    Code:
    while ( infile.peek() != EOF)
    It never prints that last cout statement outside the loop.



    I'm editing because I tried to check all the variables once it breaks outside the loop as you suggested. Comparing it to what the variables are before the loop I don't see anything. I don't think it's trying to strtok a NULL string.

    Code:
    Before entereing do while loop
    Garbage: Track Listing:
    Temp: Track Listing:
    line: Track Listing:
    Code:
    After loop
    Garbage: Track Listing:
    Temp: ------
    line: ------
    song1: ------
    test: ------
    Last edited by swayp; 01-27-2005 at 01:14 AM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    I was playing around with different kind of loops besides the do-while and my conclusion is I think the do/while (and when I try to convert it to a for and while loop) it runs one more time then I want it to.

    Does anyone see that?

    I don't know if thats causing the seg fault.

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Hey swayp, check out my latest post on your last thread here:
    http://cboard.cprogramming.com/showthread.php?t=61013
    I hope that helps broaden your options

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    as I said before, you're probably passing a NULL pointer to strcpy(). try checking the return value of strtok() before calling strcpy().
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>it runs one more time then I want it to.

    This sounds like a common problem when looking for end of file as the conditional of a loop. I'd try this:
    Code:
    while (infile.getline(garbage,100))
    {
      infile.getline(line,512);
      //etc.
    instead of this:
    Code:
    while ( infile.peek() != EOF )
    {
      infile.getline(garbage,100);
      infile.getline(line,512);
      //etc.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  2. Huge problems in Winsock recv!
    By sirSolarius in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-11-2004, 09:34 AM
  3. Reading from file problems
    By highlands in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2004, 12:50 PM
  4. Reading in the last few characters of a file problems
    By dfb78 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2002, 06:56 PM
  5. problems reading from a text file
    By korbitz in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 06:11 PM