|
A simple trick - when you want to see if a function exists to do something go Help->Index and try typing in possible function names. As you type more letters it narrows the results.
"Filecopy" didn't return anything but "Copyfile" returned
CopyFile(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, BOOL bFailIfExists)
Elaine
The tigress is here
|
|
|
|
|
I already change the code generation into multithreaded. The program can be compiled successfully but when I run it, it generate an error and can't run. Give me some hints please. Is there any logic error on my program?
The reason that I didn't use CopyFile is if the program works, I'm planning to make a program that can make XML file from text file which I think it can't be done using CopyFile function.
thx.
|
|
|
|
|
I am soooo sorry that I mis-wrote a line in my earlier post:
a = m_DataFile.GetLength() + 1;//error. Do not use.
Should have been:
a = new char [m_DataFile.GetLength() + 1]; //This one will do the thing.
this is this.
|
|
|
|
|
Are you sure the files pathes are correct? It seems that "E:/data.txt" is very likely not a valid file name. You should use "E:\\data.txt" instead.
|
|
|
|
|
I already tried all the instructions, but still the program can't run. Although it doesn't generate error on compile process, but when I try to run it, it won't run. Here is my modified source code:
int main()
{
CFile m_LogFile,m_DataFile;
char* a=NULL;
m_DataFile.Open("E:\\data.txt", CFile::modeRead|CFile::shareDenyNone);
a = new char [m_DataFile.GetLength() + 1];
memset(a,0,m_DataFile.GetLength() + 1);
try
{
m_LogFile.Open("E:\\data1.txt", CFile::modeWrite|CFile::modeRead|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyNone);
m_LogFile.SeekToEnd();
}
catch(CException* e)
{
e->ReportError();
e->Delete();
}
m_DataFile.Read(a,m_DataFile.GetLength());
m_LogFile.Write(a,strlen(a));
m_DataFile.Close();
m_LogFile.Close();
return 0;
}
|
|
|
|
|
Execute step by step your program in debug mode, and check for the result value returned by the Open, Read, Write, etc. functions. You should then be able to locate where exactly is the problem.
|
|
|
|
|
firebolt77 wrote:
...but when I try to run it...
Which means what? Single-stepping through each statement, which one is not working?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
firebolt77 wrote:
m_DataFile.Read(a,m_DataFile.GetLength);
At this point, a is a NULL pointer. A crash is imminent.
firebolt77 wrote:
m_LogFile.Write(a,strlen(a));
This erroneously assumes that a will have a \0 character and that it will be at the end.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
This code is not tested, I don't have the time, but here's a quick pass with comments.
<br />
#include<afx.h><br />
#include<stdio.h><br />
<br />
int main()<br />
{<br />
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))<br />
{<br />
_tprintf(_T("Fatal Error: MFC initialization failed\n"));<br />
return 1;<br />
}<br />
<br />
CFile logFile, dataFile;<br />
<br />
<br />
dataFile.Open("E:/data.txt", CFile::modeRead|CFile::shareDenyWrite);<br />
<br />
<br />
CFileException err;<br />
if (!logFile.Open("E:/data1.txt", CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyWrite, &err))<br />
{<br />
<br />
err.ReportError();<br />
return 1;<br />
}<br />
logFile.SeekToEnd();<br />
<br />
int rval = 0;<br />
<br />
int bufferLen = dataFile.GetLength();<br />
char* pBuffer = new char[bufferLen];<br />
if (dataFile.Read(pBuffer, bufferLen) == dataFile.GetLength())<br />
{<br />
if (logFile.Write(pBuffer, bufferLen) != bufferLen)<br />
{<br />
_tprintf("Error writing file\n");<br />
rval = 1;<br />
}<br />
}<br />
delete [] pBuffer;<br />
return rval;<br />
}<br />
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
I have tried the code(Mr. Joe Woodbury's code), but when I run it, it generate the same error with my code, although there is no error in the compile process.When I run the program, it generate a typical windows error dialog box(the one that has debug,send error report, and don't send button). I also try to debug it, but when I debug it, it opens other source code(winmain.cpp). I'm so confused. Pls help..
thx.
|
|
|
|
|
Interesting, since it doesn't compile, you couldn't have tried my code. For one, you didn't include the right headers so the call to AfxWinInit would fail when compiling. Second you would need to declare an instance of CWinApp. Third CFile::Write() is a void function.
The problem actually likely lies with the failure to check for failure with the dataFile.Open call if e:/data.txt doesn't exist or can't be otherwise opened.
I simplified it and compiled the file below.
An additional possibility is that your project is not set up correctly. So, before continuing, close the project and create a new one with Visual Studio. Assuming you are using VS 2003, On the File menu, click New and then Project. In the left pane, open Visual C++ Projects and the Win32 folder. In the right pane Select Win32 Console Project and enter a project name in the Name edit control, and then click OK.
In the wizard, select Application Settings and then click the MFC checkbox in the right, and then "Finish."
When the project opens, select everything in Main between the two outer braces, then paste the code below into it. Compile and run.
Here is the code as I compiled in a console project:
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
return 1;
}
CFile logFile, dataFile;
CFileException err;
if (!dataFile.Open("e:/data.txt", CFile::modeRead | CFile::shareDenyWrite, &err))
{
err.ReportError();
return 1;
}
if (!logFile.Open("e:/data1.txt", CFile::modeReadWrite | CFile::modeCreate | CFile::modeNoTruncate | CFile::shareDenyWrite, &err))
{
err.ReportError();
return 1;
}
logFile.SeekToEnd();
int rval = 0;
int bufferLen = (int) dataFile.GetLength();
char* pBuffer = new char[bufferLen];
if (dataFile.Read(pBuffer, bufferLen) == dataFile.GetLength())
{
logFile.Write(pBuffer, bufferLen);
}
delete [] pBuffer;
return rval;
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
|
Hi,
Am facing problems in following...........
1. How to know whether Database exist in SQL Server
2. How to Create just Database name in SQL Server
I need code solutoin in VC++ 6.0
3. How to give option of, Download a HTML FIle Directly, without displaying SaveAs Dialog Box.....in C#.NET
Babar
|
|
|
|
|
Ask the third question in the C# forum.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Q1: This example (not tested) is how you could write a function to determine whether a database exists (using our internal ODBC wrapper classes).
BOOL DoesDatabaseExist(char *cpDBName, char *cpDSN, char *cpUserID, char *cpPassword)
{
BOOL bDBExists = FALSE;
DBConnection oDBConn;
if (oDBConn.Connect(cpDSN,cpUserID,cpPassword))
{
char caBuf[256];
sprintf(caBuf,"select COUNT(*) from master..sysdatabases where name = '%s'",cpDBName);
DBStatement *pStmt = oDBConn.ExecSQL(caBuf);
if (pStmt)
{
if (pStmt->FetchNext())
bDBExists = pStmt->GetShort(1);
pStmt->EndSQL();
delete pStmt;
}
else
DoDBError("Select Error",&oDBConn);
oDBConn.Disconnect();
}
else
DoDBError("Connection Failed!",&oDBConn);
return bDBExists;
}
Q2: Using the same form as above, you would execute the CREATE DATABASE command instead of a select. Note that the CREATE DATABASE command does not return any result sets. For more info, see CREATE DATABASE in SQL BOL.
Q3: Post to C# forum... I do C but I am not sharp
onwards and upwards...
|
|
|
|
|
Hi I am having the following probem when creating an MFC application.
I have a static global variable (declared in globals.h which is included in all places where foo is used) of class foo:
static foo foo_1;
I am accessing it in a windows dialog box called WorldView (class name).
From this dialog box I can open another modal dialog box called Options using CDialog::DoModal() method.
Here is my problem, when I try to access "foo" from Options its a completly new version of "foo" - the addresses of this pointer in Options and WorldView do not match. Changes to one "foo" do not reflec in the other "foo", so basically I now have 2 static variable of class "foo", which is really odd.
Can some one explain to me why this is hapening and how to fix it.
Thank you.
|
|
|
|
|
Best approach - don't have static global variables. If you must, then set them up in your stdafx.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
You declared it in a header didn't you? I've seen this problem here a few times and the solution is the following: don't declare variables in header files!
Tou immediatly think: how do I use global variables? Without the static keyword, the compiler complains of having multiple defined symbols!
The solution to this problem is not to add the static keyword, but to plave the variable in a translation unit (*.cpp file). In the header you declare the variable as extern :
Foo g_SomeGlobalFoo;
extern Foo g_SomeGlabalFoo;
This should solve your problem.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Thank you very much, it worked.
|
|
|
|
|
You're welcome
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Hi,
I want to create xp-style menu in win32.How it is possible & i am not able to put the strip(vertical).
|
|
|
|
|
Take a look at this[^] CodeProject sponsor.
|
|
|
|
|
I can't seem to get anything but ERROR_INVALID_ADDRESS when I want to allocate memory at a specific address.
I use this Win32 API function:
LPVOID VirtualAlloc(<br />
LPVOID lpAddress,<br />
SIZE_T dwSize,<br />
DWORD flAllocationType,<br />
DWORD flProtect<br />
);
I have used VirtualQueryEx to verify that the memory region is in the MEM_FREE state prior to attempting to VirtualAlloc memory from a MEM_FREE region.
I also set the argument flAllocationType to MEM_RESERVE and flProtect to PAGE_NOACCESS.
The minute I put NULL in for lpAddress it works fine.
I have tried rounding the address up to the next SYSTEM_INFO.dwPageSize and also tried rounding it up to the next SYSTEM_INFO.dwAllocationGranularity and appropriately adjusting (decreasing) the MEMORY_BASIC_INFORMATION.RegionSize which is used for the dwSize parameter.
Does anyone know what is the problem
|
|
|
|
|
1. Make sure that the address you want to allocate doesn't already exist or spand onto another address that does!
2. Make sure the address is 4k aligned. This means that you should allocate a number that is:
0xnnnnn000
So, 0x00003000 for example. If you display the address in HEX, the last 3 hex digits need to be 0 to be on a 4k boundary.
8bc7c0ec02c0e404c0cc0680f7018827ebee
|
|
|
|
|
MEM_RESERVE -
1. lpAddress must be aligned with a 64K boundary (SYSTEM_INFO.dwAllocationGranularity)
2. dwBlockSize must be multiple of 64K (SYSTEM_INFO.dwAllocationGranularity)
3. Entire memory region must be free (I was already checking that)
MEM_COMMIT - within an already MEM_RESERVE region -
1. lpAddress must be aligned on the SYSTEM_IONFO.dwPageSize boundary
2. VirtualAlloc will internally adjust for dwBlockSize to be a multiple of SYSTEM_IONFO.dwPageSize, but VirtuAlloc will fail if lpAddress + dwBlockSize goes beyond end of MEM_RESERVE.
Carve another notch in the Totem dedicated to obtuse MSDN documentation
|
|
|
|