help with fseek and fwrite

This is a discussion on help with fseek and fwrite within the C Programming forums, part of the General Programming Boards category; can you explain to me what the two things do? for example code: using a struct clientData{ int acctNum; char ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    4

    help with fseek and fwrite

    can you explain to me what the two things do?


    for example code:

    using a struct clientData{
    int acctNum;
    char lastName[15];
    char firstName[10];
    float balance;
    };

    Code:
     
    
    while (client.acctNum!=0) {
      printf("Enter lastname, firstname, balance\n? ");
      scanf("%s%s%f", &client.lastName, &client.firstName, &client.balance);
      fseek(cfPtr, (client.acctNum-1) *sizeof(struct clientData), SEEK_SET);
      fwrite(&client,sizeof(struct clientData),1,cfPtr);
    printf"Enter account number\n?");
    scanf("%d", &client.acctNum);
    i understand fwrite writes a block of data to a file, but can you explain SEEK_SET, fseek, and why it's (client.acctNum-1)*sizeof(struct clientData) ? thanks in advance.

  2. #2
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    You use fseek to move the file pointer to a certain position in the file.
    It is (client.acctNum-1)*sizeof(struct clientData) because you want to move the pointer to the part of the file that is (client.acctNum-1) * the size of the struct from the beginning. SEEK_SET tells you to start at the beginning of the file. SEEK_CUR would tell you to start where the pointer is currently at. SEEK_END would make you start at the end. So if the client.acctNum is 1, it would put it at 0 * the size of the struct, which would be the beginning of the file.
    On thing though. I think, (I'm pretty sure) if you try to access a client.acctNum that would push the pointer past the end of the file, you will get a segmentation fault.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    so the pointer moves 1 space automatically with each run in the while loop?

    on the first run, where pointer points at lets say the struct array a[1] , i'm assuming the client.accountnum -1 places it in the struct array position a[0]. on the second entry of another person's name/acct number, ect.. does it add one to the struct array automatically? or is there a phrase below that does that. I hope i'm not utterly confused.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    i'm assuming structs are like block arrays, are they not?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Structures are collections of other variables all tied together nicely with whatever name you give them. They'll be probably padded to word boundaries by your compiler, so the number of bytes of every variable in the structure added together, may not be the sizeof the structure.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Let's see how your file pointer could be positioned.
    When you open the file (unless you open it in append which would be bad for this example), the file pointer would be pointing to 0 bytes past the beginning of the file. Let's just say, for the sake of example, the size of your struct is 35 bytes.
    Code:
    printf"Enter account number\n?");
    scanf("%d", &client.acctNum);
    while (client.acctNum!=0) 
    {
      printf("Enter lastname, firstname, balance\n? ");
      scanf("%s%s%f", &client.lastName, &client.firstName, &client.balance);
      fseek(cfPtr, (client.acctNum-1) *sizeof(struct clientData), SEEK_SET);
      fwrite(&client,sizeof(struct clientData),1,cfPtr);
    printf"Enter account number\n?");
    scanf("%d", &client.acctNum);
    }
    The fseek places the pointer (client.acctNum-1)*35 bytes past the beginning of the file (since you are using SEEK_SET).
    After you fwrite a struct to the file, the pointer will be 35 bytes from where it was.

    Quote Originally Posted by monkee
    so the pointer moves 1 space automatically with each run in the while loop?
    Well, I wouldn't say it like that. If the numbers you enter are in order 1,2,3,4,5... Then it the pointer would move 35 bytes each pass, only if you go in order though. I'm pretty sure you will get an error if you don't go in order, because you will likely try to point past the end of the list.

    Also, I don't know what you mean by struct array a[], that wasn't mentioned in the code you posted. The code you posted does not add anything to a struct array. What it does do is, write structures to a file.

    What I suggest you do is take out the fseek call. It will really cause problems, unless the file you are writing to already has a bunch of data written to it and you are overwriting data.
    If you take out the fseek, the fwrite will automatically place the pointer at the appropriate spot to write to. The user could enter any account number without pointing past the end of the file. Afterwards you could sort the file to get them in order.

    I hope this helps some.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Except, in the above example, you'd be using a binary file, and as such, it wouldn't probably be a good idea to use scanf in the way you use it. scanf as you use it would read until there was white space. If your string filled its entire buffer, then it would just merrily read along past it. Or, if your string was less than its buffer length, you'd start reading again, immediately past that point, which wouldn't be the start of the next string.

    You'd be best served fread-ing the entire structure in, and then pulling what you need from it. (In *nix, "b" is only around for compatability with old code, but the same flaw or problem would occur with you reading with scanf the way you've done here also.)

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  2. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  3. Replies: 2
    Last Post: 12-26-2008, 03:38 PM
  4. Image Processing Help Required
    By dantu1985 in forum C Programming
    Replies: 7
    Last Post: 12-31-2007, 08:30 AM
  5. Replies: 2
    Last Post: 04-03-2002, 08:59 AM

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