|
Hello everyone!
Look at this code:
for (x = strlen(fileName) - 1; x > 0; x--)<br />
{<br />
<br />
<br />
if (fileName[x] == '.')<br />
{<br />
<br />
newName = malloc(x-1);<br />
strcat(newName, fileName);<br />
return newName;<br />
}<br />
else<br />
printf("%c", fileName[x]);<br />
}
That code, seems like it'll work if I modify other parts a little bit, but that else statement is not needed anymore... But if I remove it, compile, and run the program, I get a Windows error:
Title: mythax.exe - Application Error<br />
<br />
The instruction at "0x7c93426d" referenced memory at "0x00000000". The memory could not be "read".<br />
<br />
Click on OK to terminate the program<br />
Click on CANCEL to debug the program
Is this a bug in the compiler or something? I'm using MinGW 3.4.2... Thanks!
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
|
|
|
|
|
Kixdemp wrote: strcat(newName, fileName);
What are you trying to do there ? Your newName string is not initialized (so it contains garbage) and you try to append data to it (which is also larger than the newName array). So you write outside the bounds of your array.
To use strcat, your destination string needs to be zero terminated and is not secure:
Security Note Because strcat does not check for sufficient space in strDestination before appending strSource, it is a potential cause of buffer overruns. Consider using strncat instead.
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
Your program seems to have problems not connected with else . First of all, you try to execute strcat , witch concatenates two strings, but you first string -- allocated with malloc -- actually does not contains a valid value. Probably, more appropriate here is strcpy .
It seems that your problem is to determine the file’s name without extension. In this case the size of the malloc-allocated buffer should be x , not x-1 , because a space for last null character is required.
Also consider other solutions for your problem. For instance, for finding the dot, you can avoid your loop and use strrchr function. For breaking a path into components, you can use splitpath function.
|
|
|
|
|
Kixdemp wrote: strcat(newName, fileName);
the problem is here
newName not having enough memory to contain the data in "filename"
newName can hold till "screenshot"
but the file name is trying copy whole string ("screeshot.jpg") to the newName. So a buffer overrun occured.
hope u clear.
try using strncpy.
Always keep in mind that "for a string buffer allocate buffersize +1" to hold the end null character. make sure that it is initialized to NULL to avoid unexpected behavior
SaRath.
"Don't Do Different things... Do Things Differently..."
Understanding State Pattern in C++
|
|
|
|
|
While Cedric has already provided you with an answer, I'm curious what this code snippet actually does. Is it separating a file's basename from its extension?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hey, thanks you all!
Now, I'm trying to use _splitpath() , since that's... umm... simpler. Look:
_splitpath("C:\\Documents and Settings\\Kixdemp\\Desktop\\Mythax\\screenshot.png", NULL, NULL, newName, NULL);
It crashes after that line. Does newName have to be initialized? Thanks!
PD: Does it have to be a full path? This is a command line app, and the user might just select a file in the program's dir...
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
|
|
|
|
|
Kixdemp wrote: Does newName have to be initialized?
No.
Kixdemp wrote: Does it have to be a full path?
Yes.
Kixdemp wrote: the user might just select a file in the program's dir...
Which is probably fine, if all you are interested in is the filename itself sans the path and extension.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
|
That is why I initially did not mention using _splitpath() because you were using MinGW.
If your goal is just to separate a filename from its path and extension, using strrchr() will come in real handy.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hi
I am doing network programming in MFC. Problem is that i am trying to send a buffer to server using send(...) function. But there is an error. When i use WSAGetLastError() to check error code, it gives error code 0. Please can anyone tell me where can problem lie.
Thank you
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
Aqueel wrote: Problem is that i am trying to send a buffer to server using send(...) function. But there is an error.
And that error would be what?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Yah i just wanted to know that in which scenerio, 0 is returned. what can be problem? I went on msdn. There is a list of error codes. But they are lables not integer values. How can i determine this one.
Thank you
We Believe in Excellence
www.aqueelmirza.cjb.net
-- modified at 10:33 Wednesday 7th June, 2006
|
|
|
|
|
Aqueel wrote: There is a list of error codes. But they are lables not integer values. How can i determine this one.
Have you looked in winerror.h ?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
After you obtained the error code with "DWORD error = WSAGetLastError(); " and stopped the program at a breakpoint, you can see a textual description of the error by adding the following value to Watch panel or QuickWatch window: "error,hr".
|
|
|
|
|
Sorry i debugged it and saw that error code is 183. What is that?
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
In Winerror.h file, 183 is the code of ERROR_ALREADY_EXISTS error: "Cannot create a file when that file already exists".
Perhaps you are trying to open a socket on already used port? Or may be you are performing some invalid file operations.
|
|
|
|
|
if it is a network related error u will definitely get the error code.
check your code first. i think there should have some problems with your code. r u using multiple threads?
CSocket is designed to serve for per thread basis.
if u r creating a sample application. follow the steps provided in MSDN. for the basic implementation, u can try with the sample code provided in MSDN
SaRath.
"Don't Do Different things... Do Things Differently..."
Understanding State Pattern in C++
|
|
|
|
|
may the panel be a dialog?
could you interpret your method or offer the source code?
thank you very much.
kudoo
|
|
|
|
|
old_dustman wrote: could you interpret your method or offer the source code?
Not without a better understanding of what you are after. What is meant by "load and unload controls?"
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
What do you mean by "the panel"? A window with no frame? An instrumental guage/meter in the aircraft cockpit? A dashboard in the car?
- It's easier to make than to correct a mistake.
|
|
|
|
|
|
Hi All,
I have a requirement, where I need to get the following information.
1. MAC Address.
2. HDD Serial Number
3. CD/DVD Drive Serial Number.
4. CPU Processor ID.
I have my application in VC++ using MFC.
Any help will be highly appreciated.
Thanks,
Sachin.
|
|
|
|
|
GetVolumeInformation() will do more than half the work.
For MAC Address - (this is only my opinion)
one thing that would work all the time is run a shell
"arp -a" and internally search the output for the needed MAC.
The other thing you can do is WMI(Read it somewhere don't know how it's done)
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
_AnShUmAn_ wrote: GetVolumeInformation() will do more than half the work.
How so? While it may be used to get the serial number of the hard drive or the CD drive, how do you propose it getting the MAC address or the CPU id?
_AnShUmAn_ wrote: For MAC Address - (this is only my opinion)
one thing that would work all the time is run a shell
"arp -a" and internally search the output for the needed MAC.
Even though APIs exist for obtaining MAC addresses, capturing the output or arp -a is not going to work. You should use ipconfig /all instead.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Perhaps you will found methods based on querying the System Registry. Nevertheless, consider that the data you need can be obtained using Windows Management Instrumentation (WMI):
- MAC Address -- see
Win32_NetworkAdapter WMI class; - HDD Serial Number -- see
Win32_DiskDrive WMI class; - CD/DVD Drive Serial Number -- see
Win32_CDROMDrive WMI class; - CPU Processor ID -- see
Win32_Processor WMI class.
WMI seems to be difficult to use in C++. So use it if no easier equivalent methods.
|
|
|
|