What is wrong in this program ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pereges

    What is wrong in this program ?

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    FILE *fp;
    int i, n;
    typedef struct student_record
    {
    char name[40];
    int sid;
    }student;

    student s;
    /* I presume this creates a new file if one already doesn't exist */
    fp = fopen("student. dat", "ab+");
    if(fp == NULL)
    {
    perror("file can't be opened\n!");
    exit(EXIT_FAILU RE);
    }
    else
    {

    printf("How many records you want to write ?\n");
    scanf("%d", &n);
    for(i=0; i<n; i++)
    {
    printf("Enter record %d: student name student
    id\n", i);

    /* There seems to be some problem here and
    its obvious during run time */
    scanf("%s %d", s.name, &s.sid);
    fwrite(&s, sizeof(s),1, fp);
    }

    }

    /* doesn't print at all ? */
    while(fread(&s, sizeof(s), 1, fp) == 1)
    printf(" %s %d \n", s.name, &s.sid);

    return 0;

    }

    +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++

    The o/p I get :

    How many records you want to write ?
    3
    Enter record 0: student name student id
    Nathan Reed 5
    Enter record 1: student name student id
    Enter record 2: student name student id
  • William Pursell

    #2
    Re: What is wrong in this program ?

    On 11 Apr, 07:48, pereges <Brol...@gmail. comwrote:
    fp = fopen("student. dat", "ab+");
    if(fp == NULL)
    {
    perror("file can't be opened\n!");
    exit(EXIT_FAILU RE);
    }
    Consider the user running the program encountering
    the error message:

    file can't be opened
    :No such file or directory

    As a user, I will be really annoyed that this
    message doesn't give me the pathname of the file
    in question. (and slightly annoyed by the extra newline
    before the ':')

    Please consider writing this as:
    fp = fopen( filename = "whatever", ...)
    ....
    perror( filename );

    Or, if you really feel it necessary to be
    excessively verbose:

    fprintf( stderr, "%s can't be opened:", filename );
    perror( NULL );

    (or "%s can't be opened: %s", filename, strerror( errno )
    ....whatever you prefer, just make sure the filename
    is in the error message.)

    Comment

    • Eric Sosman

      #3
      Re: What is wrong in this program ?

      William Pursell wrote:
      [...]
      Or, if you really feel it necessary to be
      excessively verbose:
      >
      fprintf( stderr, "%s can't be opened:", filename );
      perror( NULL );
      Don't do it that way, because perror() reports the
      error that errno indicates, and fprintf() might change
      errno. You could wind up with

      flatcat.dat can't be opened: not a typewriter

      (See Question 12.24 in the FAQ.)

      One way to deal with this is to save and restore errno's
      value around the fprintf() call:

      #include <errno.h>
      ...
      {
      int errno_save = errno;
      fprintf(stderr, "%s can't be opened: ", filename);
      errno = errno_save;
      perror(NULL);
      ...

      .... but in a case like this the advice to use
      (or "%s can't be opened: %s", filename, strerror( errno )
      .... seems better.

      --
      Eric.Sosman@sun .com

      Comment

      Working...