Problem Writing and Reading an LPSTR

Jan 10, 2012 at 6:02am
Hello, first time writing to you here.

I have a program which writes variables to a file and then reads them back later, and it works reasonably fine, until I try to write an LPSTR.

I write it using:

outfile.write ((char*) (&text), sizeof(text));

...and read it with:

1
2
LPSTR intext;
fin.read((char *)(&intext), sizeof(intext));


Unfortunately, when I do this, the program either crashes or gives me scrambled characters instead of what I wrote.

Any help is appreciated, thankyou.
Jan 10, 2012 at 6:14am
WinNT.h:
typedef __nullterminated CHAR *NPSTR, *LPSTR, *PSTR;

LPSTR is already a pointer. therefore I think you would send the address into your fin by just using intext instead of using &intext.

Try using:
fin.read((char *)(intext), sizeof(intext));
Jan 10, 2012 at 6:52am
An LPSTR is a Long Pointer to a STRing, it's basically a char*, a C string. The 16 bit x86 architecture has a notion of a near and far pointer which you can ignore these days.

So a string is an array of chars and the char* starts to the beginning of the range. Where does it end? Well it ends on the first zero, the null terminator. So the length isn't sizeof(char*) which is going to be 4 or 8, the length is strlen(text).
Jan 10, 2012 at 6:59am
LPSTR is either a char* or a wchar_t* dependindg if UNICODE is defined or not. Do not use it, instead use char* explicitely.
Last edited on Jan 10, 2012 at 7:02am
Jan 10, 2012 at 8:00am
modoran Aren't you confusing that with LPTSTR?

I disagree with that advice wholesale; it depends on the context.
Jan 10, 2012 at 10:25am
kbw You are right, I confused with LPTSTR, my apologies for that

However, I am not agree with you about this:

So a string is an array of chars and the char* starts to the beginning of the range. Where does it end? Well it ends on the first zero, the null terminator. So the length isn't sizeof(char*) which is going to be 4 or 8, the length is strlen(text). 



Your advice about using strlen() is wrong, the buffer is NOT null-termionated. More likely you could use sizeof() if the array is created on the stack:
1
2
3
 char buffer[4096] = {0};
fin.read (buffer, sizeof(buffer)); // this works more or less, sizeof(char) is always 1 on windows at least
fin.read (buffer, strlen(buffer)); // strlen will return 0 in this case, WRONG 


However, this snippet willnot work with sizeof() if the buffer is allocated with new or malloc()
Jan 10, 2012 at 10:27am
Stewbond, I tried using your method, and it sais:

The variable "intext" is being used without being initialised.

I tried making it write the variable this way:

 
outfile.write ((char*) (&text), sizeof(char)*32);


and read it:

fin.read((char *)(&intext), sizeof(char)*32);

but that didn't work either (just gave me blank strings).

Thanks anyway though. Any other ideas?

EDIT: I also tried

1
2
char name[32] = {0};
fin.read(name, sizeof(name));


And it didn't crash (which is an improvement), but it gave me scrambles characters again.
Last edited on Jan 10, 2012 at 10:42am
Jan 10, 2012 at 2:35pm
1
2
3
4
5
6
7
LPSTR x = "foobarfoobarfoobar";
ofstream out("foo.bar");
out.write(x,strlen(x));
out.close();
char z[33] = {0};
ifstream in("foo.bar");
in.read(z,32);
Jan 10, 2012 at 11:55pm
Wow. Thankyou very much!

This is my first time doing Windows programming, so I'm not entirely familiar with these string types.

Thanks again.
Topic archived. No new replies allowed.