pls help

This is a discussion on pls help within the C Programming forums, part of the General Programming Boards category; i want to write a structure to a file,but i cant do that Code: struct rec { int record; char ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    pls help

    i want to write a structure to a file,but i cant do that

    Code:
    struct rec {
              int record;
              char name[30];
              char cost[15];
    }
    struct rec store[100];
    
    int main(void) {
          int num;
          char name[30]; 
          FILE *fp;
          printf("enter record number:");
          scanf("%d",&num);
          scanf("%s",&name      
          strcpy(store[num-1],name);
          ...
          fwrite(store[num-1],sizeof(struct rec),1,fp);
          }
    is that true to create structure array in a file?


    if i want to read data:
    Code:
    fseek(fp,(num-1)*sizeof(struct rec),SEEK_SET);
    fread(store,sizeof(struct rec),1,fp);
    ...
    is this way true?
    Last edited by condorx; 06-05-2002 at 06:23 AM.

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    You missed the &
    Code:
    fwrite(&store[num-1],sizeof(struct rec),1,fp);
    It is possible to read the whole array in one go, you would require to:
    Code:
    fread(store, sizeof(store), 1, fp);
    Also, check this line in your code:
    Code:
    strcpy(store[num-1],name);
    
    shold be
    
    strcpy(store[num-1].name,name);
    One more error, you are missing the ; in your struct declaration;

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    ... and remove the & in the next line:
    Code:
    scanf("%s",&name);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 07:47 AM
  2. i dont know what to do. pls. help!!!!
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 03-14-2002, 02:24 PM
  3. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 09:46 AM
  4. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 07:46 PM
  5. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 02:05 PM

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