First Last Next and Previous

This is a discussion on First Last Next and Previous within the C Programming forums, part of the General Programming Boards category; Very frustated person needs help to create a menu which will read the first last next and previous line from ...

  1. #1
    Unregistered
    Guest

    First Last Next and Previous

    Very frustated person needs help to create a menu which will read the first last next and previous line from a text files

    Anyhelp will be much appreciated

    cheers

    Bazza

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Hint: check the manual pages for fseek, fgetpos and fsetpos

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Hint: check the manual pages for fseek, fgetpos and fsetpos
    Hint: This is more difficult than you think because the file positioning functions are very restricted for text files.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Prelude
    >Hint: check the manual pages for fseek, fgetpos and fsetpos
    Hint: This is more difficult than you think because the file positioning functions are very restricted for text files.

    -Prelude
    You're right, sorry Bazza.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    I might be wrong on this but can't you just read the text file, count the number of lines you have and keep track of where you currently are with an int. If you store the file in an array it should be pretty easy to navigate back and forward. I am at work now so I can't really produce any code.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I might be wrong on this but can't you just read the text file,
    >count the number of lines you have and keep track of where
    >you currently are with an int.
    That is one possibility, though a linked list would really be easiest. You would have a pointer to the first and last nodes, and if it were a double linked list then retrieving the previous and next lines would be child's play.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    >>That is one possibility, though a linked list would really be easiest. You would have a pointer to the first and last nodes, and if it were a double linked list then retrieving the previous and next lines would be child's play. <<

    Sounds like something that will work and not be complicated at all. And a doubly linked list sounds cleaner then my first thought about an array
    .

  8. #8
    Unregistered
    Guest
    Thanks this actually relates to the chocolate bannanas topic I got my code to read all the lines one after the other. But I needs to navigate.


    i need also to identify each field individually

    surname,name,id,DOB

    heres the code but this is pants


    #include <stdio.h>
    main()
    {
    char line[40];
    FILE *inp_file;

    inp_file = fopen ("records.dat","r");

    }

    while (inp_file == NULL);
    fgets (line, 40, inp_file);
    while (feof (inp_file)==0)
    {
    printf ("%s",line);
    fgets (line,40,inp_file);
    getchar() ;

    }
    fclose (inp_file);
    }

Popular pages Recent additions subscribe to a feed

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