PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > What is wrong in this program ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
What is wrong in this program ?

Réponse
 
LinkBack Outils de la discussion
Vieux 11/04/2008, 07h48   #1
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 11/04/2008, 21h03   #2
William Pursell
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is wrong in this program ?

On 11 Apr, 07:48, pereges <Brol...@gmail.com> wrote:

> 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.)
  Réponse avec citation
Vieux 11/04/2008, 21h22   #3
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19208 seconds with 11 queries