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

9
  • 2
    You don't show the declaration of TempFilePath, it's essential to solving your problem. I'm guessing it's vector<char*> when it should be vector<string>. Commented May 13, 2021 at 4: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 Commented May 13, 2021 at 5:00
  • 1
    I would suggest you to use std::vector<std::string> instead of std::vector<char*>. Commented May 13, 2021 at 5: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) Commented May 13, 2021 at 5: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) Commented May 13, 2021 at 5:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you!I understand, but I still don't have a way to realize it. Can you provide me with a modified plan
@Ahri Taking a good book 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.
Sir, you are right, and I am doing so. Now I have to ask in order to complete a task without a tutor (╥﹏╥)
As has been said before in the comments, I strongly recommend using vector<string> rather than vector<char*>. That will solve the issue you're asking about.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.