[C] Access violation reading location - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

[C] Access violation reading location

toni00ctoni00c Posts: 3Member
hello

i build two separate software simples , one for write one file that i called persona.txt and another for read it



=================================
#include
#include

struct persona {
int numero ;
char *nome ;
char cognome [10] ;
} ;

typedef struct persona persona ;

int main ()


{

FILE *Jtr ;


persona persona ;

fgets (persona.cognome , 9 , stdin) ;

persona.nome = (char*) malloc (sizeof (char ) * 6 ) ;
persona.nome = "tony" ;

fprintf (stdout , "%s" , persona.nome) ;





if ( (Jtr = fopen ("persona.txt" , "wb" ) ) != NULL )
{

fwrite (&persona , sizeof (struct persona ) , 1 , Jtr);



}


fclose (Jtr ) ;
system ("PAUSE") ;

return 0 ;
}

==========================================================


after i try to read that file "persona.txt"


with it



#include
#include

struct persona {
int numero ;
char *nome ;
char cognome [10] ;
} ;

typedef struct persona persona ;

int main ()


{


FILE *Jtr ;
char *Ptr ;

persona persona ;




if ( (Jtr = fopen ("persona.txt" , "rb" ) ) != NULL )
{



persona.nome = (char*) malloc ( sizeof (char) * 10 ) ;
fread (&persona , sizeof (struct persona ) , 1 , Jtr ) ;





fprintf (stdout , "%s
" , persona .cognome ) ;
fprintf (stdout , "%s
", persona .nome ) ;


}



fclose (Jtr ) ;
system ("PAUSE") ;

return 0 ;
}



but this software tell me this Error :

"Unhandled exception at 0x64f3984f (msvcr90d.dll) in lettura.c.exe: 0xC0000005: Access violation reading location 0x00f55758"


i thinked about problem of allocation with function malloc and
the pointer of string "persona.nome" , but i can't resolve!

somebody could help me'

thanks





Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]It is a standard issue with misunderstanding of pointers. Your structure contains a pointer. Pointer is a variable, which contains an address to OTHER location in memory. In other words, when you [italic]fwrite[/italic] your structure to the file - only the value of pointer is written and its contents is not written:[/color]
    [code]
    YOUR STRUCTURE:
    +-----------+-----+-----+
    | . . . . | PTR | ... | (THIS IS WRITTEN TO FILE)
    +-----------+-----+-----+
    |
    | +------+
    +---> | TONY | (THIS IS NOT WRITTEN TO FILE)
    +------+
    [/code]
    [color=Blue]Also, this is incorrect (if [italic]nome[/italic] is allocated):[/color]
    [code]persona.nome="tony";[/code]
    [color=Blue]Proper way in this case is copying the text:[/color]
    [code]strcpy (persona.nome, "tony");[/code][color=Blue]Or, even better, use [italic]strdup[/italic] to allocate memory AND copy text at the same time.

    A proper code to write a structure will be this way:[/color]
    [code]
    [color=Green]//
    // First write whole structure:
    //[/color]
    fwrite (&persona, 1, sizeof (persona), file);
    [color=Green]//
    // Then write length of text at nome (including terminator).
    // Without this step - not possible to read data back from file
    //[/color]
    int length = 1 + strlen (persona.nome);
    fwrite (&length, 1, sizeof (length), file);
    [color=Green]//
    // Finally, write text itself (including terminator)
    //[/color]
    fwrite (persona.nome, 1, length, file);
    [/code][color=Blue]To get the data back you need the following code:[/color][code]
    [color=Green]//
    // Read whole structure back. At this point persona.nome will
    // contain a garbage pointer, saved into file by OTHER process.
    //[/color]
    fread (&persona, 1, sizeof (persona), file);
    [color=Green]//
    // Read the length of text at nome
    //[/color]
    int length;
    fread (&length, 1, sizeof (length), file);
    [color=Green]//
    // Allocate memory for persona.nome (we know the length now)
    //[/color]
    persona.nome = malloc (length);
    [color=Green]//
    // Finally, read text with terminator
    //[/color]
    fread (persona.nome, 1, length, file);
    [/code]
Sign In or Register to comment.