Trouble displaying records using setiosflags and linked list

This is a discussion on Trouble displaying records using setiosflags and linked list within the C++ Programming forums, part of the General Programming Boards category; hello all, am doing an assignment to store records in a text file. i have successfully done the writing part ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    19

    Trouble displaying records using setiosflags and linked list

    hello all,
    am doing an assignment to store records in a text file. i have successfully done the writing part to the file but i got trouble reading from the file in the proper format.
    when savin the records to the file i used setiosflag to save the records in kind of tabulated form. i used also used linked list.
    here is sample of the code that saves the records to a file
    Code:
        contact *temp;
        temp = conductor;
    
        ofstream extFile("Database.txt", ios::app);
        do
        {
            extFile << setiosflags(ios::left) << setw(20) << temp -> lastname
            << setw(5) << temp -> initial << setw(20) << temp -> firstname
            << setw(25) << temp -> street << setw(20) << temp -> city
            << setw(20) << temp -> state << setw(25) << temp -> country
    	<< setw(20) << temp -> telephone << setw(25) << temp -> email << endl;
            temp = temp -> next;
        } while (temp != NULL);
    Code used to read from file:
    Code:
        for (int i = 0; !extFile.eof(); i++)
        {
            list = new contact;
            extFile >> list->lastname;
            extFile >> list->initial;
            extFile >> list->firstname;
            extFile >> list->street;
            extFile >> list->city;
            extFile >> list->state;
            extFile >> list->country;
            extFile >> list->telephone;
            extFile >> list->email;
    
            list->next=NULL;
            if (p == NULL)
            {
                p = list;
            }
    
            else
    
            {
                temp_ = p;
    
                while (temp_ ->next != NULL)
                {
                    temp_ = temp_->next;
                }
    
                temp_->next = list;
            }
        }
    the variable street consists several strings. so i basically have troubles displaying records after street as the linked list become messed up.
    is there a way i can set the predefined number of characters to be read from the file for each record to be stored in the linked list.

    also that only the last record is displayed. thank you in anticipation

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Code:
    extFile >> noskipws >> setw(whatever) >> thing->street;
    It's always a good idea to restore things the way they were before:
    Code:
    extFile >> skipws;
    setw isn't sticky but skipws/noskipws is.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    @whiteflags

    i did what u mentionned above. the program displays nothing. output is null and i get segmentation fault

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > p = list;
    p is the apparent head of the list in one function.

    > temp = conductor;
    And this is the apparent head of the list in another function.

    How do you get from one to the other?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    @salem
    i corrected the linked list head

    but i stil get segmentation fault when i applied whiteflags code

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Lists can be empty, which is why the normal loop is this.
    Code:
        while (temp != NULL)
        {
            extFile << setiosflags(ios::left) << setw(20) << temp -> lastname
            << setw(5) << temp -> initial << setw(20) << temp -> firstname
            << setw(25) << temp -> street << setw(20) << temp -> city
            << setw(20) << temp -> state << setw(25) << temp -> country
    	<< setw(20) << temp -> telephone << setw(25) << temp -> email << endl;
            temp = temp -> next;
        }
    Also, just saying you've done something isn't the same as showing us that you've done it properly.
    Post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i want write contact records with linked list
    By captain_turkiye in forum C Programming
    Replies: 72
    Last Post: 05-15-2011, 05:17 PM
  2. Trouble with linked list
    By mikeman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2010, 10:10 PM
  3. Replies: 2
    Last Post: 11-08-2006, 11:29 AM
  4. Linked list trouble
    By sand_man in forum C Programming
    Replies: 2
    Last Post: 02-08-2005, 12:14 PM
  5. Having trouble with Linked List
    By hkmixxa in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2004, 03:25 AM

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