即时尝试从文件中复制一些文本并将它们保存在结构成员中,我在cmd.exe上运行我的程序并崩溃,但是当我在代码块或Visual Studio上运行它时作品,无法在cmd.exe上运行此程序
#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);
}
}
在文件中的文本:
凯文|克拉克| NS | 3月15日2001|5555555|[email protected]|123街道
没有任何人知道为什么它崩溃时我尝试在cmd.exe上运行它?
您的意思是,当您运行编译的可执行文件时,它会崩溃,但是当您在Visual Studio中运行代码时,它的工作原理是什么? –
你为什么写'char nom ['']'? –
你是对的@EitanT – user1193803