c - 从 CSV 读取字符串时出错(核心转储)
问题描述
我不断收到同样的错误,我是编程新手,所以我不太确定语法是否正确。
每次我运行它时,它都会返回分段错误(核心转储),我什至不确定是否可以用字符串(地址)而不是扩展的文件名打开文件。我正在读取的文件也是 CSV,但格式为 txt。
我正在使用 C99
#define BUFFER_SIZE 1024
#define TAM_PERGUNTAS 128
struct question{
char category[TAM_PERGUNTAS];
char question[TAM_PERGUNTAS];
char option1[TAM_PERGUNTAS];
char option2[TAM_PERGUNTAS];
char option3[TAM_PERGUNTAS];
char correct[TAM_PERGUNTAS];
};
struct question vec_question[BUFFER_SIZE];
void questions() {
FILE *perguntas;
int numaleat=0;
int num_questions, counter = 0, index, temp_randomizer=0;
char line[BUFFER_SIZE];
char answer[32];
char address[TAM_PERGUNTAS];
address[0] = '\0';
srand(time(NULL));
printf("Digite agora o numero de perguntas desejadas.(MAX 20) : "); //Insert Number of questions
scanf("%d", &num_questions);
printf("\n");
for (counter = 0; counter < num_questions; counter++) {
temp_randomizer = rand() % j; //j Represents the number o CATEGORIES at play and acts as a marker in the SELECTION string
sprintf(address, "%s.txt", SELECTION[temp_randomizer]);
perguntas = fopen(address, "r");
if (perguntas == NULL) {
printf("ERROR OPENING FILE!");
}
index = 0;
while (fgets(line, sizeof(line), perguntas) != NULL) {
strcpy(vec_question[index].category, strtok(line, ";"));
strcpy(vec_question[index].question, strtok(NULL, ";"));
strcpy(vec_question[index].option1, strtok(NULL, ";"));
strcpy(vec_question[index].option2, strtok(NULL, ";"));
strcpy(vec_question[index].option3, strtok(NULL, ";"));
strcpy(vec_question[index].correct, strtok(NULL, ";"));
vec_question[index].correct[strlen(vec_question[index].correct) - 1] = '\0';
index++;
}
fclose(perguntas);
index = 20;
numaleat = rand() % index;
printf("%s : %s\n%s\n%s\n%s",vec_question[numaleat].category,vec_question[numaleat].question,vec_question[numaleat].option1,vec_question[numaleat].option2,vec_question[numaleat].option3);
for (int i = 0; i < num_users; i++) {
printf("\n%s: ", &users[i][20]);
scanf("%s", &answer[32]);
if (answer == vec_question[numaleat].correct)
userspoints[i] += 1;
}
}
}
解决方案
一般来说,应该假设像 strtok 这样的函数可能会失败。有时它会失败并返回 NULL 值。您输入中的简短记录可能是原因。
考虑将它与循环一起使用,并在 strtok 返回 NULL 时中断循环。
我在这里找到了一个简单的例子。
#include <string.h>
#include <stdio.h>
int main () {
char str[80] = "This is - www.tutorialspoint.com - website";
const char s[2] = "-";
char *token;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL ) {
printf( " %s\n", token );
token = strtok(NULL, s);
}
return(0);
}
请注意,它会执行一次 strtok 来获取第一个令牌。这可能会返回 NULL 在这种情况下循环不会运行。如果它不返回 NULL,那么它会打印该标记,并要求 strtok 提供下一个标记。它一直这样做,直到 strtok 返回 NULL。
推荐阅读
- r - data.frame 中所有变量对的元素乘法之和
- unit-testing - Dart:如果没有发出任何值,则对流进行单元测试
- jmeter - Non HTTP response message: Socket closed 是什么意思?
- struts2 - Struts 2-Tiles 3 访问问题
- json - json 无法解组字符串
- javascript - 在 Material UI 中选择日期时出现“handleChange 不是函数”错误
- java - 光标相对于窗口的坐标
- google-sheets - Convert multi column data in a single row that contains delimiter, to a multi row and flatten delimiter so only one item is in each cell
- reactjs - NextJS Back/Forward-button not working in Instagram Inline Safari
- c# - C# 中的应用程序池状态