Thread: reading from bin file

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

    reading from bin file

    so i made a program , where the input of the user is saved in a binary file , what i want to do tho is use the values saved in the binary after the program is restarted , meaning use previously inputted values.

    However , when i test to see if the program read he correct values from the binary file , i keep getting 0. The following is the code related only to this issue please ignore the prototypes thanks.

    btw , i am also trying to check if the file exist and contain elements(Hence the != NULL in the beginning of main) to display it to the user but NULL doesn't seem to do the trick since even if the bin file don't exist it prints the statement indicated.So , please if possible help with that too but the first issue is the priority.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    #define N 100  // number of points
    #define TRUE 1
    #define FALSE 0
    #define EPSILON 1e-30
    #define BINFILE "file.bin"
    #define pi 3.1415926535897932384626433832795
    
    
    typedef struct
    {
    
    
       double l; // starting time
       double c; // ending time
       double v; // velocity
       double td; // time
       double pc; // altitude
    
    
    } INFO;
    
    
    // Function Prototypes
    void findRoot(INFO[], int , double * );
    void plotFunc(double, double, int, double []);
    double func(INFO [],int , double);
    void plot(int,  double[], double[], int, double[], double[]);
    double getMin(double *, int );
    double getMax(double *, int );
    
    
    
    
    int main()
    {
        int i=0;
        int flag = FALSE;
        int flag2;
        int ans;
        INFO data2[5];
        FILE *fp2;
        double root , R;
        double check , check2;
        double f;
        char ans2;
    
    
         if (( fp2 = fopen(BINFILE, "wb"))!= NULL )
         {
             printf("Do you want to choose previously inputted values? (y/n)\n");
             fflush(stdin);
             scanf("%c",&ans2);
    
    
             if (ans2 == 'y')
                flag = TRUE;
    
    
                fp2 = fopen(BINFILE, "wb");
        }
    
    
        while(i < 5 && flag==FALSE)
        {
          do
          {
              flag2 = TRUE;
            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);
    
    
            R = (-2* data2[i].l * log(data2[i].pc))/data2[i].td;
            check = 1/(data2[i].l*data2[i].c);
            check = check - pow((R/(2*data2[i].l)),2) ;
            check2 = sqrt(check);
            f = check2/(2*pi);
            f= 50/f;
    
    
            if(check < 0)
            {
               flag2 = FALSE;
               printf("Invalid input , Please enter a bigger value for td\n");
            }
            if(f < data2[i].td)
            {
                flag2 = FALSE;
                printf("Frequency too high; plot may be distorted\n");
            }
    
    
    
    
          }while(flag2 == FALSE);
    
    
    
    
            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 use?\n");
            scanf("%d",&ans);
    
    
        rewind(fp2);
        // compensate for zero element.
        if ( ans-1 > 6)
        {
            printf("out of bounds\n");
            return -1;
        }
        else
        {
            fread(data2, sizeof(data2), ans, fp2);
        }
    
    
    
    
    
    
    
    
    
    
        findRoot(data2,ans,&root);
    printf("%lf\n",data2[ans-1].l);
    fclose(fp2);
    return 0;
    
    
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Generally you want to read a binary file in a manner similar to the way that you wrote said file. Expecting five doubles in a file to be exactly the same as a structure containing five doubles is a mistake.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    I tired making 5 fread similar to what i did with fwrite except with data2[ans-1] instead of data2[i] , however , i keep getting 0 as an output. Can you please elaborate a little bit more?

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    take a look at how you're opening your files, in what mode they are being opened in.

    fopen(3) - Linux manual page

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    yeah I noticed that yesterday , i added an fclose for the "wb" fopen and i added an fopen for "rb" , here's the code I have now , however notice it's still outputting zero, meaning it's still wrong

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    #define N 100  // number of points
    #define TRUE 1
    #define FALSE 0
    #define EPSILON 1e-30
    #define BINFILE "file.bin"
    #define pi 3.1415926535897932384626433832795
    
    
    typedef struct
    {
    
    
       double l; // starting time
       double c; // ending time
       double v; // velocity
       double td; // time
       double pc; // altitude
    
    
    } INFO;
    
    
    // Function Prototypes
    void findRoot(INFO[], int , double * );
    void plotFunc(double, double, int, double []);
    double func(INFO [],int , double);
    void plot(int,  double[], double[], int, double[], double[]);
    double getMin(double *, int );
    double getMax(double *, int );
    
    
    
    
    int main()
    {
        int i=0;
        int flag = FALSE;
        int flag2;
        int ans;
        INFO data2[5];
        FILE *fp2;
        double root , R;
        double check , check2;
        double f;
        char ans2;
    
    
         if (( fp2 = fopen(BINFILE, "wb")) != NULL )
         {
             printf("Do you want to choose previously inputted values? (y/n)\n");
             fflush(stdin);
             scanf("%c",&ans2);
    
    
             if (ans2 == 'y')
                flag = TRUE;
    
    
                fp2 = fopen(BINFILE, "wb");
        }
    
    
        while(i < 5 && flag==FALSE)
        {
          do
          {
              flag2 = TRUE;
            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);
    
    
            R = (-2* data2[i].l * log(data2[i].pc))/data2[i].td;
            check = 1/(data2[i].l*data2[i].c);
            check = check - pow((R/(2*data2[i].l)),2) ;
            check2 = sqrt(check);
            f = check2/(2*pi);
            f= 50/f;
    
    
            if(check < 0)
            {
               flag2 = FALSE;
               printf("Invalid input , Please enter a bigger value for td\n");
            }
            if(f < data2[i].td)
            {
                flag2 = FALSE;
                printf("Frequency too high; plot may be distorted\n");
            }
    
    
    
    
          }while(flag2 == FALSE);
    
    
    
    
            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 use?\n");
            scanf("%d",&ans);
    
    
        fclose(fp2);
    
    
        fp2 = fopen(BINFILE, "rb");
        // compensate for zero element.
        if ( ans-1 > 6)
        {
            printf("out of bounds\n");
            return -1;
        }
        else
        {
            fread(&data2[ans-1].l, sizeof(double), 1, fp2);
            fread(&data2[ans-1].c, sizeof(double), 1, fp2);
            fread(&data2[ans-1].v, sizeof(double), 1, fp2);
            fread(&data2[ans-1].td, sizeof(double), 1, fp2);
            fread(&data2[ans-1].pc, sizeof(double), 1, fp2);
        }
    
    
    
    
    
    
    
    
    
    
    findRoot(data2,ans,&root);
    printf("%lf\n",data2[ans-1].l);
    fclose(fp2);
    return 0;
    
    
    }

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    didn't you have this thing working or almost working. that code is looking rather familiar now that I read some of it. there was a entire post on this where whiteflags and I got into a discussion on this. here is that very very similar code that is working code.
    Code:
    #include <stdio.h> 
    #include <ctype.h> 
    #include <string.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, "wb+")) == NULL) 
         { 
             printf("no file yet\n"); 
                fp2 = fopen(BINFILE, "wb+"); 
        } 
                
        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), i, 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.  
        // and whatever other little tweeks that maybe 
        // needed.  
         
          
        fseek(fp2, 0 , SEEK_SET); 
        char ch; 
        int start, amount;  
        size_t whatsInThere; 
        printf("\nWould you like to see more than 1 file? y/n\n"); 
            scanf(" %c",&ch); 
             
        if(tolower(ch) == 'y') 
        {  //clear struct / zero out struct 
            memset(&data2, 0, sizeof(data2)); 
            printf("how many files and starting from which one?\n"); 
            scanf("%d%d", &amount, &start); 
          
         printf("size of : %ld\n", (i*sizeof data2[0])); 
          
          
            whatsInThere = fread(&data2, sizeof(data2), i, fp2); 
            printf("whatsInThere %zu\n", whatsInThere); 
                if (amount <= i ) // if over total added else 
                {  
                    // read entire file using i  
                    fread(data2, sizeof(data2), i, fp2); 
                    int d = 0; // do not go over amount wanted. 
                            for (; d < amount; d++) 
                            { // start where said to start 
                                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++; // move it down the array elements.  
                                  
                            }     
                            printf("\n");                     
                                  
                }         
            else 
            { 
                printf(" woo dude not valid, good-bye\n"); 
                fclose(fp2); 
                return -1; 
            } 
        } 
        else 
        { 
            printf("fine c ya\n"); 
        } 
                 
        fclose(fp2); 
        remove(BINFILE); // get a clean file each run 
     return 0; 
      
    }
    is that not (mostly) your code??

    you might want to get a look at how it is being printed out, getting the sizes and everything else it is doing, and opening the file with wb+ ( read write binary)
    Last edited by userxbw; 11-26-2017 at 03:19 PM.

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    yes that's it but that the program wasn't finished , i added some new stuff in there , in that other post it was just reading and writing in the file while program is open . now i added a couple of conditions to the inputs and , i'm trying to read data saved int he file after program closes and opens again.And please elaborate on those design flaws , thanks.

  8. #8
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    also , what do u mean getting the sizes ? cause i tried switch it from ans-1 to i and even make fread , read the whole data2 and tried to print data2 element by element to see if maybe it stored somewhere in there i still got the wrong output , can you please elaborate on the sizes thing

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    well for one forget about what I said about sizes . my mistake. but your prototypes are wrong, that might have something to do with it
    Code:
    // Function Prototypes
    void findRoot(INFO[], int , double * );
    void plotFunc(double, double, int, double []);
    double func(INFO [],int , double);
    void plot(int,  double[], double[], int, double[], double[]);
    double getMin(double *, int );
    double getMax(double *, int );
    Code:
    gcc -Wall -Wextra -Wpedantic -lm -o "read_write_binary_001" "read_write_binary_001.c" (in directory: /home/userx/bin)
    read_write_binary_001.c: In function 'findRoot':
    read_write_binary_001.c:165:1: error: parameter name omitted
     void findRoot(INFO[], int , double * )
     ^
    read_write_binary_001.c:165:1: error: parameter name omitted
    read_write_binary_001.c:165:1: error: parameter name omitted
    Compilation failed.
    I made a dummy function using your prototype to get that for you.

    MOD:

    regardless writing manipulating your data then writing to it then reading from the same file what you do to it in the middle has nothing to do with how you get it out of the file then print it out.
    this is suppose to be passing a value back out of the prams then printing it?
    Code:
    findRoot(data2,ans,&root);
    
    printf("%lf\n",data2[ans-1].l);
    Last edited by userxbw; 11-26-2017 at 04:34 PM.

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kamal Joub View Post
    so i made a program , where the input of the user is saved in a binary file , what i want to do tho is use the values saved in the binary after the program is restarted , meaning use previously inputted values.

    However , when i test to see if the program read he correct values from the binary file , i keep getting 0. The following is the code related only to this issue please ignore the prototypes thanks.

    btw , i am also trying to check if the file exist and contain elements(Hence the != NULL in the beginning of main) to display it to the user but NULL doesn't seem to do the trick since even if the bin file don't exist it prints the statement indicated.So , please if possible help with that too but the first issue is the priority.
    what'a ya mean by keep getting 0?

    to check for a file
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <unistd.h>
     
    #define N 100  // number of points
    #define TRUE 1
    #define FALSE 0
    #define EPSILON 1e-30
    #define BINFILE "file.bin"
    #define pi 3.1415926535897932384626433832795
     
     
    typedef struct
    {
     
     
       double l; // starting time
       double c; // ending time
       double v; // velocity
       double td; // time
       double pc; // altitude
     
     
    } INFO;
     
     
     
    int main()
    {
     
     
     if( access( BINFILE, F_OK ) != -1 )
     {
        // file exists
        printf("yep its here\n");
    }
     else
     {
        // file doesn't exist
        printf("no file exist\n");
        return -1;
    }
    Code:
    userx@slackwhere:~/bin
    $ rm file.bin
    userx@slackwhere:~/bin
    $ ./read_write_binary_001
    no file exist
    Last edited by userxbw; 11-26-2017 at 04:53 PM.

  11. #11
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    I doubt the prototypes are the problem since what i am printing here as a test is only related tot he main function i am not printing anything from the functions , also i noticed in your 2nd code that all the errors regarding the reading are coming from the other functions not main but the other functions aren't supposed to read anything from bin file , then how am i still getting a wrong value when printing it after restarting the program.Also , yes that findRoot should pass the root value back and printing it is working just fine when before restarting the program , then the wrong values are read from the bin file.

    and I mean by getting zero , that after closing the program and opening it again the output of printf is not the value of data2[ans-1].l , i get an output of zero, which i assume is a garbage value and is caused by misreading the bin file

    I feel i'm missing something , I mean i wish i could read in .txt the contents of the bin file to see what's stored but if u remember from the other post , we've established that the program writes and reads in and from the bin file correctly so the problem comes after closing it and restarting , how come the values read into it are printed out correctly , i feel it's 1 of 2 possibilites , either I am reading the wrong part of the file(doubtful) or the values are getting erased after closing the program which would weird since i have no Remove(BINFILE) or any such commands in my program.
    Last edited by Kamal Joub; 11-26-2017 at 05:00 PM.

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kamal Joub View Post
    I doubt the prototypes are the problem since what i am printing here as a test is only related tot he main function i am not printing anything from the functions , also i noticed in your 2nd code that all the errors regarding the reading are coming from the other functions not main but the other functions aren't supposed to read anything from bin file , then how am i still getting a wrong value when printing it after restarting the program.Also , yes that findRoot should pass the root value back and printing it is working just fine when before restarting the program , then the wrong values are read from the bin file.
    they are a problem for me, when I try to compile it to run it to see what is really going on well them bad prototypes get in the way. and that last function call looked important. and I have no idea what your functions are doing to get a perspective if they maybe hindering you in some way or not.

    as far are you bin file I found that I have to delete mine from time to time because they get corrupted for whatever reason. just for to find out, delete your bin file make a new one with your program and then try it again to see what you see if you have not already tried that.

    pd look at post 10 if you haven't -- how to check if file is present or not.

  13. #13
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    Quote Originally Posted by userxbw View Post
    they are a problem for me, when I try to compile it to run it to see what is really going on well them bad prototypes get in the way. and that last function call looked important. and I have no idea what your functions are doing to get a perspective if they maybe hindering you in some way or not.

    as far are you bin file I found that I have to delete mine from time to time because they get corrupted for whatever reason. just for to find out, delete your bin file make a new one with your program and then try it again to see what you see if you have not already tried that.
    I have tried deleting the bin file several times , it's not corrutped per say , it's just the values start overlapping so u end up reading the same values over and over. Here is my full code so far , to give you a better perspective

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    #define N 100  // number of points
    #define TRUE 1
    #define FALSE 0
    #define EPSILON 1e-30
    #define BINFILE "file.bin"
    #define pi 3.1415926535897932384626433832795
    
    
    typedef struct
    {
    
    
       double l; // starting time
       double c; // ending time
       double v; // velocity
       double td; // time
       double pc; // altitude
    
    
    } INFO;
    
    
    // Function Prototypes
    void findRoot(INFO[], int , double * );
    void plotFunc(double, double, int, double []);
    double func(INFO [],int , double);
    void plot(int,  double[], double[], int, double[], double[]);
    double getMin(double *, int );
    double getMax(double *, int );
    
    
    
    
    int main()
    {
        int i=0;
        int flag = FALSE;
        int flag2;
        int ans;
        INFO data2[5];
        FILE *fp2;
        double root , R;
        double check , check2;
        double f;
        char ans2;
    
    
         if (( fp2 = fopen(BINFILE, "wb")) != NULL )
         {
             printf("Do you want to choose previously inputted values? (y/n)\n");
             fflush(stdin);
             scanf("%c",&ans2);
    
    
             if (ans2 == 'y')
                flag = TRUE;
    
    
                fp2 = fopen(BINFILE, "wb");
        }
    
    
        while(i < 5 && flag==FALSE)
        {
          do
          {
              flag2 = TRUE;
            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);
    
    
            R = (-2* data2[i].l * log(data2[i].pc))/data2[i].td;
            check = 1/(data2[i].l*data2[i].c);
            check = check - pow((R/(2*data2[i].l)),2) ;
            check2 = sqrt(check);
            f = check2/(2*pi);
            f= 50/f;
    
    
            if(check < 0)
            {
               flag2 = FALSE;
               printf("Invalid input , Please enter a bigger value for td\n");
            }
            if(f < data2[i].td)
            {
                flag2 = FALSE;
                printf("Frequency too high; plot may be distorted\n");
            }
    
    
    
    
          }while(flag2 == FALSE);
    
    
    
    
            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 use?\n");
            scanf("%d",&ans);
    
    
        fclose(fp2);
    
    
        fp2 = fopen(BINFILE, "rb");
        // compensate for zero element.
        if ( ans-1 > 6)
        {
            printf("out of bounds\n");
            return -1;
        }
        else
        {
            fread(&data2[ans-1].l, sizeof(double), 1, fp2);
            fread(&data2[ans-1].c, sizeof(double), 1, fp2);
            fread(&data2[ans-1].v, sizeof(double), 1, fp2);
            fread(&data2[ans-1].td, sizeof(double), 1, fp2);
            fread(&data2[ans-1].pc, sizeof(double), 1, fp2);
        }
    
    
    
    
    
    
    
    
    printf("%lf\n",data2[ans-1].l);
    findRoot(data2,ans,&root);
    
    
    fclose(fp2);
    return 0;
    
    
    }
    
    
    void findRoot(INFO data2[] , int sel , double *root)
    {
    sel = sel -1;
        double upper = sqrt(4*data2[sel].l/data2[sel].c) ;
        double lower = 0;
        double x0 , x1 ;
        double sol , sol1;
    
    
        x0=upper;
        x1= lower;
    
    
        sol = func(data2,sel,x0);
        sol1 = func(data2,sel,x1);
    
    
    if(func(data2,sel,x0)* func(data2,sel,x1) < 0)
    {
        do
      {
    
    
        *root = (x0+x1)/2;
    
    
        if(func(data2,sel,*root)*func(data2,sel,x1) < 0 )
         x0 = *root;
        else x1 = *root;
    
    
    
    
    
    
    }while(fabs(func(data2,sel,*root)*func(data2,sel,x1)) >  EPSILON);
    
    
    }
    
    
    
    
    
    
    
    
    }
    
    
    double func(INFO data[] ,int sel , double x)
    {
       double root;
    
    
    
    
    
    
       root = exp(-(x*data[sel].td)/(2*data[sel].l));
       root = root - data[sel].pc ;
    
    
    
    
    
    
     return(root);
    
    
    
    
    }
    when i run the program and created a bin file , then close it and try to read from it again , between closing the program and restarting it something seems to go wrong and also Thanks I have checked post 10 , the file is present in this case.
    Last edited by Kamal Joub; 11-26-2017 at 05:04 PM.

  14. #14
    Banned
    Join Date
    Aug 2017
    Posts
    861
    let me see if I can strip some of your code out to get to just open write close reopen read print, you could just do a rewind on it and see how that works for you.

  15. #15
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    I did have a rewind , but i changed it up while experimenting to see if it was causing any error , feel free to change it to that if it would help you in anyway to figure out the problem in the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-27-2013, 12:24 PM
  2. Replies: 0
    Last Post: 03-21-2013, 03:31 PM
  3. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  4. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  5. Replies: 13
    Last Post: 05-31-2009, 11:30 AM

Tags for this Thread