Thread: binary file input and out

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I am not even sure if it is getting printing out properly because it is binary to readable?
    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)
         {
           if (( fp = fopen(BINFILE, "wb+")) == NULL)
               fp = fopen(BINFILE, "wb+");
            fwrite(&data[i].l, sizeof(data), 1, fp);
            fwrite(&data[i].c, sizeof(data), 1, fp);
            fwrite(&data[i].v, sizeof(data), 1, fp);
            fwrite(&data[i].td, sizeof(data), 1, fp);
            fwrite(&data[i].pc, sizeof(data), 1, fp);
            fclose(fp);
         }
        else
         {  flag = TRUE;
        if ((fp = fopen(BINFILE, "rb")) == NULL)
        {
            printf("NULL\n");
            return -1;
        }
        i = 0;
        while (fread(&data, sizeof(data), 1, fp) > 0) {
            printf("\n%lf %lf %lf %lf %lf", data[i].l, data[i].c, data[i].v, data[i].td, data[i].pc);
            i++;
        }
    
            /*
            fread(&data[0].l,sizeof(data),1,fp);
            fread(&data[0].c, sizeof(data), 1, fp);
            fread(&data[0].v, sizeof(data), 1, fp);
            fread(&data[0].td, sizeof(data), 1, fp);
            fread(&data[0].pc, sizeof(data), 1, fp);
            */
            fclose(fp);
         }
    }
     
     
     
     
    //    printf("%lf",data[0].l);
     
     return 0;
     
    }
    output
    Code:
    D:\>TextFile1
    Please enter the value for L:
    1
    Please enter the value for C:
    2
    Please enter the value for the battery voltage:
    3
    Please enter the value for the dissipation time td:
     4
    Please enter the value for the percentage of the
     original charge pc to reach within td
    5
    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:
    9
    Please enter the value for C:
    8
    Please enter the value for the battery voltage:
    7
    Please enter the value for the dissipation time td:
     6
    Please enter the value for the percentage of the
     original charge pc to reach within td
    5
    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 6295431406286370727536011237946244791705237690491218584095526881385347905064740438979131453551567357078445402736873848149739361202374231640980307203492061796916584926163844184616367201174709747561557468758843641624377720573490380473771878685395564965510635352718573568.000000 5656480192548399055291658992773965530404306390921326280525488803357313139184696431374759754138077308452519644901332723765673548193246423670603339053276874272259508069289106976417600979556005707351823707837986058737075859224019869521716361614428238887988213392257056768.000000 5510372129097922078883755878498384747837673462694624155700227671993397332099305205258285769402949440140913498937149923678849750886679489080108847856040680009822707940304745757324583255867618086074339466276421131754444240370246706160157026176931015331489162973677617152.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 71813619448390590500334750711676470725602066625745435032210338747412176309250661589373563975484971914574651650549458809012657041157266604032.000000
    D:\>
    binary file
    Code:
    k S S gˆw   •…w8S •\…w         ,S w)     '       C8+`k @S OtŸ        \S  Ÿ         5\ˆS ˆS ˆS g     e™ ם ם z™ 5\S a    00k Xdk 5\ם ם  ' S gˆw   •…w8S •\…w         ,S w)     '       C8+`k @S OtŸ        \S  Ÿ         5\ˆS ˆS ˆS g     e™ ם ם z™ 5\S a    00k Xdk 5\ם ם  '     ם    •…w8S •\…w         ,S w)     '       C8+`k @S OtŸ        \S  Ÿ         5\ˆS ˆS ˆS g     e™ ם ם z™ 5\S a    00k Xdk 5\ם ם  '     ם ם ˜S 8S •\…w         ,S w)     '       C8+`k @S OtŸ        \S  Ÿ         5\ˆS ˆS ˆS g     e™ ם ם z™ 5\S a    00k Xdk 5\ם ם  '     ם ם ˜S     S          ,S w)     '       C8+`k @S OtŸ        \S  Ÿ         5\ˆS ˆS ˆS g     e™ ם ם z™ 5\S a    00k Xdk 5\ם ם  '     ם ם ˜S     S  +\
    using this page for a guild line
    The fread() fwrite() function in C
    Last edited by userxbw; 11-20-2017 at 06:46 PM.

  2. #17
    Banned
    Join Date
    Aug 2017
    Posts
    861
    edited:

    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 LITTEINFO 
    { 
      
      
       double l; // starting time 
       double c; // ending time 
       double v; // velocity 
       double td; // time 
       double pc; // altitude 
      
      
    } INFO; 
     
    typedef struct LITTEINFO2 
    { 
      
      
       double l; // starting time 
       double c; // ending time 
       double v; // velocity 
       double td; // time 
       double pc; // altitude 
      
      
    } INFO2; 
      
      
    int main() 
    { 
        INFO data;  
        int i=0; 
        int flag = FALSE; 
        int ans; 
        FILE *fp; 
     
        if ((fp = fopen(BINFILE, "wb+")) == NULL) 
            fp = fopen(BINFILE, "wb+"); 
      
        do 
        { 
            printf("Please enter the value for L:\n"); 
            scanf("%lf", &data.l); 
            printf("Please enter the value for C:\n"); 
            scanf("%lf", &data.c); 
            printf("Please enter the value for the battery voltage:\n"); 
            scanf("%lf", &data.v); 
            printf("Please enter the value for the dissipation time td:\n "); 
            scanf("%lf", &data.td); 
            printf("Please enter the value for the percentage of the "); 
            printf("\n original charge pc to reach within td\n"); 
            scanf("%lf", &data.pc); 
            fwrite(&data, sizeof(data), 1 ,fp); 
     
          
            i++; 
        } while (i < 2);  
            fclose(fp); 
      
            if ((fp = fopen(BINFILE, "rb")) == NULL) 
            { 
                printf("NULL\n"); 
                return -1; 
            } 
            while (fread(&data, sizeof(data), 1, fp) > 0)  
                printf("\n%lf %lf %lf %lf %lf", data.l, data.c, data.v, data.td, data.pc); 
                 
            fclose(fp); 
         
        INFO2 data2[5];  
    //    int i=0; 
        //int flag = FALSE; 
        //int ans; 
        FILE *fp2; 
        i=0; 
         while(i < 5 && flag==FALSE) 
    { 
        printf("\n\nPlease enter the value for L:\n"); 
        scanf("%lf",&data2[i].l); 
        printf("Please enter the value for C:\n"); 
        scanf("%lf",&data2[i].c); 
        printf("Please enter the value for the battery voltage:\n"); 
        scanf("%lf",&data2[i].v); 
        printf("Please enter the value for the dissipation time td:\n "); 
        scanf("%lf",&data2[i].td); 
        printf("Please enter the value for the percentage of the "); 
        printf("\n original charge pc to reach within td\n"); 
        scanf("%lf",&data2[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) 
         { 
           if (( fp2 = fopen(BINFILE, "wb+")) == NULL) 
               fp2 = fopen(BINFILE, "wb+"); 
                
            fwrite(&data2[i].l, sizeof(double), 1, fp2); 
            fwrite(&data2[i].c, sizeof(double), 1, fp2); 
            fwrite(&data2[i].v, sizeof(double), 1, fp2); 
            fwrite(&data2[i].td, sizeof(double), 1, fp2); 
            fwrite(&data2[i].pc, sizeof(double), 1, fp2); 
            fclose(fp2); 
         } 
        else 
         {  flag = TRUE; 
        if ((fp2 = fopen(BINFILE, "rb")) == NULL) 
        { 
            printf("NULL\n"); 
            return -1; 
        } 
        int e = 0; 
        while (fread(&data, sizeof(double), 1, fp2) > 0) { 
            printf("\n%lf %lf %lf %lf %lf", data2[e].l, data2[e].c, data2[e].v, data2[e].td, data2[e].pc); 
            e++; 
        } 
      
            /* 
            fread(&data[0].l,sizeof(data),1,fp); 
            fread(&data[0].c, sizeof(data), 1, fp); 
            fread(&data[0].v, sizeof(data), 1, fp); 
            fread(&data[0].td, sizeof(data), 1, fp); 
            fread(&data[0].pc, sizeof(data), 1, fp); 
            */ 
            fclose(fp2); 
         } 
    } 
         
         
     return 0; 
      
    }
    input/output
    Code:
                        getnumcount.c         my_file                               string_vector.cpp
    userx@slackwhere:~/bin
    $ ./TextFile1
    Please enter the value for L:
    1
    Please enter the value for C:
    2
    Please enter the value for the battery voltage:
    3
    Please enter the value for the dissipation time td:
     4
    Please enter the value for the percentage of the 
     original charge pc to reach within td
    5
    Please enter the value for L:
    6
    Please enter the value for C:
    7
    Please enter the value for the battery voltage:
    8
    Please enter the value for the dissipation time td:
     9
    Please enter the value for the percentage of the 
     original charge pc to reach within td
    1
    
    1.000000 2.000000 3.000000 4.000000 5.000000
    6.000000 7.000000 8.000000 9.000000 1.000000
    
    Please enter the value for L:
    32
    Please enter the value for C:
    43
    Please enter the value for the battery voltage:
    54
    Please enter the value for the dissipation time td:
     65
    Please enter the value for the percentage of the 
     original charge pc to reach within td
    12
    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
    
    32.000000 43.000000 54.000000 65.000000 12.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 6.000000 7.000000 8.000000 9.000000
    1.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 30289136841674484613820984470952380437706717204413672115931267714705824252379917673511780352.000000 0.000000
    0.000000 0.000000 0.000000 -0.000000 -0.000000userx@slackwhere:~/bin
    $
    so taking from that webpage
    The fread() fwrite() function in C

    that first one works so you might want to rethink your strategy, or I beleive yours did not work because of the way it is being written into the file.
    if you write to element 1 then you try to pull off of element 4 ( or 5) that is not even in the file yet so that is where it fails.
    bold is only one entry and loop gets it then the rest are crap. because nothing has been put into that part of it. and it is still using the same output data file.
    with a little more experimentation on your part you should be able to get along just fine. yes?
    Last edited by userxbw; 11-20-2017 at 07:48 PM.

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    @userxbw
    In your current code, you read into data but print info from data2.
    Code:
        while (fread(&data, sizeof(double), 1, fp2) > 0) { 
    
            printf("\n%lf %lf %lf %lf %lf", data2[e].l, data2[e].c, data2[e].v, data2[e].td, data2[e].pc); 
    
            e++; 
    
        }
    It seems like an odd way to prove that things are working...

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    If you want to be sure that your fread() and fwrite calls work, be careful with your arguments. The second argument is the size of the data in bytes that you point to. The third argument is more for arrays of objects, i.e. how many of the object being pointed to there are, or the length of the array. If you are not writing or reading an array of things, then the argument is 1.

  5. #20
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    @userxbw
    In your current code, you read into data but print info from data2.
    Code:
        while (fread(&data, sizeof(double), 1, fp2) > 0) { 
    
            printf("\n%lf %lf %lf %lf %lf", data2[e].l, data2[e].c, data2[e].v, data2[e].td, data2[e].pc); 
    
            e++; 
    
        }
    It seems like an odd way to prove that things are working...
    I knew someone might see that, I just noticed that after it was too late to mod it. I got something more logical written from what he did. and fixed that typeo/mistake in the code I got here. that is what I get from select Ctr+d to dup all of it then edit it. missed one.
    Quote Originally Posted by whiteflags
    If you want to be sure that your fread() and fwrite calls work, be careful with your arguments. The second argument is the size of the data in bytes that you point to. The third argument is more for arrays of objects, i.e. how many of the object being pointed to there are, or the length of the array. If you are not writing or reading an array of things, then the argument is 1.
    so if a struc is not all double's and it is mixed then that call needs to be adjusted to reflect same in the order of data types declared when reading the data from the file else errors will ensue? or run it like i have it in this code bottom line for fread to cover different data types it just needs to be in the same order in struct as printed out. in this code I wasn't planning posting this (just yet maybe) but for sake of argument. Because I am wondering if it is really even reading from the file to begin with. in the code it too is being put into the array and written into a file.
    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 LITTEINFO 
    { 
      
       double l; // starting time 
       double c; // ending time 
       double v; // velocity 
       double td; // time 
       double pc; // altitude 
      
      
    } INFO; 
      
      
    int main() 
    { 
        int i=0; 
        int flag = FALSE; 
        int ans; 
        INFO data2[5];  
        FILE *fp2; 
          
         if (( fp2 = fopen(BINFILE, "wb+")) == NULL) 
                fp2 = fopen(BINFILE, "wb+"); 
                
        while(i < 5 && flag==FALSE) 
        { 
            printf("Please enter the value for L:\n"); 
                scanf("%lf",&data2[i].l); 
            printf("Please enter the value for C:\n"); 
                scanf("%lf",&data2[i].c); 
            printf("Please enter the value for the battery voltage:\n"); 
                scanf("%lf",&data2[i].v); 
            printf("Please enter the value for the dissipation time td:\n "); 
                scanf("%lf",&data2[i].td); 
            printf("Please enter the value for the percentage of the\n" 
                    "original charge pc to reach within td\n"); 
                    scanf("%lf",&data2[i].pc); 
        
            fwrite(&data2, sizeof(data2), 1 ,fp2);      
            i++; 
             
            printf("do you want to \n" 
            "1. enter a new set of values(max 5)\n" 
            "or\n" 
            "2.choose a saved set of values\n"); 
                scanf("%d",&ans); 
                 
            if (ans == 2) 
                flag = TRUE; 
        } 
     
        printf("which set of values do you want to see?\n"); 
            scanf("%d",&ans); 
     
        rewind(fp2); 
        // compensate for zero element. 
        if ( ans-1 > i) 
        { 
            printf("out of bounds\n"); 
            return -1; 
        } 
        else 
        { 
            //from stuc 
            printf("\n%.2lf %.2lf %.2lf %.2lf %.2lf", data2[ans-1].l, data2[ans-1].c, data2[ans-1].v, 
            data2[ans-1].td, data2[ans-1].pc); 
             
            printf("\nSKIP LINE\n"); 
            //from file? 
            fread(&data2, sizeof(data2), 1, fp2); 
                printf("\n%.2lf %.2lf %.2lf %.2lf %.2lf", data2[ans-1].l, data2[ans-1].c, data2[ans-1].v, 
            data2[ans-1].td, data2[ans-1].pc); 
        } 
             
        fclose(fp2); 
     return 0; 
      
    }
    is that really reading the data both times once from the stuct array and once from the file?
    Code:
    userx@slackwhere:~/bin
    $ ./TextFile1
    Please enter the value for L:
    32 
    Please enter the value for C:
    32
    Please enter the value for the battery voltage:
    4
    Please enter the value for the dissipation time td:
     554
    Please enter the value for the percentage of the
    original charge pc to reach within td
    343
    do you want to 
    1. enter a new set of values(max 5)
    or
    2.choose a saved set of values
    1
    Please enter the value for L:
    6
    Please enter the value for C:
    5
    Please enter the value for the battery voltage:
    46466
    Please enter the value for the dissipation time td:
     2333
    Please enter the value for the percentage of the
    original charge pc to reach within td
    5.4
    do you want to 
    1. enter a new set of values(max 5)
    or
    2.choose a saved set of values
    2
    which set of values do you want to see?
    1
    
    32.00 32.00 4.00 554.00 343.00
    SKIP LINE
    
    32.00 32.00 4.00 554.00 343.00
    and what is a different number other than 1 in the 3rd place indicate, or I could go read that web page again. I'll just ask you.
    Last edited by userxbw; 11-20-2017 at 09:04 PM.

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    That looks alright to me. The one minor error that I would fix is here.
    Code:
     fread(&data2, sizeof(data2), 1, fp2);
    &data2 is technically pointer to array, not a pointer to the first element... although it did work out, because the base address and the pointer's value would be the same, keeping yourself type strict is important.

    And yes this call is just another way to write a whole array of stuff. I wrote an example program where my call is a little bit different:
    Code:
            infoHandled = fread(data, sizeof data[0], 5, fp);
            if (infoHandled == 5) { 
                  // ...
            }
    You can see it is just a matter of perspective on the size of the object being read or written.

  7. #22
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    That looks alright to me. The one minor error that I would fix is here.
    Code:
     fread(&data2, sizeof(data2), 1, fp2);
    &data2 is technically pointer to array, not a pointer to the first element... although it did work out, because the base address and the pointer's value would be the same, keeping yourself type strict is important.

    And yes this call is just another way to write a whole array of stuff. I wrote an example program where my call is a little bit different:
    Code:
            infoHandled = fread(data, sizeof data[0], 5, fp);
            if (infoHandled == 5) { 
                  // ...
            }
    You can see it is just a matter of perspective on the size of the object being read or written.
    uut oh


    Code:
        else 
        { 
            //from stuc 
            printf("\n%.2lf %.2lf %.2lf %.2lf %.2lf", data2[ans-1].l, data2[ans-1].c, data2[ans-1].v, 
            data2[ans-1].td, data2[ans-1].pc); 
             
            printf("\nSKIP LINE\n"); 
            //from file? 
            fread(data2, sizeof(data2), 1, fp2); 
                printf("\n%.2lf %.2lf %.2lf %.2lf %.2lf", data2[ans-1].l, data2[ans-1].c, data2[ans-1].v, 
            data2[ans-1].td, data2[ans-1].pc); 
        }
    
    .......which set of values do you want to see?
    2
    
    434.33 455.43 200.43 43.00 9553.30
    SKIP LINE
    
    0.00 0.00 0.00 0.00 0.00
    now I'm going to see if I can figure out what you did with your snippet of code.

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    I mean I promise that it works, but you know, &data and just data are different in terms of type... ask cdecl. You need to be consistent for it not to be a problem.

    Actually I think I might be wrong about this conceptually, and fread(&data, sizeof data, 1, fp); is probably the right call. I just had a brain fart. Sorry.
    Last edited by whiteflags; 11-20-2017 at 10:11 PM.

  9. #24
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    Thank you guys so much for the help I actually got not only an answer to my question but a better understanding of binary files. I just have 1 question , wy the " + " after wb in (( fp2 = fopen(BINFILE, "wb+")) == NULL)

  10. #25
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    I mean I promise that it works, but you know, &data and just data are different in terms of type... ask cdecl. You need to be consistent for it not to be a problem.
    now it is not working either way, I think you jinks me. lol
    I am trying to see if it will read the file then print at a given line (element) then the amount picked to print. but I do not think it is going to work. but it should if one can pick which one to print in my other one, then logic says if looped ( i just thought of an if statement i might have to add) to get it to start printing when reaching that element. if possible. nope .. bugging out right now tying to get this to work.
    who is "ask cdecl."
    Last edited by userxbw; 11-20-2017 at 10:21 PM.

  11. #26
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kamal Joub View Post
    Thank you guys so much for the help I actually got not only an answer to my question but a better understanding of binary files. I just have 1 question , wy the " + " after wb in (( fp2 = fopen(BINFILE, "wb+")) == NULL)
    this should cross over to windows too.

    fopen(3) - Linux manual page

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Quote Originally Posted by userxbw View Post
    now it is not working either way, I think you jinks me. lol
    I am trying to see if it will read the file then print at a given line (element) then the amount picked to print. but I do not think it is going to work. but it should if one can pick which one to print in my other one, then logic says if looped ( i just thought of an if statement i might have to add) to get it to start printing when reaching that element. if possible. nope .. bugging out right now tying to get this to work.
    Hard for me to say. The version you posted a while ago worked, so I would just revert to that instead of figuring this out. If you want to just print one line at a time you have to fwrite/fread differently as well. It is one element of data and not all of them.
    So maybe:

    fread(&data[i], sizeof data[i], 1, fp);
    fwrite(&data[i], sizeof data[i], 1, fp);

    cus data[i] is one structure and the program is really built to operate one structure at a time -- at least the way that the OP had it.

    Also consider seeking with fseek(), passing in SEEK_SET. Every struct should start at i*sizeof data[0] so you can just seek and read or write at random.
    fseek(3) - Linux manual page
    Last edited by whiteflags; 11-20-2017 at 10:54 PM.

  13. #28
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    Hard for me to say. The version you posted a while ago worked, so I would just revert to that instead of figuring this out. If you want to just print one line at a time you have to fwrite/fread differently as well. It is one element of data and not all of them.
    So maybe:

    fread(&data[i], sizeof data[i], 1, fp);
    fwrite(data[i], sizeof data[i], 1, fp);

    cus data[i] is one structure and the program is really built to operate one structure at a time -- at least the way that the OP had it.

    Also consider seeking with fseek(), passing in SEEK_SET. Every struct should start at i*sizeof data[0] so you can just seek and read or write at random.
    fseek(3) - Linux manual page
    ga duuh ok now I think I see what you're talking about, instead of shoving them all in at the top revert back to element numbers.
    and I did that switch from rewind to fseek to see if that maybe the problem. let me go into rewrite mode.

  14. #29
    Banned
    Join Date
    Aug 2017
    Posts
    861
    gurr sometimes it works and sometimes it does not, with that shove them all in at once.
    Code:
    fwrite(&data2, sizeof(data2), 1 ,fp2);
    you sure it is
    Code:
    fread(&data[i], sizeof data[i], 1, fp);
    fwrite(data[i], sizeof data[i], 1, fp);
    

    not
    Code:
    fread(data[i], sizeof data[i], 1, fp);
    fwrite(&data[i], sizeof data[i], 1, fp);
    

    or both with & ?

    edit, yeah it complined your way.
    Code:
    #include <stdio.h> 
    #include <ctype.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 LITTEINFO 
    { 
      
       double l; // starting time 
       double c; // ending time 
       double v; // velocity 
       double td; // time 
       double pc; // altitude 
      
      
    } INFO; 
      
      
    int main() 
    { 
        int i=0; 
        int flag = FALSE; 
        int ans; 
        INFO data2[10];  
        FILE *fp2; 
          
         if (( fp2 = fopen(BINFILE, "ab+")) == NULL) 
         { 
             printf("no file yet\n"); 
                fp2 = fopen(BINFILE, "ab+"); 
        } 
                
        while(i < 10 && flag==FALSE) 
        { 
            printf("Please enter the value for L:\n"); 
                scanf("%lf",&data2[i].l); 
            printf("Please enter the value for C:\n"); 
                scanf("%lf",&data2[i].c); 
            printf("Please enter the value for the battery voltage:\n"); 
                scanf("%lf",&data2[i].v); 
            printf("Please enter the value for the dissipation time td:\n "); 
                scanf("%lf",&data2[i].td); 
            printf("Please enter the value for the percentage of the\n" 
                    "original charge pc to reach within td\n"); 
                    scanf("%lf",&data2[i].pc); 
        
                              
            fwrite(&data2[i].l, sizeof(double), 1, fp2);  
            fwrite(&data2[i].c, sizeof(double), 1, fp2);  
            fwrite(&data2[i].v, sizeof(double), 1, fp2);  
            fwrite(&data2[i].td, sizeof(double), 1, fp2);  
            fwrite(&data2[i].pc, sizeof(double), 1, fp2);  
             
            //fwrite(&data2, sizeof(data2), 1 ,fp2);      
            i++; 
             
            printf("do you want to \n" 
            "1. enter a new set of values(max 5)\n" 
            "or\n" 
            "2.choose a saved set of values\n"); 
                scanf("%d",&ans); 
                 
            if (ans == 2) 
                flag = TRUE; 
        } 
     
        printf("which set of values do you want to see?\n"); 
            scanf("%d",&ans); 
     
        rewind(fp2); 
        // compensate for zero element. 
        if ( ans-1 > i) 
        { 
            printf("out of bounds\n"); 
            return -1; 
        } 
        else 
        { 
            //from stuc 
            printf("\n%.2lf %.2lf %.2lf %.2lf %.2lf", data2[ans-1].l, data2[ans-1].c, data2[ans-1].v, 
            data2[ans-1].td, data2[ans-1].pc); 
             
            printf("\nSKIP LINE\n"); 
            //from file? 
            fread(data2, sizeof(data2), 1, fp2); 
                printf("\n%.2lf %.2lf %.2lf %.2lf %.2lf", data2[ans-1].l, data2[ans-1].c, data2[ans-1].v, 
            data2[ans-1].td, data2[ans-1].pc); 
        } 
         
        // trying to get where to start, and how many lines. 
        // no check for over shoot array yet.  
        // the minium is not even workng if it can 
        // even be done this way.  
         
        //rewind(fp2); 
        fseek(fp2, 0 , SEEK_SET); 
        char ch; 
        int start, amount, count =0; 
        size_t whatsInThere; 
        printf("\nWould you like to see more than 1 file? y/n\n"); 
            scanf(" %c",&ch); 
             
        if(tolower(ch) == 'y') 
        { 
            printf("how many files and starting from which one?\n"); 
            scanf("%d%d", &amount, &start); 
          
            whatsInThere = fread(&data2, sizeof(data2), i, fp2); 
            printf("whatsInThere %zu\n", whatsInThere); 
            if (amount <= whatsInThere ) 
            { printf("2: whatsInThere %zu\n", whatsInThere); 
                //for (; (start-1) < amount; start++) 
                  while (fread(data2, sizeof(data2), 1, fp2) > 0) 
                { 
                //    fread(&data2, sizeof(data2), i, fp2); 
                        if ( count >= (start-1)) 
                        { 
                            printf("\n%.2lf %.2lf %.2lf %.2lf %.2lf", data2[start-1].l, data2[start-1].c, data2[start-1].v, 
                            data2[start-1].td, data2[start-1].pc); 
                            start++; 
                        }     
                             
                            count++; 
                            printf("count %d\n", count); 
                } 
            } 
            else 
            { 
                printf(" woo dude not valid, good-bye\n"); 
                fclose(fp2); 
                return -1; 
            } 
        } 
        else 
        { 
            printf("fine c ya\n"); 
        } 
                 
        fclose(fp2); 
     return 0; 
      
    }
    sometimes it works sometimes it does not.

    Last edited by userxbw; 11-20-2017 at 10:51 PM.

  15. #30
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    I've fixed it in the earlier post. I don't know how I missed the &s in all the necessary places. I guess that's what I get for not compiling. I also thought that maybe I was wrong in post 23... so sorry for all the confusion here. It's just getting worse and worse because I'm not really experimenting 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