Re: problem
From: Pascal Bourguignon (spam_at_mouse-potato.com)
Date: 09/27/05
- Previous message: Eric Sosman: "Re: makedepend warning on #error"
- In reply to: hirenshah.05_at_gmail.com: "problem"
- Next in thread: Wayne C. Morris: "Re: problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 27 Sep 2005 18:03:29 +0200
hirenshah.05@gmail.com writes:
> I am trying to a program which takes the data from user and write in
> file. I have written the code below. but when i am displaying data from
> file; only first data is correct; others are random. i think there is
> problem while writing in file.
>
>
>
> struct st
> {
> char ch[60];
> int n;
> };
>
> int main()
> {
> st s;
> printf("enter");
> scanf("%s",s.ch);
If the user types:
I-am-trying-to-do-a-program-which-takes-the-data-from-user-and-write-in-a-file.
then your program will crash because scanf will overwrite s.n and the
stack with the characters beyond the 60th.
NEVER use scanf "%s" !
It'd use fgets, or scanf("%59s",...);
> printf("enter no.");
> scanf("%d",&s.n);
> adddata(&s);
You've declared that main returns an integer so return it!
return(0);
> }
>
>
>
> add data(st *x)
what is the add type?
Why do you send us programs that don't even compile successfully?
> {
> int fd;
> fd=open("records",O_CREAT|O_RDWR,0644);
> close(fd);
And what would happen if some other process executed: rm records
right now? This open/close is useless!
> fd=open(path,O_CREAT|O_APPEND|O_RDWR);
What is path?
Why do you send us programs that don't even compile successfully?
> fdw=write(fd,x,sizeof(s));
What is s? What is fdw?
Why do you send us programs that don't even compile successfully?
> close(fd);
> }
Where is defined adddata?
Why do you send us programs that don't even compile successfully?
> Then i have created another function to read from file "records" and
> display all data entered.
>
> But when i am adding data , Only first data enter is correct ; rest of
> the data are random. I am not getting where is problem.
-- __Pascal Bourguignon__ http://www.informatimago.com/ Litter box not here. You must have moved it again. I'll poop in the sink.
- Previous message: Eric Sosman: "Re: makedepend warning on #error"
- In reply to: hirenshah.05_at_gmail.com: "problem"
- Next in thread: Wayne C. Morris: "Re: problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]