#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    List and structure problem


    How can I modify an entry in a list ? I saw many using fseek, but i didn't understand how to use it .
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    That's probably because fseek is for reading files, not traversing linked lists.

    The idiom for traversing a linked list is
    Code:
    temp = head;
    while ( temp != NULL ) {
        // do your thing
        temp = temp->next;
    }
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by salem
    That's probably because fseek is for reading files, not traversing linked lists.

    The idiom for traversing a linked list is
    Code:
    temp = head;
    while ( temp != NULL ) {
        // do your thing
        temp = temp->next;
    }
    what head means here?

    "temp = temp->next;" that means using pointers , am i right? and if yes,then where ?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    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
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,089
    Rep Power
    2222
    Or perhaps path_finder5 should clarify what kind of a list he's talking about.

    Good old-fashioned "roll your own" linked list in C or C++? Where you have a pointer, usually named head, pointing to the first node in the list, and then each node has a pointer that points to the next node in the list.

    A C++ list using the Standard Template Library (STL)?

    A C# list?

    path_finder5, an over-general question usually results in confused general answers that do not touch on what you want to know. If you could be a bit more specific, then that should help things along here.

    PS

    Or, since you say you've seen others using fseek, which only applies to file I/O, is this "list" simply data in a file and not in a particular type of data structure called a "list"? You need to be more careful with your terminology, since words like "list" have very definite and distinct meanings.

    If it's a file, then is a text file or a binary file? fseek works best with binary files, whereas with a text file you would need to read the file one line at a time.

    If it's a text file and you want to change one of the lines of text, then follow this procedure:
    1. Open the text file for input and create a new uniquely-named text file for output.
    2. Read in a line from the input file.
    3. If it's not the line you're looking for, then simply writing it to the output file and goto Step 2.
    4. But if it is the line you're looking for modify it and write the modified line to the output file. Continue to Step #5.
    5. For all the remaining lines in the input file, read the line and write it to the output file. When you're done, go to Step #6.
    6. Close both files.
    7. Delete the input file.
    8. Rename the output file with the old input file's name.
    9. Done.
    Last edited by dwise1_aol; May 13th, 2013 at 02:58 PM.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    fseek's often useful to help modify a randomly accessed file.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by dwise1_aol
    Or perhaps path_finder5 should clarify what kind of a list he's talking about.

    Good old-fashioned "roll your own" linked list in C or C++?

    A C++ list using the Standard Template Library (STL)?

    A C# list?
    it's a C linked list.

    fseek's often useful to help modify a randomly accessed file.
    that's exactly what i need to do , for example if i have many fields in the list then i choose a specific field and modify it.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,089
    Rep Power
    2222
    Originally Posted by path_finder5
    it's a C linked list.

    that's exactly what i need to do , for example if i have many fields in the list then i choose a specific field and modify it.
    Please make up your mind!

    Is it a C linked list? Or is it a file?

    If it is a C linked list, then you have created that list. To find the node that you want to modify, you traverse the list starting at the head and move node by node until you find the node you're looking for.

    fseek will not do that! fseek is for files!

    Is it a linked list or is it a file?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by dwise1_aol

    Is it a linked list or is it a file?
    list
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,089
    Rep Power
    2222
    Then you have the procedure you need. Start at the list's head and traverse it node-by-node until you find the node you want.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by dwise1_aol
    Then you have the procedure you need. Start at the list's head and traverse it node-by-node until you find the node you want.
    ok,thanks

IMN logo majestic logo threadwatch logo seochat tools logo