Thread: reading from bin file

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    original charge pc to reach within td
    1
    writting to file
    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 use?
    1
    closing file
    reopning file
    answer 1
    in while 1
    1.000000
    
    1.000000
    userx@slackwhere:~/bin
    $ 
    userx@slackwhere:~/bin
    $ ./read_write_binary_001
    yep its here
    Do you want to choose previously inputted values? (y/n)
    y
    which set of values do you want to use?
    1
    closing file
    reopning file
    answer 1
     
    0.000000
    userx@slackwhere:~/bin
    $ cat file.bin
    //empty file upon reopening on second run.
    seeings how it is on a second run it maybe getting over written with nothing open close reopen might be clearing out your file.

  2. #17
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    well , even by switching fopen to a "wb+" and by removing the close(fp2) and adding a rewind just like before I keep getting a 0 output on the second run here's the code now:
    Code:
    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[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;
    
    
    }
    How can I prevent data loss after closing and reopening the file?

  3. #18
    Banned
    Join Date
    Aug 2017
    Posts
    861
    you might want to comment out a chunk of your code then work on getting information saving then reading your bin file printing it out again. then when you figure out how to fix that and get it fixed then move on to bigger things, and when wrong input is gotten how to re-ask for the proper input before moving on with getting more information.

  4. #19
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    that's what I am doing , is it possible that the program is not writing into the binary files ? thats why i get garbage values when i do the second run ? , I have been trying to figure this out all day , I tried all my ideas and all of the suggestions here and I am getting nowhere , Idk what to do anymore

  5. #20
    Banned
    Join Date
    Aug 2017
    Posts
    861
    change all of your fopens to
    Code:
    if ( (fp2 = fopen(BINFILE, "ab+"))
    then run it and see if that helps ab+
    becase I was not getting anything then I did that and got a return
    Code:
    userx@slackwhere:~/bin
    $ ./read_write_binary_001
    yep its here
    Do you want to choose previously inputted values? (y/n)
    y
    in yes Tell = 40
    which set of values do you want to use?
    1
    closing file
    reopning file
    answer 1
    1.000000
    1.000000
    1.000000
    1.000000
    1.000000
    in while 1
    1.000000

  6. #21
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    yes it works now but what I would like to know why wouldn't it work with wb and rb ? because I didn't learn about ab+ in my programming class so I am not allowed to use it since it wasn't part of the course

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    i should have probably said something before, but don't do this:
    Code:
         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");
    
        }
    All that this does is open the file twice, and leak the first handle that you potentially got in the if(). Leaking file handles is potentially a problem, as the computer has a limited number available to begin with.

    Better would be this:
    Code:
    fp2 = fopen(BINFILE, "wb");
    if (fp2 != NULL) 
    {
             printf("Do you want to choose previously inputted values? (y/n)\n");
             fflush(stdin);
             scanf("%c",&ans2);
     
             if (ans2 == 'y')
                flag = TRUE;
    }
    Also what if the file doesn't open? You should probably display an error and exit normally.

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Kamal Joub View Post
    yes it works now but what I would like to know why wouldn't it work with wb and rb ? because I didn't learn about ab+ in my programming class so I am not allowed to use it since it wasn't part of the course
    "ab" mode may simply be what you wanted. "w" mode technically truncates a file (clears it) if it exists prior to opening for writing. This would erase information from previous runs of the program.

  9. #24
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kamal Joub View Post
    yes it works now but what I would like to know why wouldn't it work with wb and rb ? because I didn't learn about ab+ in my programming class so I am not allowed to use it since it wasn't part of the course
    ........ on him/her, it works ... it is in the documentation. so go to the head of the class for actually going out of your way to get someone else to fix it for you. wink wink, but now you know, so it's all good. and if he or she got a problem with that have them call me.

    Code:
     The fopen() function opens the file whose name is the string pointed
           to by pathname and associates a stream with it.
    
           The argument mode points to a string beginning with one of the
           following sequences (possibly followed by additional characters, as
           described below):
    
           r      Open text file for reading.  The stream is positioned at the
                  beginning of the file.
    
           r+     Open for reading and writing.  The stream is positioned at the
                  beginning of the file.
    
           w      Truncate file to zero length or create text file for writing.
                  The stream is positioned at the beginning of the file.
    
           w+     Open for reading and writing.  The file is created if it does
                  not exist, otherwise it is truncated.  The stream is
                  positioned at the beginning of the file.
    
           a      Open for appending (writing at end of file).  The file is
                  created if it does not exist.  The stream is positioned at the
                  end of the file.
    
           a+     Open for reading and appending (writing at end of file).  The
                  file is created if it does not exist.  The initial file
                  position for reading is at the beginning of the file, but
                  output is always appended to the end of the file.
    if you're not allowed to show some kind of prowess in your programming skills in that class. I'd get a refund.
    Last edited by userxbw; 11-26-2017 at 08:24 PM.

  10. #25
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    it's actually a university course towards my degree , a refund is kinda out of the question hahahahah, anyways I don't think it will a problem the professor is chill but I just wanted to know if alternatives were available just in case , anyways thanks for all the help guys

  11. #26
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kamal Joub View Post
    it's actually a university course towards my degree , a refund is kinda out of the question hahahahah, anyways I don't think it will a problem the professor is chill but I just wanted to know if alternatives were available just in case , anyways thanks for all the help guys
    did he actually say no you cannot excel in this class, you can only use covered material?

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Kamal Joub View Post
    it's actually a university course towards my degree , a refund is kinda out of the question hahahahah, anyways I don't think it will a problem the professor is chill but I just wanted to know if alternatives were available just in case , anyways thanks for all the help guys
    Well, there are always alternatives, but they will be harder than simply using the right mode. One approach would be to open the file in "rb+" mode first. If it fails, then you know the file doesn't exist, and then you can safely try again with "wb" mode. If the file does open in "rb+" mode, you have a file, but will need to seek to the end before doing any writes.

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