Thread: Translating linked lists

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    7

    Translating linked lists

    Heres the problem I'm trying to take a file created from a previous program and add it to a link list, but it does not seem to make through the translator portion. It will go into a loop.
    Any sugestions will be appreciated.
    Alpha22

    Code:
    int translateBudrec()
    {
       int numErrors = 0;       // holds return value of whether an error occurred
    
       BudrecType5 inRec5;      // this is a record to hold lab 5 data 
       BudrecType6 newRec6;    //this is a record to hold lab 6 data
    
       string fileName5;
       string fileName6;   
        
        cout << "Enter the lab5 binary file to read:" << flush;
        cin >> fileName5;
        inFile.open(fileName5.c_str(), ios::binary); 
        
        cout << "Enter the lab6 binary file to create:" << flush;
        cin >> fileName6;
        outFile.open(fileName6.c_str(), ios::binary);  
     
        if (inFile && outFile){
          
          inFile.read(reinterpret_cast<char *>(&inRec5), sizeof(inRec5));
          cout << endl << "The rec 5 amount is: " << inRec5.catAmts << endl;
          while (!inFile.eof()) {
            
            strcpy(newRec6.catName, inRec5.catName);
            newRec6.catAmts = inRec5.catAmts;
            newRec6.catPcts = inRec5.catPcts;
            newRec6.nextRec = NULL;  
            
            cout << "The rec 6 amount is: " << newRec6.catAmts << endl;
    
           //after this line program goes into a loop repeating the rec 6 amount.   
    
        outFile.write(reinterpret_cast<char *>(&newRec6), sizeof(newRec6));
            } // end while not end of file
          } else {
            cout << "File : " << fileName5.c_str() << " does not exist" << endl;
            numErrors = 1;
        }
        inFile.clear();
        outFile.clear();
        inFile.close();
        outFile.close();
    
        return numErrors; 
    
    }//end translateBudrec

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    Im not exactly sure what you are trying to do but if you are trying to create a linked list you need to allocated new memory every time you read in a new record and have the previous record link to the next one. Currenlty you are just use one record and overwriting the information in that record evey time you read a line in the file.
    Last edited by dnysveen; 04-30-2006 at 05:52 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You aren't reading anything inside the loop. Your call to inFile.read happens before the loop starts, so the loop goes on forever never reading anything else.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    Quote Originally Posted by Daved
    You aren't reading anything inside the loop. Your call to inFile.read happens before the loop starts, so the loop goes on forever never reading anything else.
    Good point. I missed that one. Even if he was reading from the file he would not have a linked list.

    You also do not have a head pointer for your linked list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM