|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi
I am trying ti write a structure to a file, but I am getting segmentation fault. Below is my program #include<stdio.h> main() { FILE *fp; struct student { int age; char name[19]; char school[15]; }; struct student s1={25,"khade","KVOFPM"}; struct student s2={25,"khade","KVOFPM"}; fp = fopen("khade","r+"); fwrite(&s1,sizeof(struct student ),1,fp); fwrite(&s2,sizeof(struct student ),1,fp); } Can you tell me why I am getting the error. Also I want to read from the file. how can I do that ? Thanks in advance |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Apr 12, 2:37pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> prashant.khade1...@gmail.com said: > > > Hi > > > I am trying ti write a structure to a file, but I am getting > > segmentation fault. Below is my program > > <snip> > > > fp = fopen("khade","r+"); > > if(fp == NULL) > { > printf("Aha! THAT's why.\n"); > > -- > Richard Heathfield <http://www.cpax.org.uk> > Email: -http://www. +rjh@ > Google users: <http://www.cpax.org.uk/prg/writings/googly.php> > "Usenet is a strange place" - dmr 29 July 1999 I am not able to open the file. What can be the reason for that ? |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
prashant.khade1623@gmail.com said:
> Hi > > I am trying ti write a structure to a file, but I am getting > segmentation fault. Below is my program > <snip> > fp = fopen("khade","r+"); if(fp == NULL) { printf("Aha! THAT's why.\n"); -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
<prashant.khade1623@gmail.com> wrote in message news:39155f01-d7bc-4067-90bb-f46d600c028f@n1g2000prb.googlegroups.com... > Hi > > I am trying ti write a structure to a file, but I am getting > segmentation fault. Below is my program > > #include<stdio.h> > main() > { > FILE *fp; > struct student { > int age; > char name[19]; > char school[15]; > }; > > struct student s1={25,"khade","KVOFPM"}; > struct student s2={25,"khade","KVOFPM"}; > > fp = fopen("khade","r+"); > > fwrite(&s1,sizeof(struct student ),1,fp); > fwrite(&s2,sizeof(struct student ),1,fp); > > > } > > > Can you tell me why I am getting the error. > Also I want to read from the file. how can I do that ? > Check fp(). It is probably null. Why are you using "r+" instead of "w" ? Otherwise your code is correct, but fclose() afterwards and, for Brownie points, check for write errors. fopen with "r" and call fread() to get an identical structure back in. Also strictly you should be using binary mode, because you are dumping the structures in binary images, not as text. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Hi
In your program file is opening in 'r+' mode 'r+' opens the file for both reading and writing. but it won't check whether file is there or not. (i.e., it won't create new file) in that case you need to create the file in 'w' mode that will solve your problem. or if you don't want to change your code, then you need to create a blank file in the current directory with the same name you are opening the file in 'r+' mode(crete a file blank file "khade" in the current directory) then your code should work. On Apr 12, 2:20 pm, "prashant.khade1...@gmail.com" <prashant.khade1...@gmail.com> wrote: > Hi > > I am trying ti write a structure to a file, but I am getting > segmentation fault. Below is my program > > #include<stdio.h> > main() > { > FILE *fp; > struct student { > int age; > char name[19]; > char school[15]; > > }; > > struct student s1={25,"khade","KVOFPM"}; > struct student s2={25,"khade","KVOFPM"}; > > fp = fopen("khade","r+"); > > fwrite(&s1,sizeof(struct student ),1,fp); > fwrite(&s2,sizeof(struct student ),1,fp); > > } > > Can you tell me why I am getting the error. > Also I want to read from the file. how can I do that ? > > Thanks in advance |
|
![]() |
| Outils de la discussion | |
|
|