Like Tree1Likes
  • 1 Post By anduril462

write to file .. pointer to structure array

This is a discussion on write to file .. pointer to structure array within the C Programming forums, part of the General Programming Boards category; I am trying to learn how to write from a pointer to a structure array to a file .. this ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    54

    write to file .. pointer to structure array

    I am trying to learn how to write from a pointer to a structure array to a file ..
    this what I have so far.

    this is a learning exercise for me later I want to write a structure holding different variable types and write a file.

    my overall objective is to have a file of the screen size .. image size and location of my mandelbrot set program so I have a record of where I have been.

    any help appreciated thanks in advance al.

    Code:
    #include<stdio.h>
    //prototyping my structure
    struct foo{
      int array[5];
    };
    int main(){
      //creating a foo structure bar and assigning a pointer to it
      struct foo bar;
      struct foo *p;
      p = &bar;
    
      //creating a file pointer
      FILE *fp;
     
      //counter
      int c;
    
      //giving values to array elements in structure
      for(c = 0; c < 5; c++){
        p->array[c] = c;
      }
      //pinting out values .. checking
      for(c = 0; c < 5; c++){
        printf(" c = %i i = %i\n",c,p->array[c]);
      }
    
      //opening a file so I can write values to it
      fp = fopen("foobar.txt","wb");
      /*
        no idea what to put in here
      */
      //closing file
      fclose(fp);
      return;
    }
    Last edited by mad_muppet; 05-05-2011 at 11:57 AM. Reason: make readable

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    Well, f-open opens a file, f-close closes a file, I'll give you 3 guesses on what the write function is called.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    54
    what I have so far .. it will write out integers using the workaround p->array[c]+ 48 but Im pretty sure it will not work with floats I dunno how to do that .. I tried using fwrite but got a bunch of errors relating to p pointer not being the correct type

    again any help appreciated al.

    Code:
    #include<stdio.h>
    //prototyping my structure
    struct foo{
      int array[5];
    };
    int main(){
      //creating a foo structure bar and assigning a pointer to it
      struct foo bar;
      struct foo *p;
      p = &bar;
    
      //creating a file pointer
      FILE *fp;
     
      //counter
      int c;
      int ln;
      //giving values to array elements in structure
      for(c = 0; c < 5; c++){
        p->array[c] = c;
      }
      //pinting out values .. checking
      for(c = 0; c < 5; c++){
        printf(" c = %i i = %i\n",c,p->array[c]);
      }
    
      //opening a file so I can write values to it
      fp = fopen("foobar.txt","wb");
      ln = strlen(p);
      for(c = 0; c < 5; c++){
        fputc((p->array[c] + 48),fp);
      }
      /*
        no idea what to put in here
      */
      //closing file
      fclose(fp);
      return;
    }
    Last edited by mad_muppet; 05-05-2011 at 12:37 PM. Reason: changes in code

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You need to figure out if the written information should be ASCII or binary.
    You say you don't know how it will react with float. My guess is you may want fprintf(fp, "%f", variable);

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
      fp = fopen("foobar.txt","wb");
      ln = strlen(p);
      for(c = 0; c < 5; c++){
        fputc((p->array[c] + 48),fp);
      }
      /*
        no idea what to put in here
      */
      //closing file
      fclose(fp);

    Wellllll... you could try...
    Code:
      fp = fopen("foobar.txt","wb");
      fwrite(foo,sizeof(foo),1,fp);
      fclose(fp);
    Just fwrite() the struct to disk and then fread() it back in later...

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    54
    thanks for the replies .. I have a solution that appears to do what I want so far ..

    Code:
    #include<stdio.h>
    //prototyping my structure added second variable and will need more later
    struct foo{
      int array[5];
      double array2[5];
    };
    int main(){
      //creating a foo structure bar and assigning a pointer to it
      struct foo bar;
      struct foo *p;
      p = &bar;
    
      //creating a file pointer
      FILE *fp;
      
      //counter
      int c;
      
      //giving values to array elements in structure
      for(c = 0; c < 5; c++){
        p->array[c] = c;
        p->array2[c] = c - 1.2;//cheap way of making a double
      }
      //pinting out values .. checking
      for(c = 0; c < 5; c++){
        printf(" c = %i i = %i\n",c,p->array[c]);
        printf(" c = %i f = %f\n",c,p->array2[c]);
      }
      //opening a file so I can write values to it
      fp = fopen("foobar.txt","wb");
      
      for(c = 0; c < 5; c++){
        fprintf(fp,"%i\n",p->array[c]);
        fprintf(fp,"%f\n",p->array2[c]);
      }
      fclose(fp);
      return;
    }
    //yay!! thanks for the help
    trying CommonTaters suggestion
    Code:
    #include<stdio.h>
    //prototyping my structure
    struct foo{
      int array[5];
    };
    int main(){
      //creating a foo structure bar and assigning a pointer to it
      struct foo bar;
      struct foo *p;
      p = &bar;
    
      //creating a file pointer
      FILE *fp;
      
      //counter
      int c;
      
      //giving values to array elements in structure
      for(c = 0; c < 5; c++){
        p->array[c] = c;
      }
      //pinting out values .. checking
      for(c = 0; c < 5; c++){
        printf(" c = %i i = %i\n",c,p->array[c]);
      }
      //opening a file so I can write values to it
      fp = fopen("foobar.txt","wb");
      fwrite(foo,sizeof(foo),1,fp);
      fclose(fp);
      return;
    }
    I get an error
    cc pointer.c
    pointer.c: In function ‘main’:
    pointer.c:28:10: error: ‘foo’ undeclared (first use in this function)
    pointer.c:28:10: note: each undeclared identifier is reported only once for each function it appears in

    so not sure how to proceed using fwrite atm ..

    thanks again for the help so far .. if anyones interested in showing me my errors with the fwrite feel free .. al.
    Last edited by mad_muppet; 05-05-2011 at 03:17 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,639
    You're on the right track, but you just copied Tater's suggestion without really looking at it or thinking about it. For shame!

    For starters, read up on the fwrite documentation (Google will help you). Then, consider that foo is the name of a struct, not a variable. Thus, foo does not refer to any memory location where data is stored. Also, to refer to that type correctly, you need to say struct foo. So pass in the address of the data you want to write and give sizeof the proper parameter and you should be fine.
    quzah likes this.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mad_muppet View Post
    thanks for the replies .. I have a solution that appears to do what I want so far ..

    Code:
      //opening a file so I can write values to it
      fp = fopen("foobar.txt","wb");
      fwrite(foo,sizeof(foo),1,fp);
      fclose(fp);
      return;
    }
    I get an error
    cc pointer.c
    pointer.c: In function ‘main’:
    pointer.c:28:10: error: ‘foo’ undeclared (first use in this function)
    pointer.c:28:10: note: each undeclared identifier is reported only once for each function it appears in

    so not sure how to proceed using fwrite atm ..

    thanks again for the help so far .. if anyones interested in showing me my errors with the fwrite feel free .. al.
    What did you name the variable holding your foo struct... bar... right?
    And although it is my error (sorry) you couldn't figure iout to try bar instead?

    Code:
      fwrite(bar,sizeof(bar),1,fp);
    So, there's an object lesson for both of us...
    I have to learn to proofread more carefully
    You've got to stop scoop and poop coding and try figuring these things out on your own.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    54
    ok I believe I am writing the file correctly ..

    however after several hours searching I have yet to find a way to print my file out to console to check it is correct .. I cannot read it using an editor and do not think it is readable in an editor.

    any suggestions on how to read my file written to disk welcome .. thanks for the help so far ..

    I think that using fprintf will do what I am asking but have got the bug of trying to find out how fwrite works ..

    sleep is catchin up with me so again thanks for the help so far .. al.
    Code:
    #include<stdio.h>
    //prototyping my structure
    struct foo{
      int array[5];
    };
    int main(){
      //creating a foo structure bar and assigning a pointer to it
      struct foo bar;
      struct foo *p;
      p = &bar;
    
      //creating a file pointer
      FILE *fp;
    
      //counter
      int c;
    
      //giving values to array elements in structure
      for(c = 0; c < 5; c++){
        p->array[c] = c;
      }
    
      //pinting out values .. checking
      for(c = 0; c < 5; c++){
        printf(" c = %i i = %i\n",c,p->array[c]);
      }
    
      //opening a file so I can write values to it
      fp = fopen("foobar.txt","w");
      fwrite(&bar,sizeof(bar),1,fp);
      //writing file to console as a check .. hmm not sure I think Im
      //reading the structure again here ..
      for(c = 0; c < 5; c++){
        fread(&bar,sizeof(bar),1,fp);
        printf("%d\n",p->array[c]);
      }
      fclose(fp);
      return;
    }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,639
    Not quite. You're writing the entire bar object in one go, but read it back 5 times. Don't put your fread in a loop. You also have to rewind the file pointer before you go reading data back. Think of it like a tape recorder (actually, some of the first non-volatile storage was on reels of magnetic tape). If you talk into the mic while recording, then hit stop and hit play, you wont hear your voice. You have to rewind first, to right before the recording started, then hit play. You can look into the fseek function for details on this. You should check the return value of all your file operations to make sure they succeeded. Your documentation/Google should tell you what each one returns on success and failure. Also, fread/fwrite are for binary data. They store the data on disk in the same manner the computer stores it in memory. You could read data back into a separate struct and compare them if you wanted to check if your read and write code worked. If you want your file to be human-readable, you probably don't want this, you probably want to stick with fprintf/fscanf. Or, you could examine your file with a hex editor to verify that everything is stored correctly.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you want to read back the file without closing and reopening it you need to do it like this...

    Code:
      fp = fopen("foobar.txt","w+");
      fwrite(&bar,sizeof(bar),1,fp);
    
       rewind(fp);  // set pointer to beginning
    
       fread(&bar,sizeof(bar),1,fp);
     
       for(c = 0; c < 5; c++)
         printf("%d\n",p->array[c]);
    
      fclose(fp);
      return 0;
    You only read it once because the struct is saved as a block of memory... your array is inside the struct so the whole thing is written and read at once.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    54
    thanks all for the help .. al.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to write data from file to structure, This case is different.
    By TanzeelurRehman in forum C Programming
    Replies: 6
    Last Post: 12-22-2010, 11:34 PM
  2. Replies: 37
    Last Post: 12-13-2007, 03:40 PM
  3. Replies: 5
    Last Post: 03-18-2006, 11:25 AM
  4. Structure Dynamic with File Write
    By k4z1nh0 in forum C Programming
    Replies: 1
    Last Post: 06-25-2005, 12:46 PM
  5. pointer to an array in a structure
    By samual_van in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2005, 11:51 AM

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