What is wrong in this program ?

P

pereges

#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
 
M

Martin

#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).
 
P

pereges

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.
 
I

Irrwahn Grausewitz

pereges said:
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.
 
K

Kenneth Brody

pereges said:
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:[email protected]>
 
L

lawrence.jones

Kenneth Brody said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top