Discussion:
Trying to read the last 20 bytes of a text file in Visual C but it's not working?
(too old to reply)
g***@hotmail.com
2015-03-18 13:50:40 UTC
Permalink
Here is the programme:

char pbuffer[20];

p_file.Seek( 20, CFile::end );


p_file.Read( pbuffer, 20 );


SetDlgItemText(IDC_TEXT, pbuffer);

------
Above I'm trying to read the last 20 bytes of a text file but I get weird characters in return instead of the actual text characters???
Lothar Frings
2015-03-18 14:13:45 UTC
Permalink
Post by g***@hotmail.com
char pbuffer[20];
p_file.Seek( 20, CFile::end );
p_file.Read( pbuffer, 20 );
SetDlgItemText(IDC_TEXT, pbuffer);
I think you are setting the file position
to the end of the file and then you read... 0 bytes.
The weird characters is probably whatever
was in the buffer before. Probably you have
to determine the file size, then do a

Seek (filesize-20, CFile::begin)
R.Wieser
2015-03-18 16:19:25 UTC
Permalink
Lothar,
Post by Lothar Frings
Seek (filesize-20, CFile::begin)
Would using a negative offset together with "CFile::end" not work as well ?

p_file.Seek( -20, CFile::end );

Regards,
Rudy Wieser
Post by Lothar Frings
I think you are setting the file position
to the end of the file and then you read... 0 bytes.
The weird characters is probably whatever
was in the buffer before. Probably you have
to determine the file size, then do a
Seek (filesize-20, CFile::begin)
Lothar Frings
2015-03-18 16:40:30 UTC
Permalink
Post by R.Wieser
Lothar,
Post by Lothar Frings
Seek (filesize-20, CFile::begin)
Would using a negative offset together with "CFile::end" not work as well ?
p_file.Seek( -20, CFile::end );
Yes, the documentation says so:

<https://msdn.microsoft.com/en-us/library/8c5ccz0x.aspx>
Guskz Kz
2015-03-18 19:16:24 UTC
Permalink
Post by Lothar Frings
Post by R.Wieser
Lothar,
Post by Lothar Frings
Seek (filesize-20, CFile::begin)
Would using a negative offset together with "CFile::end" not work as well ?
p_file.Seek( -20, CFile::end );
<https://msdn.microsoft.com/en-us/library/8c5ccz0x.aspx>
Great it works, thanks!

I would appreciate if anyone also knows the answer to my second post?
R.Wieser
2015-03-18 21:49:59 UTC
Permalink
Guskz,
Post by Guskz Kz
Great it works, thanks!
You tried both, and got both to work ? Thats good to hear.
Post by Guskz Kz
I would appreciate if anyone also knows the answer to my second post?
You mean your first (by 5 minutes) ? :-)

Impossible to answer I'm afraid, as it lacks a bit of info. Like is the
created dialog modal, or non-modal. And what functions are you using to
create your dialog.

In the first case you would need to supply the data with the creating of the
dialog (DialogBoxParam). In the latter case you could do the same
(CreateDialogParam), but also wait until the (CreateDialog) function returns
(with the Window handle) and use that like you use it to access any control.

... but to be honest, I'm not even sure if I understood the question right.
It reads like that "new dialog box" is created from by an already existing
one, but I could be wrong, and that "new dialog box" is a stand-alone one
(the start of a new app), in which case I really do not understand what you
mean.

Regards,
Rudy Wieser
Post by Guskz Kz
Post by Lothar Frings
Post by R.Wieser
Lothar,
Post by Lothar Frings
Seek (filesize-20, CFile::begin)
Would using a negative offset together with "CFile::end" not work as well ?
p_file.Seek( -20, CFile::end );
<https://msdn.microsoft.com/en-us/library/8c5ccz0x.aspx>
Great it works, thanks!
I would appreciate if anyone also knows the answer to my second post?
Guskz Kz
2015-03-19 03:24:36 UTC
Permalink
Post by R.Wieser
Guskz,
Post by Guskz Kz
Great it works, thanks!
You tried both, and got both to work ? Thats good to hear.
Post by Guskz Kz
I would appreciate if anyone also knows the answer to my second post?
You mean your first (by 5 minutes) ? :-)
Impossible to answer I'm afraid, as it lacks a bit of info. Like is the
created dialog modal, or non-modal. And what functions are you using to
create your dialog.
It is a modal "alert type" dialog box created in the resource section of visual c++ (visual studio).
Post by R.Wieser
In the first case you would need to supply the data with the creating of the
dialog (DialogBoxParam). In the latter case you could do the same
(CreateDialogParam), but also wait until the (CreateDialog) function returns
(with the Window handle) and use that like you use it to access any control.
I did not do any of the above, it is an "alert type" dialog box created automatically in the Resource section of Microsoft's Visual Studio. So you click on the properties of that dialog box and it gives you and IDD_DIALOG inside. Then I created a static text automatically inside this dialog box from the Resource section of Visual Studio and its property automatically sets it as IDC_STATIC.


Then for example I try to pass "hello" in the program to this variable by using:


SetDlgItemText(IDC_STATIC, "hello");


And nothing happens....but if instead I create a static text in the regular window of the program (instead of the newly opened dialog box) the words "hello" do appear in the window.

So how can I paste "hello" in the newly opened dialog box in front of the program's regular window instead?
Post by R.Wieser
... but to be honest, I'm not even sure if I understood the question right.
It reads like that "new dialog box" is created from by an already existing
one, but I could be wrong, and that "new dialog box" is a stand-alone one
(the start of a new app), in which case I really do not understand what you
mean.
Regards,
Rudy Wieser
Post by Guskz Kz
Post by Lothar Frings
Post by R.Wieser
Lothar,
Post by Lothar Frings
Seek (filesize-20, CFile::begin)
Would using a negative offset together with "CFile::end" not work as
well ?
Post by Guskz Kz
Post by Lothar Frings
Post by R.Wieser
p_file.Seek( -20, CFile::end );
<https://msdn.microsoft.com/en-us/library/8c5ccz0x.aspx>
Great it works, thanks!
I would appreciate if anyone also knows the answer to my second post?
R.Wieser
2015-03-19 08:58:22 UTC
Permalink
Guskz,
Post by Guskz Kz
I did not do any of the above, it is an "alert type" dialog
box created automatically in the Resource section of
Microsoft's Visual Studio
That means I cannot help you further as I'm not a Visual Studio user myself,
and have no idea how the above is implemented. I'm an direct API kind of
guy, using as little language-specific wrappers as possible.

But at least now anyone reading your question will probably have all the
info he/she needs. :-)

Regards,
Rudy Wieser
Post by Guskz Kz
Guskz,
Post by Guskz Kz
Great it works, thanks!
You tried both, and got both to work ? Thats good to hear.
Post by Guskz Kz
I would appreciate if anyone also knows the answer to my second post?
You mean your first (by 5 minutes) ? :-)
Impossible to answer I'm afraid, as it lacks a bit of info. Like is the
created dialog modal, or non-modal. And what functions are you using to
create your dialog.
It is a modal "alert type" dialog box created in the resource section of
visual c++ (visual studio).
Post by Guskz Kz
In the first case you would need to supply the data with the creating of the
dialog (DialogBoxParam). In the latter case you could do the same
(CreateDialogParam), but also wait until the (CreateDialog) function returns
(with the Window handle) and use that like you use it to access any control.
I did not do any of the above, it is an "alert type" dialog box created
automatically in the Resource section of Microsoft's Visual Studio. So you
click on the properties of that dialog box and it gives you and IDD_DIALOG
inside. Then I created a static text automatically inside this dialog box
from the Resource section of Visual Studio and its property automatically
sets it as IDC_STATIC.


Then for example I try to pass "hello" in the program to this variable by
using:


SetDlgItemText(IDC_STATIC, "hello");


And nothing happens....but if instead I create a static text in the regular
window of the program (instead of the newly opened dialog box) the words
"hello" do appear in the window.

So how can I paste "hello" in the newly opened dialog box in front of the
program's regular window instead?
Post by Guskz Kz
... but to be honest, I'm not even sure if I understood the question right.
It reads like that "new dialog box" is created from by an already existing
one, but I could be wrong, and that "new dialog box" is a stand-alone one
(the start of a new app), in which case I really do not understand what you
mean.
Regards,
Rudy Wieser
Guskz Kz
2015-03-19 20:43:04 UTC
Permalink
Post by R.Wieser
Guskz,
Post by Guskz Kz
I did not do any of the above, it is an "alert type" dialog
box created automatically in the Resource section of
Microsoft's Visual Studio
That means I cannot help you further as I'm not a Visual Studio user myself,
and have no idea how the above is implemented. I'm an direct API kind of
guy, using as little language-specific wrappers as possible.
But at least now anyone reading your question will probably have all the
info he/she needs. :-)
Regards,
Rudy Wieser
Post by Guskz Kz
Guskz,
Post by Guskz Kz
Great it works, thanks!
You tried both, and got both to work ? Thats good to hear.
Post by Guskz Kz
I would appreciate if anyone also knows the answer to my second post?
You mean your first (by 5 minutes) ? :-)
Impossible to answer I'm afraid, as it lacks a bit of info. Like is the
created dialog modal, or non-modal. And what functions are you using to
create your dialog.
It is a modal "alert type" dialog box created in the resource section of
visual c++ (visual studio).
Post by Guskz Kz
In the first case you would need to supply the data with the creating of
the
Post by Guskz Kz
dialog (DialogBoxParam). In the latter case you could do the same
(CreateDialogParam), but also wait until the (CreateDialog) function
returns
Post by Guskz Kz
(with the Window handle) and use that like you use it to access any
control.
I did not do any of the above, it is an "alert type" dialog box created
automatically in the Resource section of Microsoft's Visual Studio. So you
click on the properties of that dialog box and it gives you and IDD_DIALOG
inside. Then I created a static text automatically inside this dialog box
from the Resource section of Visual Studio and its property automatically
sets it as IDC_STATIC.
SetDlgItemText(IDC_STATIC, "hello");
And nothing happens....but if instead I create a static text in the regular
window of the program (instead of the newly opened dialog box) the words
"hello" do appear in the window.
So how can I paste "hello" in the newly opened dialog box in front of the
program's regular window instead?
Post by Guskz Kz
... but to be honest, I'm not even sure if I understood the question
right.
Post by Guskz Kz
It reads like that "new dialog box" is created from by an already existing
one, but I could be wrong, and that "new dialog box" is a stand-alone one
(the start of a new app), in which case I really do not understand what
you
Post by Guskz Kz
mean.
Regards,
Rudy Wieser
Ok thanks Rudy

Continue reading on narkive:
Loading...