Results 1 to 5 of 5

Thread: Reading a file usine fseek() in C

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Australia
    Posts
    149

    Reading a file usine fseek() in C

    Hi,

    Ok i have this question,
    I am writing a program that stores information from a structure to a file (read and written in binary mode).

    One of the variables in the structure is a type int.

    What i want to do is open the file, use fseek() and set the origin to SEEK_END - (moves the indicator to the end of the file), then i want to go back in the file the sizeof() the structure.

    That is all fine and here is the code that i use.

    Code:
    if ( (fseek(fp, (1 * sizeof(struct data)), SEEK_END)) != 0)
    {
          fprintf(stderr, "Error using fseek()");
          exit(1);
    }
    Now the question that i want to ask is, if i read the data using fread(), does it read the file backwards (as in from the end of file to the start of file), or does it read it the normal way??

    Thanks for any help.

    -|- Hurgh -|-
    Email: [email protected]
    Website: http://www.hurgh.org/

    Unix, Linux, FreeBSD, OpenBSD, Solaris, Windows

    C, C++, PHP, VB6, ASP, VBScript, JavaScript

  2. #2
    jim mcnamara
    Guest
    In simple statements - you want to read the last record of the file.

    Assuming a file with at least one record---------------

    Code:
       #define ZERO 0
       int result;
       result = fseek(fp,ZERO,SEEK_END);
       // file pointer is on the last record, let's see where that is
       result = ftell(fp);
       result -= sizeof(struct data);  // calculate one back
       result = fseek(fp,result,SEEK_SET);
       // file pointer is now on first byte of last record
    fread() reads from the file pointer postion on further into the file. NEVER backwards.


    Okay?

  3. #3
    jim mcnamara
    Guest
    Rhetorical question --


    Why do questions like these look like stuff I used to assign students?

    Hmmm. I wonder.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Australia
    Posts
    149
    Well Jim,
    I will let you know that i am not a student, infact i am currently a Network Administrator and a CNE (if you know what that is).

    I am teaching myself C (i use to do a lot of VB programing but i was sick of the stupid runtime files and the slow programs), and i was just writing one of my first real programs and came accross this problem.

    I worked it out after a lot of testing and wasted time but thanks for replying ne way.

    Have a nice day

    -|- Hurgh -|-
    Email: [email protected]
    Website: http://www.hurgh.org/

    Unix, Linux, FreeBSD, OpenBSD, Solaris, Windows

    C, C++, PHP, VB6, ASP, VBScript, JavaScript

  5. #5
    jim mcnamara
    Guest
    We had a spate of college & high school students getting their homework, projects , and exams done by the programmers on the forum.

    Your question about the direction of a file read shouldn't come from a CNE or anybody who's programmed for a while - unless they are totally confused or are a student trying to get us to do what they were supposed to have learned.

    All native file reads for every computer language I've messed with for 40+ years are forward. In C you can ungetc to move the pointer backward - but you are not reading, just moving the file pointer.

    regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width