0

I want to build a series of paths with serial numbers through a for loop, so I use a series of strcpy and strcat (I know there is a string method, but please forgive me, my technique is really poor). But after the loop, I get a series of the same results.

Here is my code:

#include <iostream>
#include <io.h>
#include <vector>

vector<char*> TempFilePath;    
char temp[300];
strcpy(temp, EndFilepath);
strcat(temp, "\\Temp");
createDirectory(temp);
char TempFilePathChar[300];
for (int j = 0; j < filevector.size(); j++)
{
    int number = j + 1;
    char TEMPNum[1];
    itoa(number, TEMPNum, 10);
    strcpy(TempFilePathChar, temp);
    strcat(TempFilePathChar, "\\tempData");
    strcat(TempFilePathChar, TEMPNum); 
    strcat(TempFilePathChar, ".tif");
    strcpy(TempFilePathChar, TempFilePathChar);
    TempFilePath.push_back(TempFilePathChar);
}

The EndFilepath="E:\\", the size of filevector is 2. Undoubtly, I'd like to get the result of flowing:

TempFilePath[0]="E:\\Temp\\tempData1.tif"
TempFilePath[1]="E:\\Temp\\tempData2.tif"

But after running, the result is like following:

 TempFilePath[0]="E:\\Temp\\tempData2.tif"
 TempFilePath[1]="E:\\Temp\\tempData2.tif"

Can someone tell me why and how to change it?

Note:I still want to use vector < char * > instead of vector < string >, because I use a lot of functions on the network, and their return value and input value are char * type. Of course, if there is a method, it can still achieve the above purpose. Thanks again

Ahri
  • 5
  • 3
  • 2
    You don't show the declaration of `TempFilePath`, it's essential to solving your problem. I'm guessing it's `vector` when it should be `vector`. – Mark Ransom May 13 '21 at 04:56
  • @MarkRansom Sorry, this is my negligence. He is indeed a vector. I have revised the inquiry. Could you tell me what the problem is – Ahri May 13 '21 at 05:00
  • 1
    I would suggest you to use `std::vector` instead of `std::vector`. – Daniel Langr May 13 '21 at 05:03
  • You only have one `char temp[300];` buffer, with multiple pointers stored in the vector that all point to it. So it's natural that all of the pointers in the vector point to the same data (whatever is currently in `temp`) – Jeremy Friesner May 13 '21 at 05:04
  • Also your `char TEMPNum[1]` is too short; if you want it large enough to store one ASCII character, it needs to be at least `char TEMPNum[2]`, so that you have one char for the digit and another char to hold the NUL-terminator byte. (and if you want it to store numbers with more than one digit in them, it needs to be bigger than that) – Jeremy Friesner May 13 '21 at 05:06
  • @DanielLangr thank u !I'll change it – Ahri May 13 '21 at 05:38
  • @JeremyFriesner Thank u!Could you please give me a revised plan? – Ahri May 13 '21 at 05:39
  • Like Mark Ransom said, if you use `vector` instead of `vector` you will get the behavior you want. – Jeremy Friesner May 13 '21 at 14:11
  • @JeremyFriesner , Thanks a lot,i GOT IT – Ahri May 14 '21 at 06:05

1 Answers1

3

Your vector's elements are all the same pointer value, the address of the array TempFilePathChar. In your loop you are overwriting the content of that array so you are always getting the content produced by the last iteration of the loop.

jkb
  • 2,376
  • 1
  • 9
  • 12
  • Thank you!I understand, but I still don't have a way to realize it. Can you provide me with a modified plan – Ahri May 13 '21 at 05:40
  • @Ahri Taking a [good book](https://stackoverflow.com/q/388242/580083) and start learning C++ from the beginning sounds like a good plan. You seem to be missing some basic knowledge, such as how to use strings and how objects are created, or what is their lifetime and scope. – Daniel Langr May 13 '21 at 06:02
  • Sir, you are right, and I am doing so. Now I have to ask in order to complete a task without a tutor (╥﹏╥) – Ahri May 13 '21 at 06:05
  • As has been said before in the comments, I strongly recommend using `vector` rather than `vector`. That will solve the issue you're asking about. – jkb May 13 '21 at 17:46