Reputation: 43
im trying to copy some texts from a file and save them in struct members, i run my program on cmd.exe and it crashed, but when i run it on codeblocks or visual studio it works,
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct AMIGOS
{
char nom[' '];
char apellido[' '];
char nompila[' '];
char tel[' '];
char correo[' '];
char dir[' '];
char fecha[' '];
};
int main()
{
struct AMIGOS reg;
char registro[128];
char**datos;
char*dato;
datos = (char**)malloc(10*sizeof(char**));
int tam;
int i=0;
FILE* pt = fopen("arch.txt","r");
if(pt==NULL)
{
printf("filenotfound\n");
}
else
{
while(fgets(registro,128,pt))
{
dato = strtok(registro,"|");
while(dato)
{
tam = strlen(dato);
datos[i] = (char *)malloc(tam);
memcpy(datos[i],dato,tam);
datos[i][tam]=0;
i++;
datos[i]=0;
dato = strtok(0,"|");
}
}
strcpy(reg.nom,datos[0]);
strcpy(reg.apellido,datos[1]);
strcpy(reg.nompila,datos[2]);
strcpy(reg.fecha,datos[3]);
strcpy(reg.tel,datos[4]);
strcpy(reg.correo,datos[5]);
strcpy(reg.dir,datos[6]);
printf("%s\n",reg.nom);
printf("%s\n",reg.apellido);
printf("%s\n",reg.nompila);
printf("%s\n",reg.fecha);
printf("%s\n",reg.tel);
printf("%s\n",reg.correo);
printf("%s\n",reg.dir);
}
}
the text on the file:
kevin|clark|ns|march 15 2001|5555555|[email protected]|123 street
does anybody know why it crash when i try to run it on cmd.exe ?
Upvotes: 0
Views: 261
Reputation: 89975
Here are a couple of problems:
datos = (char**)malloc(10*sizeof(char**));
You actually mean:
datos = (char**)malloc(10*sizeof(char*));
(Following the normal C idiom of:
var = malloc(n * sizeof *var);
would avoid this mistake. In practice, the sizes are likely to be the same, but it is wrong in principle. Also note that casting the result of malloc
is also frowned upon in C.)
tam = strlen(dato);
datos[i] = (char *)malloc(tam);
memcpy(datos[i],dato,tam);
datos[i][tam]=0;
You're overflowing your buffer. You allocated tam
bytes of memory, then you copied tam
bytes into it, but then you try to NUL-terminate it. You need to allocate tam + 1
bytes.
As other bits of advice:
strcpy
is unsafe; you can't guarantee that the tokenized input won't cause you to overflow those buffers as well.fgets(registro,128,pt)
would be better as fgets(registro, sizeof registro, pt)
.Upvotes: 1
Reputation: 24895
I don't know why it works in Visual Studio but crashes in Cmd Line. But, there are a few issues in your code:
tam = strlen(dato);
datos[i] = (char *)malloc(tam); //You are not allocating memory for '\0' character
memcpy(datos[i],dato,tam);
datos[i][tam]=0; //So this is effectively an array out of bound write
should be
tam = strlen(dato);
datos[i] = (char *)malloc(tam+1);
memcpy(datos[i],dato,tam);
datos[i][tam]=0;
Upvotes: 1
Reputation: 121971
This is writing beyond the end of the array:
tam = strlen(dato);
datos[i] = (char *)malloc(tam);
memcpy(datos[i],dato,tam);
datos[i][tam]=0; <---- 'tam -1' is the last element
You need to add an additional character to store the null terminator, or you could just use strdup()
:
datos[i] = strdup(dato);
Upvotes: 2
Reputation: 399833
This:
char nom[' '];
is very strange code, it almost certainly doesn't do what you expect it to. I would be interested in hearing the motivation for this code.
It will basically declare nom
as an array of characters, whose length is given by the integer value of the character SPACE. Assuming an ASCII machine, this will be equivalent to:
char nom[32];
Upvotes: 0