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, 07h43   #1
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut What is wrong in this program ?

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

#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, 08h58   #2
Martin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is wrong in this program ?

On Fri, 11 Apr 2008 07:43:40 +0100, pereges <Broli00@gmail.com> wrote:
> #include <stdio.h>
> #include <stdlib.h>
>
> #include <stdio.h>
> #include <stdlib.h>


No need to include these twice.


> /* 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);
> }


%s scans for non-white space characters in the input. "Nathan Reed"
contains whitespace. You're better off reading in a line at a time; then
parse the read-in line for the data you want. scanf() is better suited to
well-formed data, which user input isn't. Perhaps try fgets() (eschew
gets() which is dangerous).


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


The third argument to printf should be s.sid, not &s.sid. I get a few
screenfuls of zeros (after I fix the third argument).

--
Martin

  Réponse avec citation
Vieux 11/04/2008, 10h00   #3
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is wrong in this program ?

On Apr 11, 12:58 pm, Martin <m...@b.c> wrote:


> The third argument to printf should be s.sid, not &s.sid. I get a few
> screenfuls of zeros (after I fix the third argument).


I tried to create the file and wrote some records on it. Then I
commented the bit where data is written and the program can display
the records without any mistakes. guess you just can't do a read just
after write.

  Réponse avec citation
Vieux 11/04/2008, 11h01   #4
Irrwahn Grausewitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is wrong in this program ?

pereges schrieb:
> On Apr 11, 12:58 pm, Martin <m...@b.c> wrote:
>
>
>> The third argument to printf should be s.sid, not &s.sid. I get a few
>> screenfuls of zeros (after I fix the third argument).

>
> I tried to create the file and wrote some records on it. Then I
> commented the bit where data is written and the program can display
> the records without any mistakes. guess you just can't do a read just
> after write.


You can. Look up the fseek function in your favourite C standard
library reference.

--
Irrwahn Grausewitz [irrwahn35@freenet.de]


  Réponse avec citation
Vieux 11/04/2008, 16h27   #5
Kenneth Brody
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is wrong in this program ?

pereges wrote:
>
> On Apr 11, 12:58 pm, Martin <m...@b.c> wrote:
>
> > The third argument to printf should be s.sid, not &s.sid. I get a few
> > screenfuls of zeros (after I fix the third argument).

>
> I tried to create the file and wrote some records on it. Then I
> commented the bit where data is written and the program can display
> the records without any mistakes. guess you just can't do a read just
> after write.


Of course you can, as long as the file is open in read/write mode
(which "ab+" does). You just have to remember that, after any
read or write, the current position in the file in the character
immediately after the last one read/written. In your case, you
just wrote off the end-of-file, meaning there is nothing after
it to be read.

Look up the fseek() function, which lets you change the current
position.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com>


  Réponse avec citation
Vieux 13/04/2008, 22h19   #6
lawrence.jones@siemens.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What is wrong in this program ?

Kenneth Brody <kenbrody@spamcop.net> wrote:
> pereges wrote:
>>
>> I tried to create the file and wrote some records on it. Then I
>> commented the bit where data is written and the program can display
>> the records without any mistakes. guess you just can't do a read just
>> after write.

>
> Of course you can, as long as the file is open in read/write mode
> (which "ab+" does).


And you call fflush() or a file positioning function like fseek() when
switching from writing to reading or vice versa (7.19.5.4p6).

-Larry Jones

The real fun of living wisely is that you get to be smug about it. -- Hobbes
  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 12h51.


É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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15725 seconds with 14 queries