斯特托克获取分段错误读取文件
strtok getting segmentation error read file
在 strtok 上出现分段错误,我的输入字符串lyne
定义为字符数组而不是指针,但似乎不起作用。 这是在C和Linux中
typedef struct
{
int x;
char *y;
} child;
typedef struct{
child *details;
} parent;
fp = fopen(filename,"r"); // read mode
char lyne[25];
char *item;
fgets(lyne,25,fp);
parent record;
record.details= malloc (5 * sizeof(child));
while (fgets(lyne,25,fp)) {
printf("test %s n",lyne);
item = strtok(lyne," ");
strcpy(record.details->y,item);//seg error on this line
}
fclose(fp);
my file looks like this
file#1
ABC 100
BCE 200
OUTPUT:
test ABC 100
Segmentation fault
您尚未将内存分配给结构子成员"y",因为您的结构是
typedef struct
{
int x;
char *y;
} child;
你要做的是:
record.details->y = malloc(sizeof(char)*(strlen(item) + 1));
strcpy(record.details->y,item);
必须添加 使用前parent.deatils->y = (char *) malloc(24);
相关文章:
- 分段错误 - 读取初始化指针的数组
- 在指向函数中读取变量时出现分割错误
- 读取某些文件时出现分段错误,似乎与文件大小无关
- 从对象C++中的文件读取内容时出现分段错误
- 我编写了以下代码来读取C++矩阵,然后打印其行和列.我收到此错误
- 可能的 ComputeCPP SYCL 错误读取嵌套缓冲区
- Eigen SparseLU解决错误读取访问冲突,这>m_sup_to_col被0x111011101110112
- 访问违规读取位置和错误读取字符串的错误
- libpng 错误:读取错误
- 错误读取字符到矩阵
- 分段错误读取 JSON 文件
- 斯特托克获取分段错误读取文件
- ifstream 初始化错误 - 读取字符时出错
- 读取错误-读取时无限
- 异常错误:读取位置0xDDDDDDDD时发生访问冲突
- 如何修复此错误读取位置0x00000008时发生访问冲突
- GDB:<错误读取变量>,带有 C++ 向量
- 找不到错误 读取大小 4 瓦尔格林德无效
- 分割错误读取文件
- libpng错误:读取错误(Visual Studio 2010)