|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
#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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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] |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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> |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|