ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Looking at that comment it would seem you have no idea what the two instructions do. Do you understand what strncpy() does? I'll paste an excerpt from the manual page of strcpy, on my Gentoo Linux system:
DESCRIPTION
The strcpy() function copies the string pointed to by src (including the terminat-
ing `\0' character) to the array pointed to by dest. The strings may not overlap,
and the destination string dest must be large enough to receive the copy.
The strncpy() function is similar, except that not more than n bytes of src are
copied. Thus, if there is no null byte among the first n bytes of src, the result
will not be null-terminated.
#2. Another thing:
Code:
char buffer1 [256]; /*prima stringa da acquisire da cercare*/
char buffer2 [256]; /*seconda stringa da sostituire alla prima stringa trovata*/
char buffer3 [1000]; /*per contenere il testo nel file1*/
Yet:
Code:
char buffer5[4096];
buffer5 seems to be too big. Why not allocate it dynamically? If you don't want to do this but decide to fine tune the size to a lower value .. make sure it's not so low as to allow buffer overflows to occur.
#3. I think this is what kaz2100 meant.. Your function returns a pointer to char. However you do nothing with this return value:
Code:
stringa_sost(buffer3, buffer1, buffer2);
^^ That is the actual call to the function. As you can see, stringa_sost() could be of type void for all main() cares. I'm thinking: you probably think that by calling:
The problem that I found with the original code was that whenever you attempt to replace a substring that's shorter in lenght that the new string, the buffer overflows and the logic crashes. I have added additional logic to fix this problem should you ever encounter such a problem.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.