PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > writing structure to a file
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
writing structure to a file

Réponse
 
LinkBack Outils de la discussion
Vieux 12/04/2008, 10h20   #1
prashant.khade1623@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut writing structure to a file

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
  Réponse avec citation
Vieux 12/04/2008, 10h34   #2
prashant.khade1623@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: writing structure to a file

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 ?
  Réponse avec citation
Vieux 12/04/2008, 10h37   #3
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: writing structure to a file

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
  Réponse avec citation
Vieux 12/04/2008, 10h37   #4
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: writing structure to a file


<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

  Réponse avec citation
Vieux 12/04/2008, 10h54   #5
Pradeep
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: writing structure to a file

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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11476 seconds with 13 queries