-1

I'm getting an error with this code. The output looks like this: enter image description here

Why is the output half normal?

I want the output to be:

2 ppa 90 90 98 89 49 83.20 B
junior 90 90 90 90 90 90.00 A

Here is my code:

void class_result(){
     system("cls");
     fptr=fopen("stdnin.txt","r");
     if(fptr==NULL)
     {
       printf("ERROR!!! FILE COULD NOT BE OPEN\n\n\n Go To Entry Menu to create File");
       printf("\n\n\n Program is closing ....");
       getch();
       exit(0);
     }

      printf("\n\n\t\tALL STUDENTS RESULT \n\n");
      printf("====================================================\n");
      printf("R.No.  Name       P   C   M   E   CS  %age   Grade\n");
      printf("====================================================\n");
//  while(fscanf(fptr,"%d %s %d %d %d %d %d %.2f %c",&st.rollno,st.name,&st.p_marks,&st.c_marks,&st.m_marks,&st.e_marks,&st.cs_marks,&st.per,st.grade)!=EOF){


 while(fread(&st,sizeof(st),1,fptr))
   {
       printf("%-6d %-10s %-3d %-3d %-3d %-3d %-3d %-3.2f %-2c\n",st.rollno,st.name,st.p_marks,st.c_marks,st.m_marks,st.e_marks,st.cs_marks,st.per,st.grade);
     }
//      while(fread(&st,sizeof(st),1,fptr))
//        printf ("%d %s \n",st.rollno, st.name);
 fclose(fptr);
     getch();
}
indiv
  • 17,306
  • 6
  • 61
  • 82
  • 2
    are you using windows? then you should open your file in binary mode `fptr=fopen("stdnin.txt","rb");` because else it converts linefeeds to CR+LF and corrupts the output. note: I'm sure you're using windows: cls & getch are dead giveaway. – Jean-François Fabre Jan 04 '18 at 22:52
  • 1
    What is 'st'? Data is more important than code. – Martin James Jan 04 '18 at 22:52
  • 1
    Possible duplicate of [How to read a binary file in c? (video, images, or text)](https://stackoverflow.com/questions/2307057/how-to-read-a-binary-file-in-c-video-images-or-text) – Jean-François Fabre Jan 04 '18 at 22:53
  • I'm not using binary .. i have txt data to read – Agrow Growan Jan 04 '18 at 22:57
  • st is from struct data – Agrow Growan Jan 04 '18 at 22:58
  • full code https://pastebin.com/0tsyNx6W – Agrow Growan Jan 04 '18 at 23:00
  • You are writing text but reading as if it's memory. Ask your instructor about reading text files raw and then converting strings to the in-memory values. atoi() is the function you'll use, or similar types. This is not a problem with the code, but a fundamental understanding behind what's going on. – Stephen J Jan 04 '18 at 23:05
  • I haven't instructor yet ,,, – Agrow Growan Jan 04 '18 at 23:08
  • maybe you can give me a sugest .. – Agrow Growan Jan 04 '18 at 23:10
  • `fread` is used to read binary data. `fgets`, `getchar`, `fscanf`, etc are used to read text. (note the `scanf` family of functions is full of traps for the unwary new C programmer). Each function has a return. ***validate*** all input by checking the return of whatever function you use. Suggest starting with `fgets` and do not use `atoi` for string to integer conversions -- it provides ZERO error checking, use `strtol` instead. Always consult the *man page* for any function you use (even after you think you know it cold) – David C. Rankin Jan 04 '18 at 23:16
  • i've tried using fscanf but still error ... same ... while(fscanf(fptr,"%d %s %d %d %d %d %d %.2f %c",&st.rollno,st.name,&st.p_marks,&st.c_marks,&st.m_marks,&st.e_marks,&st.cs_marks,&st.per,st.grade)!=EOF){ – Agrow Growan Jan 04 '18 at 23:19
  • https://pastebin.com/0tsyNx6W – Agrow Growan Jan 04 '18 at 23:20
  • In `printf("R.No. Name P C M E CS %age Grade\n");` the `%` is a typo or do you want to print that symbol? – Bob__ Jan 04 '18 at 23:22
  • @Bob__ yeah typo .. the important is i want data below is showing well .. ;D – Agrow Growan Jan 04 '18 at 23:25
  • Could you please edit your question adding some lines of the input file? – Bob__ Jan 04 '18 at 23:30
  • here pastebin.com/0tsyNx6W – Agrow Growan Jan 04 '18 at 23:31
  • @AgrowGrowan what will be most helpful for allowing me to help you is to post any existing `"stdnin.txt"` file that you are required to start with (meaning if it already has data you are supposed to read and then add to), if you are creating it from scratch, let me know that too. – David C. Rankin Jan 05 '18 at 00:23
  • Don't include an image for pure text output. – Jongware Jan 05 '18 at 00:39

1 Answers1

0

You have to make up your mind if you want to read/write the file in text mode or in binary mode. You cannot open the file in text mode and write in binary mode, or vice versa.

To read in binary mode, open the file as binary (use "rb" instead of "r"), use fread and fwrite. If your file already contains text data then you must delete that file and start with a new one.

fptr = fopen("stdnin.txt", "ab+");
fwrite(&st,sizeof(st),1,fptr);
fclose(fptr);
...

fptr = fopen("stdnin.txt", "rb");
while(fread(&st, sizeof(st), 1, fptr))
    printf...;

To read/write in text mode, open the file in text mode, use fscanf(fptr, ...) and fprintf(fptr, ...). Again, if your file contains binary data then you must delete the file start with a new file for text input.

To read text input from console you can use scanf, not gets

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77