468,315 Members | 1,476 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

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_FAILURE);
}
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
Apr 11 '08 #1
2 1350
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_FAILURE);
}
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.)
Apr 11 '08 #2
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.

--
Er*********@sun.com
Apr 11 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

137 posts views Thread by Philippe C. Martin | last post: by
1 post views Thread by Qiangning Hong | last post: by
46 posts views Thread by Keith K | last post: by
669 posts views Thread by Xah Lee | last post: by
3 posts views Thread by belton180 | last post: by
20 posts views Thread by Daniel.C | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.