C Program - 'Junk value' appearing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dharanidhar
    New Member
    • Apr 2008
    • 7

    C Program - 'Junk value' appearing

    #include<stdio. h>
    #include<math.h >

    main()
    {
    FILE * fp;
    struct
    {
    int a;
    float b;
    }file;
    fp = fopen("Y.TXT"," w+");
    printf("enter the value of a :");
    scanf("%d",&fil e.a);
    printf("\n");
    file.b =sin(file.a);
    printf("%d",fil e.a);
    printf("\n");
    printf("%f",fil e.b);
    fwrite(&file,si zeof(file),1,fp );
    fclose(fp);
    }


    i am getting some garbage value into the file.can you tell me what may be the problem?
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    The junk is becos you write a float value to the file and u use fwrite to write the information.
    If u use fwrite then use wb+ when opening the file.


    Thanks
    raghuram

    Comment

    • dharanidhar
      New Member
      • Apr 2008
      • 7

      #3
      thank you for your reply

      i have used wb+ as i am using fwrite.but i am getting the same junk value as before

      i am using quincy C compiler

      I could get the output in the file when i used fprintf()

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by dharanidhar
        thank you for your reply

        i have used wb+ as i am using fwrite.but i am getting the same junk value as before

        i am using quincy C compiler

        I could get the output in the file when i used fprintf()

        Try to open the file after writing using fopen and reag the contents using fread.
        If it is having proper value then it is not junk in the file.


        Raghuram

        Comment

        • dharanidhar
          New Member
          • Apr 2008
          • 7

          #5
          #include<stdio. h>
          #include<math.h >

          main()
          {
          FILE * fp;
          int i;
          int m[20];
          struct
          {
          int a;
          float b;
          }file;
          fp = fopen("Y.TXT"," wb");
          printf("enter the value of a :");
          scanf("%d",&fil e.a);
          printf("\n");
          file.b =sin(file.a);
          printf("%d",fil e.a);
          printf("\n");
          printf("%f\n",f ile.b);
          fwrite(&file,si zeof(file),1,fp );
          fclose(fp);
          fp = fopen("Y.TXT"," r");
          fread(m,1,2,fp) ;
          for(i = 0;i <= 1;i++)
          printf("%d",*(m +i));
          }

          i have tried as u suggested me that is also giving junk value.......

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Please note that the 'junk value' happens to be the content of your structure in
            binary form. Did you expect some textual representation of the int and float
            values in your file? If so, don't use write but fprintf each struct member to the file.

            kind regards,

            Jos

            Comment

            • gpraghuram
              Recognized Expert Top Contributor
              • Mar 2007
              • 1275

              #7
              You should try opening the file in binary mode and when using fread use the same syntax what u have used for fwrite.
              Then you will know its not a junk.

              Raghu

              Comment

              Working...