I/O freaks me out !
I have no idea what I''m doing wrong... Whenever I try writing a binary file the data doesn''t come out right...
Here we go:
struct FILE
{
char *data; // Data
} file;
file.data = "this is data"; // Some file
ofstream fout("file.thg",ios::binary);
fout.write((char *)&file,sizeof(file));
fout.close();
Now load it:
ifstream fin("file.thg",ios::binary);
fin.read((char *)&file,sizeof(file));
fin.close();
But the file.data never comes out "this is data".
It sometimes even comes strings I used before...
Can anyone help ?
Thanks
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
Did you look at the size of your file?
It seems to me that it won''t be longer than 4 Bytes,
because by using
fout.write((char *)&file,sizeof(file));
you''ll only write 4 bytes since file consists only
of a char pointer.
I would suggest the following:
fout.write(file.data, strlen(file.data));
This will write whats at the memory location file.data
is pointing to and not only the pointer to your disk.
It seems to me that it won''t be longer than 4 Bytes,
because by using
fout.write((char *)&file,sizeof(file));
you''ll only write 4 bytes since file consists only
of a char pointer.
I would suggest the following:
fout.write(file.data, strlen(file.data));
This will write whats at the memory location file.data
is pointing to and not only the pointer to your disk.
July 28, 2000 03:34 PM
You cannot init a char pointer with a string
/* wrong */
file.data = "this is data";
/* correct */
file.data = (char*)malloc (strlen("this is data") + 1);
strcpy (file.data,"this is data");
OK?
METALLICA RULEZ
/* wrong */
file.data = "this is data";
/* correct */
file.data = (char*)malloc (strlen("this is data") + 1);
strcpy (file.data,"this is data");
OK?
METALLICA RULEZ
>/* wrong */
>file.data = "this is data";
No, this is perfectly legal.
Plug this in to your compiler and try it:
Edited by - FordPrefect on July 28, 2000 8:01:04 PM
>file.data = "this is data";
No, this is perfectly legal.
Plug this in to your compiler and try it:
#include <stdio.h>#include <string.h>int main(void){ char *p; p = "text"; if (strcmp(p, "text") != 0) printf("Help! My compiler is broken!\n"); else printf("Everything's okay.\n"); return 0;}
Edited by - FordPrefect on July 28, 2000 8:01:04 PM
Yeah, it''s legal.
-----------------------------1. "Diplomacy is the art of saying 'Nice doggie!'... till you can find a rock." 2. "Always remember you're unique, just like everyone else." 3. "If we don't succeed, we run the risk of failure."-Dan Quayle4. If life gives you sour grapes, squash them and make wine!
Let me explain the problem...
When I save the file, it seems to work.
But when I load the file, wacky things happen.
Let''s say I just loaded a bitmap:
Load_Bitmap("test.bmp");
Now when I try loading the data (Has nothing to do with Load_Bitmap) from the file I get "test.bmp" as the data !
If I change "test.bmp" to "roro.bmp" it says the data from the file is "roro.bmp" !!
I have no idea what''s going on... Maybe I should use something else and not fstream.h
The road to success is always under construction
When I save the file, it seems to work.
But when I load the file, wacky things happen.
Let''s say I just loaded a bitmap:
Load_Bitmap("test.bmp");
Now when I try loading the data (Has nothing to do with Load_Bitmap) from the file I get "test.bmp" as the data !
If I change "test.bmp" to "roro.bmp" it says the data from the file is "roro.bmp" !!
I have no idea what''s going on... Maybe I should use something else and not fstream.h
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
So this is what''s happening:
when you do:
struct FILE
{
char *data; // Data
} file;
you''re defining a structure that has a member wich is a pointer to memory, "formatted" as chars
when you do:
file.data = "this is data"; // Some file
what you are doing is making the value of file.data equal to the address in memory where the string "this is data" is in memory.
So when you save the struct to disk
ofstream fout("file.thg",ios::binary);
fout.write((char *)&file,sizeof(file));
fout.close();
what happens is that what is saved is the value of the address (at least 4 bytes, as Nazgul pointed out), not the sequence of chars of the string herself.
The next time you run a program, the position indicated by that value, might not even be valid, and it will allmost certainly point to rubish. In your case, if you''re using that code inside the same program, if might actually work, since it''s possible that the string is still at the place indicated on file.data.
what you should do, is something Nazghul suggested, or something like:
You know, I never wanted to be a programmer...
Alexandre Moura
when you do:
struct FILE
{
char *data; // Data
} file;
you''re defining a structure that has a member wich is a pointer to memory, "formatted" as chars
when you do:
file.data = "this is data"; // Some file
what you are doing is making the value of file.data equal to the address in memory where the string "this is data" is in memory.
So when you save the struct to disk
ofstream fout("file.thg",ios::binary);
fout.write((char *)&file,sizeof(file));
fout.close();
what happens is that what is saved is the value of the address (at least 4 bytes, as Nazgul pointed out), not the sequence of chars of the string herself.
The next time you run a program, the position indicated by that value, might not even be valid, and it will allmost certainly point to rubish. In your case, if you''re using that code inside the same program, if might actually work, since it''s possible that the string is still at the place indicated on file.data.
what you should do, is something Nazghul suggested, or something like:
struct FILE
{
char data[256]; //Data, with enought space to store a string
}
//this will actually make a store space inside your struct to store your string.
then:
strcpy(file.data,"this is data"); // Some file, copy values of
//each char to the struct
ofstream fout("file.thg",ios::binary);
fout.write((char *)&file,sizeof(file));
fout.close();
Now load it:
ifstream fin("file.thg",ios::binary);
fin.read((char *)&file,sizeof(file));
fin.close();
You know, I never wanted to be a programmer...
Alexandre Moura
It finally works !
Thanks for helping out guys
The road to success is always under construction
Thanks for helping out guys
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
Ford,
char* p;
p = "Text";
may be legal , but it is dangerous. You are writting into memory address space that does not exist. Where exactly is the memory for the pointer being initialized. It''s not on the stack.
char* p = "text"; /*this is fine*/
char p[] = "test"; /*this is fine*/
char p[4] = "test"; /* this will work, but is dangerous, especially if you will be using a lot of strcpy. Just be aware of how this is implemented. There is no room for a null character. So it''s not a proper string literal. */
char* p;
p = "Text";
may be legal , but it is dangerous. You are writting into memory address space that does not exist. Where exactly is the memory for the pointer being initialized. It''s not on the stack.
char* p = "text"; /*this is fine*/
char p[] = "test"; /*this is fine*/
char p[4] = "test"; /* this will work, but is dangerous, especially if you will be using a lot of strcpy. Just be aware of how this is implemented. There is no room for a null character. So it''s not a proper string literal. */
No room for a null character? Thats a five character array. "test\n'' == 5 chars. You need to remember to start from 0.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement