Re: problem in reading from file
From: Ed Morton (morton_at_lsupcaemnt.com)
Date: 09/26/05
- Next message: hirenshah.05_at_gmail.com: "Re: problem in reading from file"
- Previous message: hirenshah.05_at_gmail.com: "problem in reading from file"
- In reply to: hirenshah.05_at_gmail.com: "problem in reading from file"
- Next in thread: hirenshah.05_at_gmail.com: "Re: problem in reading from file"
- Reply: hirenshah.05_at_gmail.com: "Re: problem in reading from file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 25 Sep 2005 17:35:42 -0500
hirenshah.05@gmail.com wrote:
> struct st{char ch[50];
> int n;
> }
>
> int main()
> {
> st *x,*y;
> int fd;
> scanf("%s\n%d",x.ch,x.n);
>
> fd=open("data",........)
> close(fd);
> fd=open("data",O_APPEND...)
> write(fd,&x,sizeof(st));
> close(fd);
> fd=open("data",...)
>
> // here i am facing problem. every time i run the program i am writing
> some data in file. but i want read data of whole file from start till
> end and display is on screen. but it is printing only entry added
> during program(i.e. last enrty).
> //
>
> while(read(fd,&y,sizeof(st)))
> {
> printf("%s%d",y->ch,y->n);
> }}
>
When you declare x and y like this:
st *x,*y;
you're declaring POINTERS, not structures, so you don't have any memory
allocated to hold structures, so when you invoke scanf() and try to
populate the structures that you don't have, you're just writing to
random memory locations with unpredictable results. You're compiler is
almost certainly warning you about this. Fix this first, then see if
you're still having problems.
Ed.
- Next message: hirenshah.05_at_gmail.com: "Re: problem in reading from file"
- Previous message: hirenshah.05_at_gmail.com: "problem in reading from file"
- In reply to: hirenshah.05_at_gmail.com: "problem in reading from file"
- Next in thread: hirenshah.05_at_gmail.com: "Re: problem in reading from file"
- Reply: hirenshah.05_at_gmail.com: "Re: problem in reading from file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|