-1
I am studying and implementing some C++ schemes but it is causing errors when assigning value to the matrix. Well... I have a txt file that contains lines with "[email protected]" content. My Ret variable is returning the values correctly, but at the time of inserting in the vector the following error is popped:
Exception generated in 0x0F6DE559 (ucrtbased.dll) in Consoleapplication3.exe: 0xC0000005: breach of access when writing in local 0xCCCCCC.
Debugging the code, all values are apparently correct, but the exception is thrown anyway. I previously tried to do the same process without strcpy, inserting directly into the desired index and using Strtok to separate the string, but the value of all index was changed to the last record always. Could someone give me a light to understand where I’m going wrong?
FILE *fp = NULL;
fp = fopen("Emails.txt", "rt");
char* matriz[99][2];
int index = 0;
char str[256];
char nome[20];
char email[60];
while (1)
{
char *ret = fgets((char*)str, 255, fp);
if (ret == NULL)
break;
sscanf(str, "%s %s", &nome, &email);
strcpy(matriz[index][0], nome); // Acusa erro aqui!
strcpy(matriz[index][1], email);
index++;
};
But if it’s C++ why did you make a code in C? https://answall.com/q/392951/101
– Maniero
You are not allocating space for the string. You may have up to 99 index, it seems to me, but you do not allocate in matrix. Anyway, you tag C++, so you should use containers and Std:string:.
– Kevin Kouketsu
Thanks for the answers! So, I’m actually studying a C++ program ready, I ended up following his standards that seem to be in C... Anyway, I was kind of lost anyway! All the research I did on the net regarding C++ returned these methods and this way of doing the code... hehehe
– Allotropos