|
I am doing this because I want to read file byte by byte.storing this bytes in the char buffer to write these bytes into the files.
ritz1234
|
|
|
|
|
ritz1234 wrote: I am doing this because I want to read file byte by byte.
Understandable, but extremely inefficient.
ritz1234 wrote: storing this bytes in the char buffer to write these bytes into the files.
Which does not explain why you are using heap memory. You are killing the memory manager for no good reason. Use a stack-based variable instead.
"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
|
|
|
|
|
Thanks for the reply David,
Right now I am least concern about the efficiency of the code.This is not
the final code of the application.
Right now I am only concerning about the result of the program which is
not correct.
Please help me out.A sample code would be appreciated.
ritz1234
|
|
|
|
|
ritz1234 wrote: A sample code would be appreciated.
char ch;
int x = 0;
while (! fin.eof())
{
fin.read(&ch, 1);
buf[x++] = ch;
}
...
fout.write(buf, x - 1);
"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
|
|
|
|
|
There are two problems with this code.
1. strcat: you cannot do a strcat with a single character since it expects both source and destination strings to be NULL terminated. Moreover, executable file is a binary file and this is going to create problems.
2. strlen: computes the length of a NULL terminated string so if a NULL character is encountered in between the buffer it will return incorrect length in case of binary files.
Try the following code. Here I have taken correct file size because you cannot have too large arrays on a stack.
char* buf = new char[2567672];
strcpy(buf, "");
int i =0;
ifstream fin("C:\\1.exe", ios::binary);
if(fin.is_open())
{
char ch;
while(!fin.eof())
{
fin.read(&ch, 1);
buf[i] = ch;
i++;
}
}
ofstream fout("C:\\2.exe", ios::binary);
fout.write(buf, i-1);
fout.close();
If you ask me then I would use following code:
size_t FileSize(std::ifstream* fp)
{
std::ifstream::pos_type begin = fp->tellg();
fp->seekg (0, std::ios::end);
std::ifstream::pos_type end = fp->tellg();
fp->seekg (0, std::ios::beg);
return end - begin;
}
void CopyBinaryFile()
{
ifstream fin("C:\\1.exe", ios::binary);
size_t _len = FileSize(&fin);
char* _buffer = new char[_len];
if(fin)
{
fin.read(_buffer, _len);
}
ofstream fout("C:\\2.exe", ios::binary);
if(fout)
{
fout.write(_buffer, _len);
fout.close();
}
}
Hope this solves your problem.
-Saurabh
|
|
|
|
|
Thanks for the help Subhas and David,
The problem is now resolved.
ritz1234
|
|
|
|
|
|
Hi All,
I am facing an issue relating to named pipes.
The OS used is Vista.
I am creating a named pipe within a service that is running in session zero. A client application tries to read through the pipe by calling api CreateFile. This fails since the client application is running in the user context.
Is there any way that this caould be overcome? Should i pass any securtiy attributes to the "CreateNamedPipe" or "CreateFile" apis?
Thanks in advance
Sunil
|
|
|
|
|
What error code are you getting?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
It gives an error ACCESS_DENIED.
Sunil
|
|
|
|
|
|
Hi all
I am trying to develope a demo project with which we can disable PPP modem connections .
Can anyone please help ?
regards
redindian
|
|
|
|
|
I want to send a key to a window. I used Global Hook and my code is like this :
//In hook proc :
if(nCode <0)
CallNextHookEx(MyHook,nCode,wParam,lParam);
hWndApp = CWnd::FindWindow(0,_T("Test"));
if (hWndApp == NULL)
MessageBox(NULL,L" Can't find the window", L"",0);
hWndApp->SendMessage(WM_CHAR, 8, 0); //Backspace
It can find the window but it can't send the message. Some one plz help me
|
|
|
|
|
|
I think you should should send:
WM_KEYDOWN
WM_CHAR
WM_KEYUP
-@SuDhIrKuMaR@-
|
|
|
|
|
What do you mean ? Do I have to send three messages at one time ?
|
|
|
|
|
see SendInput api of any use??
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You/xml>
|
|
|
|
|
If the target window is active, you can use SendInput() .
"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
|
|
|
|
|
Could you show me an example of using SendInput to send a key to an active window plz ? I find it difficult to find a specific example
|
|
|
|
|
I found this using Google.
"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
|
|
|
|
|
Thank you very much
|
|
|
|
|
DavidCrow wrote: I found this using Google.
he he he Answer of yahoo getting very popular
|
|
|
|
|
Plz help me one more time. I would like to know how to send WM_KEYUP by SendInput. When I send a key, it seems to be pressed forever ...
|
|
|
|
|
See here.
"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
|
|
|
|
|
I've read many pages from the pages you gave me but I still can't find the answer They recommend to use SendMessage but it doesn't work with SendMessage...
|
|
|
|