bad crash
When ever I call fread and then call fclode afterwards my program crashes. If I I don''t try and read it closes fine. Thanks
InFerN0Not all who wander are lost...
Post some code. Maybe you file weren''t opened (correctly) and fread() and fclose() crashed because of that? Maybe you overwrite some array when calling fread()?.
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
here is my code.
#include
#include
class File
{
public :
FILE *file;
Open(char *filename);
Write(int numchars, char *string);
Read(int numchars, char *buffer);
Close();
};
File::Open(char *filename)
{
file = fopen(filename, "r, w");
if(!file);
//error
}
File::Close()
{
fclose(file);
}
File::Read(int numchars, char *buffer)
{
fread(buffer,sizeof(char), numchars, file);
}
File::Write(int numchars, char *string)
{
fwrite(string,1, numchars, file);
}
main()
{
File f;
char mem[50];
f.Open("test.txt");
if(f.file)
printf("\nfile opened");
else
printf("\nbad");
//f.Read(10,&mem[50]);
getch();
f.Close();
if(f.file)
printf("\nfile still opened");
else
printf("\ngood");
getch();
}
InFerN0Not all who wander are lost...
I think you need to use "r+" not "r, w" to open a file for reading and writing.
I dont think you''re doing a proper check to see if the file was loaded
you should do
if(f.file!=NULL)
.....
ooooh, you''re also passing &m[50] for reasons which are unkmown to me
that''ll pass the end of your m[50] char buffer, causing fread to write 10 bytes passed it
just pass m to the function and it should work flawlessly
ByteMe95::~ByteMe95()
you should do
if(f.file!=NULL)
.....
ooooh, you''re also passing &m[50] for reasons which are unkmown to me
that''ll pass the end of your m[50] char buffer, causing fread to write 10 bytes passed it
just pass m to the function and it should work flawlessly
ByteMe95::~ByteMe95()
ByteMe95::~ByteMe95()My S(h)ite
Both bloatware and byteme pointed out the things that were probably causing trouble. Btw, change this:
fwrite(string,1, numchars, file);
to this:
fwrite(string,sizeof(char), numchars, file);
Incase the size of char would ever change.
fwrite(string,1, numchars, file);
to this:
fwrite(string,sizeof(char), numchars, file);
Incase the size of char would ever change.
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement