c - Why is my program stopping before reading the txt file? -


my code keeps closing before reading file, made comment closes. know why wont work? showed lecturer couldn't figure out , had leave wondering if here figure out!

#include <stdio.h> #include <stdlib.h> #define result_max = 100; #define result_min = 0;  int main()  {      int studentid;     char firstname[20];     char lastname[20];     int result;      file *fptr;      if ((fptr = fopen("student.txt", "w")) == null)     {             printf("file not opened\n");             //exit(0);     }     else     {         printf("enter id, first name, last name , result\n");         scanf("%d %s %s %d", &studentid, firstname, lastname, &result);         while(!feof(stdin) )         {             fprintf(fptr, "%d %s %s %d\n", studentid, firstname, lastname, result);             scanf("%d %s %s %d", &studentid, firstname, lastname, &result);         }          fclose(fptr);     }//else end  // program ends here , wont continue!       if ((fptr = fopen("student.txt", "r")) == null)     {             printf("file not opened\n");             //exit(0);     }     else     {         printf("id, first name, last name, result ");         fscanf(fptr, "%d %s %s %d", &studentid, firstname, lastname, &result);          while(!feof(fptr) )         {             printf("%d %s %s %d \n", studentid, firstname, lastname, result);             fscanf(fptr, "%d %s %s %d", &studentid, firstname, lastname, &result);          }//end while     fclose( fptr );     }//end if     } 

the problem line: 'while(!feof(stdin) )'   because feof() becomes valid when program  tries read past end of 'stdin' file.     cannot accomplished  suggest  modifying program to: 1) read local buffer[] array, in loop,  2) using fgets() loop control 3) have leading 'q' (or similar) indication of having read input 4) output prompt every input line 5) if going parse fields, parse them using     strtok()/atoi() strncpy() strncpy() atoi()    or perhaps    sscanf() 6) check first char of input buffer[] end marker (the 'q' above)    exit input loop before parsing  there similar problem  loop reading file i.e. not use feof() loop control     rather use fgets()  differentiate each line of input written file, modify fprintf(fptr, ... ) format string include trailing '\n' 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -