You are using the address of record which is held by the variable temp as the key in your map. Its address is always the same so subsequent additions are ignored because multiple keys are not allowed in a map.
A simple solution would be to replace all the pointer to null terminated strings (char* typed variables) with std::string
s.
Using std::strings rather than C strings is considered a more modern C++ coding style and is far less error prone. The code below is semantically similar to yours but compiles and runs under MSVC2013. I would also recommend using regex
rather than strtok
or similar C string parsing functions. Regex
is a bit of intellectual overhead up front but it pays off in the long run as it is less error prone and more powerful.
Another side note, using nullptr
rather than NULL
is considered better if your compiler supports it. see What exactly is nullptr?
using namespace std
is also considered bad practice, see : Why is "using namespace std" considered bad practice?
Here is the code:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <cstring>
int main()
{
char buffer[1024];
char * line;
std::map<std::string, std::vector<std::string>> mapFile;
std::vector<std::string> myVector;
const std::string fileName{ "E:\\test project\\ConsoleApplication1\\Debug\\TestFile.csv" };
FILE *fstream;
if ((fstream = fopen(fileName.c_str(), "r")) == NULL) { //should at least use fopen_s here, see https://stackoverflow.com/questions/906599/why-cant-i-use-fopen
printf("File could not be opened\n");
return -1;
}
else{
while ((line = fgets(buffer, sizeof(buffer), fstream)) != NULL)
{
char * result = strtok(line, ","); //stdtok is very unsafe and outdated, should use regex something simmilar
while (result != nullptr){
myVector.push_back(std::string(result));
result = strtok(nullptr, ","); //stdtok is very unsafe and outdated, should use regex something simmilar
}
if (myVector.empty()){
//TODO handle error here, may be a line like ",,,"
}
else{
mapFile.insert(std::pair<std::string, std::vector<std::string>>(myVector[0], myVector));
}
}
fclose(fstream);
}
return 0;
}