Block reading and writing using fread and fwrite in C











up vote
-1
down vote

favorite












I am trying to read employee information from the user and enter it into a file using fwrite() function and later i want to print the data contents on the screen using fread() to read from file and then print it.
When i am inside the program, this process is working absolutely fine but after program is exited and i access the file where the information has been stored, i see unreadable characters but in the program they are printed as normal english characters and digits.
Here is my code:



#include<stdio.h>

struct emp{
int id;
char name[30];
double salary;
}S;

void main(){
char fname[60];
printf("Enter file name: ");
scanf("%s", fname);

FILE *fptr = fopen(fname, "a+"); // open file in append + mode, create if not found.

if(fptr == NULL){
printf("Some error occured !n");
return;
}

int i, size;
printf("Enter the number of employees whose information is needed to be added to the file: ");
scanf("%d", &size);

// writing

for(i = 0 ; i < size ; i++){
printf("Employee %d:n", i+1);
printf("Enter id: ");
scanf("%d", &S.id);

printf("Enter name: ");
while(getchar() != 'n'); // clear buffer
scanf("%s", S.name);

printf("Enter salary: ");
scanf("%lf", &S.salary);

fwrite(&S, sizeof(struct emp), 1, fptr);
printf("----------------------------------------n");
}

rewind(fptr); // move pointer to first record in file

// reading
printf("File contents: n");

printf("IDttNAMEttSALARYn");

while(fread(&S, sizeof(struct emp), 1, fptr) != 0){
printf("%dtt%stt%lfn", S.id, S.name, S.salary);
}
}


Here is the picture of what i am trying to expalin.
enter image description here










share|improve this question


























    up vote
    -1
    down vote

    favorite












    I am trying to read employee information from the user and enter it into a file using fwrite() function and later i want to print the data contents on the screen using fread() to read from file and then print it.
    When i am inside the program, this process is working absolutely fine but after program is exited and i access the file where the information has been stored, i see unreadable characters but in the program they are printed as normal english characters and digits.
    Here is my code:



    #include<stdio.h>

    struct emp{
    int id;
    char name[30];
    double salary;
    }S;

    void main(){
    char fname[60];
    printf("Enter file name: ");
    scanf("%s", fname);

    FILE *fptr = fopen(fname, "a+"); // open file in append + mode, create if not found.

    if(fptr == NULL){
    printf("Some error occured !n");
    return;
    }

    int i, size;
    printf("Enter the number of employees whose information is needed to be added to the file: ");
    scanf("%d", &size);

    // writing

    for(i = 0 ; i < size ; i++){
    printf("Employee %d:n", i+1);
    printf("Enter id: ");
    scanf("%d", &S.id);

    printf("Enter name: ");
    while(getchar() != 'n'); // clear buffer
    scanf("%s", S.name);

    printf("Enter salary: ");
    scanf("%lf", &S.salary);

    fwrite(&S, sizeof(struct emp), 1, fptr);
    printf("----------------------------------------n");
    }

    rewind(fptr); // move pointer to first record in file

    // reading
    printf("File contents: n");

    printf("IDttNAMEttSALARYn");

    while(fread(&S, sizeof(struct emp), 1, fptr) != 0){
    printf("%dtt%stt%lfn", S.id, S.name, S.salary);
    }
    }


    Here is the picture of what i am trying to expalin.
    enter image description here










    share|improve this question
























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I am trying to read employee information from the user and enter it into a file using fwrite() function and later i want to print the data contents on the screen using fread() to read from file and then print it.
      When i am inside the program, this process is working absolutely fine but after program is exited and i access the file where the information has been stored, i see unreadable characters but in the program they are printed as normal english characters and digits.
      Here is my code:



      #include<stdio.h>

      struct emp{
      int id;
      char name[30];
      double salary;
      }S;

      void main(){
      char fname[60];
      printf("Enter file name: ");
      scanf("%s", fname);

      FILE *fptr = fopen(fname, "a+"); // open file in append + mode, create if not found.

      if(fptr == NULL){
      printf("Some error occured !n");
      return;
      }

      int i, size;
      printf("Enter the number of employees whose information is needed to be added to the file: ");
      scanf("%d", &size);

      // writing

      for(i = 0 ; i < size ; i++){
      printf("Employee %d:n", i+1);
      printf("Enter id: ");
      scanf("%d", &S.id);

      printf("Enter name: ");
      while(getchar() != 'n'); // clear buffer
      scanf("%s", S.name);

      printf("Enter salary: ");
      scanf("%lf", &S.salary);

      fwrite(&S, sizeof(struct emp), 1, fptr);
      printf("----------------------------------------n");
      }

      rewind(fptr); // move pointer to first record in file

      // reading
      printf("File contents: n");

      printf("IDttNAMEttSALARYn");

      while(fread(&S, sizeof(struct emp), 1, fptr) != 0){
      printf("%dtt%stt%lfn", S.id, S.name, S.salary);
      }
      }


      Here is the picture of what i am trying to expalin.
      enter image description here










      share|improve this question













      I am trying to read employee information from the user and enter it into a file using fwrite() function and later i want to print the data contents on the screen using fread() to read from file and then print it.
      When i am inside the program, this process is working absolutely fine but after program is exited and i access the file where the information has been stored, i see unreadable characters but in the program they are printed as normal english characters and digits.
      Here is my code:



      #include<stdio.h>

      struct emp{
      int id;
      char name[30];
      double salary;
      }S;

      void main(){
      char fname[60];
      printf("Enter file name: ");
      scanf("%s", fname);

      FILE *fptr = fopen(fname, "a+"); // open file in append + mode, create if not found.

      if(fptr == NULL){
      printf("Some error occured !n");
      return;
      }

      int i, size;
      printf("Enter the number of employees whose information is needed to be added to the file: ");
      scanf("%d", &size);

      // writing

      for(i = 0 ; i < size ; i++){
      printf("Employee %d:n", i+1);
      printf("Enter id: ");
      scanf("%d", &S.id);

      printf("Enter name: ");
      while(getchar() != 'n'); // clear buffer
      scanf("%s", S.name);

      printf("Enter salary: ");
      scanf("%lf", &S.salary);

      fwrite(&S, sizeof(struct emp), 1, fptr);
      printf("----------------------------------------n");
      }

      rewind(fptr); // move pointer to first record in file

      // reading
      printf("File contents: n");

      printf("IDttNAMEttSALARYn");

      while(fread(&S, sizeof(struct emp), 1, fptr) != 0){
      printf("%dtt%stt%lfn", S.id, S.name, S.salary);
      }
      }


      Here is the picture of what i am trying to expalin.
      enter image description here







      c






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 13:54









      Lakshya Munjal

      306




      306
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You are writing to the file the struct and not printing its contents separately. It works when you read it back, but when you open the file, it simply doesn't know what it is (you have int and char* and double in your struct.



          If you want to visualize it on the file, you need to print each term of the struct individually, and read it back the same way, in order to see it on screen.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239664%2fblock-reading-and-writing-using-fread-and-fwrite-in-c%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You are writing to the file the struct and not printing its contents separately. It works when you read it back, but when you open the file, it simply doesn't know what it is (you have int and char* and double in your struct.



            If you want to visualize it on the file, you need to print each term of the struct individually, and read it back the same way, in order to see it on screen.






            share|improve this answer

























              up vote
              0
              down vote













              You are writing to the file the struct and not printing its contents separately. It works when you read it back, but when you open the file, it simply doesn't know what it is (you have int and char* and double in your struct.



              If you want to visualize it on the file, you need to print each term of the struct individually, and read it back the same way, in order to see it on screen.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You are writing to the file the struct and not printing its contents separately. It works when you read it back, but when you open the file, it simply doesn't know what it is (you have int and char* and double in your struct.



                If you want to visualize it on the file, you need to print each term of the struct individually, and read it back the same way, in order to see it on screen.






                share|improve this answer












                You are writing to the file the struct and not printing its contents separately. It works when you read it back, but when you open the file, it simply doesn't know what it is (you have int and char* and double in your struct.



                If you want to visualize it on the file, you need to print each term of the struct individually, and read it back the same way, in order to see it on screen.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 14:33









                zediogoviana

                1388




                1388






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239664%2fblock-reading-and-writing-using-fread-and-fwrite-in-c%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation