More Reading in problems

This is a discussion on More Reading in problems within the C++ Programming forums, part of the General Programming Boards category; Ok I'm having problems reading in a file again. Its the same CD Database but I decided to add a ...

  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 12: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:
    Reading in File Help
    I hope that helps broaden your options

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    as I said before, you're probably passing a NULL pointer to strcpy(). try checking the return value of strtok() before calling strcpy().
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  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, 11:50 AM
  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, 05:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21