Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What is wrong in this program ?

Reply
Thread Tools

What is wrong in this program ?

 
 
pereges
Guest
Posts: n/a
 
      04-11-2008
#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



 
Reply With Quote
 
 
 
 
Martin
Guest
Posts: n/a
 
      04-11-2008
On Fri, 11 Apr 2008 07:43:40 +0100, pereges <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
pereges
Guest
Posts: n/a
 
      04-11-2008
On Apr 11, 12:58 pm, Martin <(E-Mail Removed)> 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.

 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      04-11-2008
pereges schrieb:
> On Apr 11, 12:58 pm, Martin <(E-Mail Removed)> 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 [(E-Mail Removed)]


 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      04-11-2008
pereges wrote:
>
> On Apr 11, 12:58 pm, Martin <(E-Mail Removed)> 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: <(E-Mail Removed)>


 
Reply With Quote
 
lawrence.jones@siemens.com
Guest
Posts: n/a
 
      04-13-2008
Kenneth Brody <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what's wrong with my program? help!!!! mailtumengyu@gmail.com Java 7 04-12-2006 05:01 AM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
what's wrong in this program VisionSet Java 12 09-28-2004 09:16 PM
Add/Remove Program Glitch: Asks If I Want To To Remove Wrong Program ? Robert11 Computer Support 6 08-02-2004 09:02 PM
Program groups for indiviual users pointing to wrong directory Meglo3k1 Computer Support 1 05-30-2004 04:02 AM



Advertisments