-1

I'm new to C++ and i have to make a recurrent modification over a big project. I have to take all strcpy and strcat methods and convert them into sprintf. I figured out that basically, the conversion would be :

strcpy(out,in) to sprintf(out, "%s", in)

and

strcat(out,in) to do{ int temp = strlen(out); sprintf(out+temp, "%s", in); } while(0)

First question, does that code works?

Second question, there is absolutely no way that the out and in variable could be numerical and that I would need to use %d instead of %s, right?

Third question, the out and in variables will be different in every iteration of the method in all the code I wanna modify, so the modification must be flexible to the variable names. Any way I can do it? It's a VisualStudio 98 project ... But I am on linux ubuntu and windows XP.

Thanks for your time and suggestions!

Oh and don't worry I made a back up of my files :D

Franko
  • 131
  • 1
  • 10
  • 2
    Note that `strcpy` and `strcat` are C library functions that operate on C strings - if you really want to learn C++ and not C then you should look at using C++'s `string` class. – Paul R Mar 29 '12 at 15:23
  • possible duplicate of [How can I modify code of all files in folders](http://stackoverflow.com/questions/9914259/how-can-i-modify-code-of-all-files-in-folders) – Paul R Mar 29 '12 at 15:24
  • 1
    "I'm new to C++ and i have to make a recurrent modification over a big project" - sounds like a good recipe for disaster. – Tadeusz Kopec for Ukraine Mar 29 '12 at 15:29
  • 1
    I'm curious about the reason you have to make this modification. I can't think any benefit to these changes. – Michael Burr Mar 29 '12 at 15:33
  • Have you considered changing `strcpy` and `strcat` to `my_strcpy` and `my_strcat` and then writing those two functions with `sprintf`? That seems less error-prone and easier to do with search-and-replace. – Blastfurnace Mar 29 '12 at 18:36

2 Answers2

0

Not clear why you would want to do this...

But be that as it may, from looking at it the first one works; the second one doesn't need to the do { } while(0) around it - you could just use {} instead to keep tmp local. But it looks like it would work. Or you could use

sprintf( out, "%s%s", in ); 

and save yourself the bother.

But my main concern with any of these methods is that you aren't checking for buffer overflow. What if out isn't large enough to hold the result? Of course that could happen in the original code, so perhaps it is being checked.

DRVic
  • 2,481
  • 1
  • 15
  • 22
0

First question, does that code works?

Yes, but simpler:

strcat(out, in) -> sprintf(out + strlen(out), in)

Second question, there is absolutely no way that the out and in variable could be numerical and that I would need to use %d instead of %s, right?

No - strcpy and strcat only work with string content.

the out and in variables will be different in every iteration of the method in all the code I wanna modify, so the modification must be flexible to the variable names. Any way I can do it? It's a VisualStudio 98 project ... But I am on linux ubuntu and windows XP.

It's tricky... could use regexp substitutions, but it could fail for really weird code. You could also try employing the preprocessor and a hash-define, but you'd want to disable include expansion, all the extra symbols the preprocessor normally silently creates... which may or may not be supported by command-line switches to your preprocessor.

How many instances do you have to change? If not too many, could try regexp and manually review....

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • It's way to much in way to many different files to verify manually ... But i don't really understand what kind of error would #define strcpy(out, in) sprintf(out, "%s", in) #define strcat(out, in) sprintf(out+strlen(out), "%s", in) in the header of my project do. Is there a way to make the out and in flexible? to what's in the actual code? – Franko Mar 29 '12 at 15:46
  • @Franko: if you use those #defines - it will substitute the actual source code text at each point of use, not just the words "in" and "out".... Try it. – Tony Delroy Mar 29 '12 at 17:06
  • It works, some bugs appeared but I just made a method that makes sure the buffer is big enough. No problem detected yet. – Franko Mar 29 '12 at 19:15
  • @Franko: this won't introduce bugs, but if the existing code had bugs it won't fix them either.... – Tony Delroy Mar 30 '12 at 16:06