Thread: binary file input and out

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    41

    binary file input and out

    ok so i am trying to make a program in which the user can input 5 values then , these values are to be stored in a binary file.

    Then I have to read the stored value from the binary file.

    However , i keep getting a random number totally unrelated to the inputs.

    here's my code please disregard the printf function it's just for testing the output.Also , note that i am only using data[].l and not the whole structure as a test .

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    #define N 100  // number of points
    #define TRUE 1
    #define FALSE 0
    #define EPSILON 1e-10
    #define BINFILE "file.bin"
    
    
    typedef struct
    {
    
    
       double l; // starting time
       double c; // ending time
       double v; // velocity
       double td; // time
       double pc; // altitude
    
    
    } INFO;
    
    
    int main()
    {
      INFO data[5];
      int i=0;
      int flag = FALSE;
      int ans;
      FILE *fp;
    
    
      while(i < 5 && flag==FALSE)
    {
        printf("Please enter the value for L:\n");
        scanf("%lf",&data[i].l);
        printf("Please enter the value for C:\n");
        scanf("%lf",&data[i].c);
        printf("Please enter the value for the battery voltage:\n");
        scanf("%lf",&data[i].v);
        printf("Please enter the value for the dissipation time td:\n ");
        scanf("%lf",&data[i].td);
        printf("Please enter the value for the percentage of the ");
        printf("\n original charge pc to reach within td\n");
        scanf("%lf",&data[i].pc);
        i = i+1;
    
    
        printf("do you want to 1. enter a new set of values(max 5) and save these values\n");
        printf("or 2.choose a saved set of values\n");
        scanf("%d",&ans);
    
    
        if(ans==1)
         {
            fp = fopen(BINFILE, "wb");
            fwrite(&data[i].l, sizeof(double), 1, fp);
            fclose(fp);
         }
        else
         {  flag = TRUE;
            fp=fopen(BINFILE, "rb");
            fread(&data[5].l,sizeof(double),1,fp);
            fclose(fp);
         }
    }
    
    
    
    
    printf("%lf",data[5].l);
    
    
    
    
    
    
    
    
    }
    Last edited by Kamal Joub; 11-20-2017 at 03:34 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at this snippet:

    Code:
     
        if(ans==1)
         {
            fp = fopen(BINFILE, "wb");
            fwrite(&data[i].l, sizeof(double), 1, fp);
            fclose(fp);
         }
        else
         {  flag = TRUE;
            fp=fopen(BINFILE, "rb");
            fread(&data[5].l,sizeof(double),1,fp);
            fclose(fp);
         }
    }
     
     
     
     
    printf("%lf",data[5].l);
    What is the size of your array?

    With that sized array what are the valid indexes available?

    You're accessing your array out of bounds, so I would expect garbage output.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    ....
    Last edited by userxbw; 11-20-2017 at 04:04 PM.

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    jimblumberg but my array is data[5] , hence it's of size 5 , how am i accessing it out of bounds ?

    and

    userxbw , i dont understand what you mean , could you elaborate please ?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    but my array is data[5] , hence it's of size 5 , how am i accessing it out of bounds ?
    Look at this snippet:
    Code:
      
    printf("%lf",data[5].l);
    Do you know that arrays in C start at zero and stop at size - 1? This means that the valid elements of your array would be 0, 1, 2, 3, and 4. Element 5 is outside the bounds of your array.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    while loop goes
    Code:
    while ( i < 5)
    {
       something[i] = "do da"; // but i = what when you're trying to access it on which loop interval ?
    
      print(something[5]);
    i++;
    }
    if that loop not go 5 times what do you think you're going to get if you try to get to 5 when only on 3?
    plus more thinking array[5] = 0 - 4 to last element as well. as stated
    Last edited by userxbw; 11-20-2017 at 04:34 PM.

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    nevermind;;

  8. #8
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    well i increased the size of the array and it didn't change anything at all

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think it would be easier to fread() or fwrite() the whole structure.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why did you increase the size of the array? You really need learn to stay within the bounds of your array.

    If you're still having problems post the smallest possible code that illustrates the problem. Which means remove everything that is not necessary to seeing the problem.

  11. #11
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    whiteflag , i'd like to do that but can it be done in a single fread and fwrite ?

    jim , idk what the hell you're talking about first you say element 5 is outside the bounds of my array , so i increase the size of the array so it would be within the bounds.like for real man you're contradicting yourself

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kamal Joub View Post
    whiteflag , i'd like to do that but can it be done in a single fread and fwrite ?
    if you can figure out how to put one in then just repeat the process for the others ones.

    Quote Originally Posted by Kamal Joub View Post
    jim , idk what the hell you're talking about first you say element 5 is outside the bounds of my array , so i increase the size of the array so it would be within the bounds.like for real man you're contradicting yourself
    he means to just back off by one number to eliminate over running your array. They are zero based, or you can keep doing it that way, and do not access the zero element.

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    running your program your logic seems to be off to begin with.
    Code:
    D:\>
    D:\>TextFile1
    Please enter the value for L:
    23
    Please enter the value for C:
    44
    Please enter the value for the battery voltage:
    55
    Please enter the value for the dissipation time td:
     66
    Please enter the value for the percentage of the
     original charge pc to reach within td
    77
    do you want to 1. enter a new set of values(max 5) and save these values
    or 2.choose a saved set of values
    1
    Please enter the value for L:
    22
    Please enter the value for C:
    33
    Please enter the value for the battery voltage:
    44
    Please enter the value for the dissipation time td:
     55
    Please enter the value for the percentage of the
     original charge pc to reach within td
    66
    do you want to 1. enter a new set of values(max 5) and save these values
    or 2.choose a saved set of values
    2
    0.000000
    if you select 1 it is suppose to save whatever, then ask for more. if 2 it is just suppose to save. when you have it reading from the file. and even on first run if 2 is selected how to get input that has not been saved?

    in file bin
    Code:
    `K¤K
    let me tinker with this some more.
    Last edited by userxbw; 11-20-2017 at 06:16 PM.

  14. #14
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    if the user just started the program he is supposed to choose 1 before 2 because there will be no data to choose from , and i'm using the extra element in data[] to store the data read from file bin but as you indicated file it is not writing the correct value in file bin to begin with , i am still working on it but I don't seem to figure it out

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    whiteflag , i'd like to do that but can it be done in a single fread and fwrite ?
    Yes it can. This is what you have now.
    Code:
        if(ans==1)
         {
            fp = fopen(BINFILE, "wb");
            fwrite(&data[i].l, sizeof(double), 1, fp);
            fclose(fp);
         }
        else
         {  flag = TRUE;
            fp=fopen(BINFILE, "rb");
            fread(&data[5].l,sizeof(double),1,fp);
            fclose(fp);
         }
    This just reads and writes l. Writing the whole structure is a minor change to the relevant function arguments.

    Code:
    fwrite(&data[i], sizeof data[i], 1, fp);
    
    fread(&data[i], sizeof data[i], 1, fp);
    One structure has all five values in it, so I am starting to think that you won't need the array anymore either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2013, 11:39 PM
  2. Replies: 9
    Last Post: 12-05-2012, 01:11 PM
  3. Binary file input, and reallocating
    By Dr Saucie in forum C Programming
    Replies: 7
    Last Post: 03-01-2010, 11:21 PM
  4. Binary Input File to Array
    By siLent0 in forum C Programming
    Replies: 19
    Last Post: 04-11-2008, 12:37 AM
  5. Deletion of input in Binary File
    By supz in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2003, 04:48 AM

Tags for this Thread