C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
[C] Access violation reading location Posted by toni00c on 12 Jan 2011 at 2:05 PM
hello

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



=================================
#include <stdio.h>
#include <stdlib.h>

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 <stdio.h>
#include <stdlib.h>

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\n" , persona .cognome ) ;
fprintf (stdout , "%s\n", 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





Report
Re: [C] Access violation reading location Posted by AsmGuru62 on 13 Jan 2011 at 5:56 AM
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 fwrite your structure to the file - only the value of pointer is written and its contents is not written:
YOUR STRUCTURE:
+-----------+-----+-----+
| . . . .   | PTR | ... | (THIS IS WRITTEN TO FILE)
+-----------+-----+-----+
               |
               |     +------+
               +---> | TONY | (THIS IS NOT WRITTEN TO FILE)
                     +------+

Also, this is incorrect (if nome is allocated):
persona.nome="tony";

Proper way in this case is copying the text:
strcpy (persona.nome, "tony");
Or, even better, use strdup to allocate memory AND copy text at the same time.

A proper code to write a structure will be this way:

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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.