CFile read
-
Hi, I want to read a .txt file frequently.I wrote this code;
char *pbuf = new char(file.GetLength())
file.Read( pbuf,file.GetLength());In this way occured an error and program shut down while it is running. But when I wrote
char pbuf[100];
file.Read( pbuf,100);this code doesn't produce an error. But I wil not know the size of the file.What can I do for that? Thanks
-
Hi, I want to read a .txt file frequently.I wrote this code;
char *pbuf = new char(file.GetLength())
file.Read( pbuf,file.GetLength());In this way occured an error and program shut down while it is running. But when I wrote
char pbuf[100];
file.Read( pbuf,100);this code doesn't produce an error. But I wil not know the size of the file.What can I do for that? Thanks
Instead of
char *pbuf = new char(file.GetLength())
use
char *pbuf = new char**[file.GetLength()];**
Also, don't forget to
delete []pbuf
, when you've finished using it.
Too many passwords to remember? Try KeePass Password Safe!
-
Hi, I want to read a .txt file frequently.I wrote this code;
char *pbuf = new char(file.GetLength())
file.Read( pbuf,file.GetLength());In this way occured an error and program shut down while it is running. But when I wrote
char pbuf[100];
file.Read( pbuf,100);this code doesn't produce an error. But I wil not know the size of the file.What can I do for that? Thanks
1/
new char [file.GetLength()];
2/ You are making a buffer exactly as long as the file - and as it's char buffer, you're likely to be expecting it to be NULL terminated. Which it won't be... Your second example reads *upto* 100 chars, so will probably be less, and as that chunk of 100 bytes probably has a zero or two in it, you get away with it. Make a text file 100 bytes long, and it would likely fail too. 3/ I'd put the size into a variable, if only for debugging purposes. 4/ You could also have some naughty person save a '\0' somewhere in the file, so your string would be messed up... Especially if this program ends up in the wild. The effect could be benign: "Ok, it will look like there was less text. big deal", or critical, depending on your code. Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
-
Hi, I want to read a .txt file frequently.I wrote this code;
char *pbuf = new char(file.GetLength())
file.Read( pbuf,file.GetLength());In this way occured an error and program shut down while it is running. But when I wrote
char pbuf[100];
file.Read( pbuf,100);this code doesn't produce an error. But I wil not know the size of the file.What can I do for that? Thanks
as long as you're using MFC, why don't you use CString instead of char* ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
as long as you're using MFC, why don't you use CString instead of char* ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Use the rant icon. :-D
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Use the rant icon. :-D
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
nope, i wasn't ranting, i was just advising, on an interrogative way... :cool:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
nope, i wasn't ranting, i was just advising, on an interrogative way... :cool:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Actually yours was a good suggestion and so, could be a rant. :)
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Hi, I want to read a .txt file frequently.I wrote this code;
char *pbuf = new char(file.GetLength())
file.Read( pbuf,file.GetLength());In this way occured an error and program shut down while it is running. But when I wrote
char pbuf[100];
file.Read( pbuf,100);this code doesn't produce an error. But I wil not know the size of the file.What can I do for that? Thanks
iayd wrote:
char *pbuf = new char(file.GetLength()) file.Read( pbuf,file.GetLength()); In this way occured an error
What error? A missing semicolon, perhaps? Did
new
fail? DidGetLength()
fail? DidRead()
fail? Be specific."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne