|
Where am I going wrong? I am trying to implement strcpy manually.
<code>
main()
{
char *source = "Hi", *dest = "";
while(*source++) *dest++ = *source;
}
Visual C++ 6.0 Compiler with service pack 6. WindowsXP 32 bit edition.
|
|
|
|
|
Aljechin wrote: *dest = "";
u havent allocated memory for the dest
nave
|
|
|
|
|
Neither have I allocated memory for the source...
char *source = "Hi", *dest = "";
dest = "Hello world";
This is working fine... Someone please tell me why is it crashing if i enable that while loop.
|
|
|
|
|
Aljechin wrote: Neither have I allocated memory for the source...
Yes you have. source is 3 characters long, "Hi" and the terminating null byte.
modified:
Or rather, source is pointing to a memory area which is initialized to "Hi" and the terminating null byte.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
kakan wrote: Yes you have. source is 3 characters long, "Hi" and the terminating null byte.
That way even the dest has the terminating null byte. ( "" == '\0' ).
Sorry but I am just curious why it's not working? I am not expert like you guys. Please help me.
I tried declaring *dest as "Halo" and it still crashes.
|
|
|
|
|
You shouldn't do it this way. Try this instead:
main(){
char temp[30];
char *source = "Hi", *dest = temp;
while(*source++) *dest++ = *source;
}
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
kakan wrote: You shouldn't do it this way. Try this instead:
Please tell me where exactly was I going wrong. Can you show me a sample to implement strcpy this way? I am trying like mad since half an hour.
|
|
|
|
|
This really is basics, I suggest you read a book that covers C. It would make things clearer to you.
Your original code was this:
main()
{
char *source = "Hi", *dest = "";
while(*source++) *dest++ = *source;
}
A declaration like this:
char *source = "Hi"
is (normally) used for constant values (that doesn't change during the execution of the program).
If you want a buffer that can be modified, you'd better allocate it, like this:
char temp[50];
Now you have a buffer that can hold up to 50 characters. Use it as you wish, i.e. like this (as long as your source text doesn't exceed 49 charaters plus the terminating null character):
strcpy(temp, "Hello World");
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
HI
me also trying for it...
its working release version not in debug version why is that?????
|
|
|
|
|
The original code stomps memory that it doesn't own. In release mode, the behaviour is undefined, in debug, the IDE is watching if you do this, and letting you know that you've done it.
The release mode version will crash, sometimes.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
HI
So why is char * source = "Hello" giving problem
why am i able to just read memory not write to it????
|
|
|
|
|
You can write to it, I believe. The problem is, it's a fixed memory location, of fixed size. You can't reassign the string to a longer length. It's bad practice to define a string this way, unless you expect it to be a constant in your program.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Aljechin wrote: Sorry but I am just curious why it's not working? I am not expert like you guys. Please help me.
What are you not understanding? It's been explained several different ways. In your code snippet, dest is not pointing to a valid (i.e., allocated) memory location. Until it is, you cannot copy characters to it from source . strcpy() would behave in the same fashion had you used:
char *source = "Hello", *dest = "";
strcpy(dest, source); One solution is to use:
char *source = "Hello", dest[6] = "";
strcpy(dest, source); Another would be:
char *source = "Hello", *dest = new char[6];
strcpy(dest, source);
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
There are two problems.
1. Your char* variables are initialized to point at string literals. You should never write to such memory, as the compiler may store the strings in read-only memory, causing a crash if you write to them.
2. Even if the memory were writable, dest only has room for one char. When you copy source into it, you're writing off the end of the array.
|
|
|
|
|
Thank you very much, I understand it now. I did not give you that 2 vote. I am checking the answer today only.
|
|
|
|
|
Hi!!!
I am just beginner in this field. I want to search for one specified file from the folder. as in I have one file called “WG_LL.txt “in my file.. n in each Webdoku section this file is contain inside loopo folder..ie File WG_LL.txt file is always inside of loopo folder but the location of loopo folder is different.
For example…
I created one dialogue box… and it contain my Webdoku section and path of the section..
Webdoku section Path of the section
DEMO70E D:\Progsample\ProdV70E\ProdMast\
DEMO70D C:\project\webdoku\
SAMPLE D:\Prog. sample\ProdV70E\WebDoku\
DEMO80D E:\blabla\webdoku\
DEMO60E D:\Progsample\WebDoku\
WT1 \\rekafs\Projekte_USB\myfolder\9_WebDoku\
WT2 \\rekafs\Projekte_USB\myfolder\9_WebDoku\
If suppose, I select DEMO70E from my dialogue box then it should be search for “WG_LL.txt file” and here path of the file is …D:\Progsample\ProdV70E\WebDoku\loopo\WG.LL.txt.
If suppose I select DEMO60E, then it should be search for file “WG_LL.txt” and path of the file is D:\Progsample\WebDoku\loopo\WG_LL.txt..
If suppose I select WT1, then it should be search for file “WG_LL.txt” and path of the file is \\rekafs\Projekte_USB\myfolder\9_WebDoku\WT1\loopo\WG_LL.txt
In short, here I want to search for loopo folder coz WG_LL.txt” file will be contain in Loopo folder…Here I have some code in this file (WG_LL.txt) so I need this file.
In my method…my section = selected Webdoku section (for example..DEMO70E)
&
Mypath =selected section path ( D:\Progsample\ProdV70E\ProdMast\)
So how can I find that file with location of the path.
Thank you very much…
Shah Satish
|
|
|
|
|
If you want to search for a file you can use of
FindFirstFile/FindNext<code> <br />
<br />
<div class="ForumSig"><div style="padding: 0; margin: 0; background-color: rgba(170, 214, 255, 1); text-align: center; font-family: Arial; font-weight: bold; font-style: italic; font-size: 9pt"><br />
<div style="color: rgba(255, 255, 255, 1); width: 300px"><br />
<hr width="250" size="7">WhiteSky<hr width="220" size="7"></div></div></div>
|
|
|
|
|
Hi!!
thanks fro your reply..
In short, I want to search file called "WG_LL.txt" from specified path..
see my code...
if i select DEMO70E then specified path is
D:\progrsample\prodv70E\prodmast...
so i want to search that file in this directory..(D:\progrsample\prodv70E\prodmast...)
Thank you..
Shah Satish
|
|
|
|
|
Well you can use of FindFirstFile did you try it I think its good for your purpose
|
|
|
|
|
Hi!!!
thanks for your reply..One more question please..
I have dialogue box and it contain name of the section and path of the section..like..
" Webdocu section " " Path of the section "
DEMO70E D:\Progsample\ProdV70E\ProdMast\
DEMO70D C:\project\webdoku\
SAMPLE D:\Prog. sample\ProdV70E\WebDoku\
DEMO80D E:\blabla\webdoku\
DEMO60E D:\Progsample\WebDoku\
WT1 \\rekafs\Projekte_USB\myfolder\9_WebDoku\
WT2 \\rekafs\Projekte_USB\myfolder\9_WebDoku\
Each section having own "wg_ll.txt" file..
for exapmple...If i will seclect DEMO70E then it will open WG_LL.txt file of that section (DEMO70E)...
Can u tell me how i can find WG_LL.txt file??..I know its somewhere D:\Progsample\ProdV70E\ProdMast\ in this path but dont know exact location.I also want path of that file.
So how can i find wg_LL.txt file with path for selected section..??
Shah Satish
|
|
|
|
|
You can use of CFileDialog for see a file is your purpose?
|
|
|
|
|
Shah Satish wrote: In short, I want to search file called "WG_LL.txt" from specified path..
So if you already know the file's path, why not use _access("D:\\progrsample\\prodv70E\\prodmast\\wg_ll.txt, 0) ?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Nop..This is the answer of my file path...
Actually my path is "D:\progrsample\prodv70E\prodmast\..
and my file is contain in "D:\progrsample\ Prodv70E\webdocu\loopo\wg_ll.txt"..
i just want to search folder from left side of my path.. but i dont know how to..
Thank you for your reply..
Shah Satish
|
|
|
|
|
Your question is very incoherent.
Shah Satish wrote: and my file is contain in "D:\progrsample\ Prodv70E\webdocu\loopo\wg_ll.txt"..
Since you already know the location of the file, what in the world are you searching for?
If you simply want to search for a file, the answer has already been provided here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hi!!
I think my question is not clear..Ok looks once again..
I have dialogue box and it contain name of the section and path of the section..like..
Webdocu section Path of the section
DEMO70E D:\Progsample\ProdV70E\ProdMast\
DEMO70D C:\project\webdoku\
SAMPLE D:\Prog. sample\ProdV70E\WebDoku\
DEMO80D E:\blabla\webdoku\
DEMO60E D:\Progsample\WebDoku\
WT1 \\rekafs\Projekte_USB\myfolder\9_WebDoku\
WT2 \\rekafs\Projekte_USB\myfolder\9_WebDoku\
Each section having own "wg_ll.txt" file..
for exapmple...If i will seclect DEMO70E then it will open WG_LL.txt file of that section (DEMO70E)...
Can u tell me how i can find WG_LL.txt file??..I know its somewhere D:\Progsample\ProdV70E\ProdMast\ in this path but dont know exact location.I also want path of that file.
So how can i find wg_LL.txt file with path for selected section..??
Shah Satish
|
|
|
|