【发布时间】:2020-06-07 20:46:06
【问题描述】:
我正在处理一个需要处理文件的项目。我目前正在向文件中添加东西,但我正在尝试做的是,当我检测到一个空行时,我会打印一些东西,当该行包含一些东西时,我会打印其他东西。我试图将空行存储到一个字符中,并在 if 语句中将其与'' 进行比较,但我没有成功。
这是我的文件的样子,1,2,3,4,5,6,7 不在我的文件中,它只是代表行:
1 Nilton
2 10
3 Ok
4 Ola
5 Oh
6 2
7
这是我尝试的:
char file_nome_receita[LOAD], file_ingredientes[MAX], file_modo_de_preparo[MAX],
file_nome_criador[LOAD], newline;
int file_tempo_preparo, file_vezes_preparado;
if (file == NULL) {
printf("Nao ha receitas no arquivo!\n");
} else {
func_select = 0;
fgets(file_nome_receita, LOAD, file);
fscanf(file, "%d\n", &file_tempo_preparo);
fgets(file_ingredientes, MAX, file);
fgets(file_modo_de_preparo, MAX, file);
fgets(file_nome_criador, LOAD, file);
fgets(buf, sizeof buf, file);
sscanf(buf, "%d", &file_vezes_preparado);
fscanf(file, "%c", &newline);
if (newline == NULL)
{
printf("Bat");
}
fscanf(file, "%c", &newline);
if (newline == '\n') {
printf("result");
}
警告;
warning: comparison between pointer and integer
39 | if (newline == NULL)
【问题讨论】:
-
你能用我的一段代码更好地解释一下吗?
-
fgets(file_nome_criador, MAX , file);-->fgets(file_nome_criador, LOAD , file);或更好的fgets(file_nome_criador, sizeof file_nome_criador , file); -
@chux-ReinstateMonica 我已经尝试过了,但我的 if 比较仍然不会打印“换行符”
-
我计数 7 个连续调用以从文件中读取,错误检查绝对为零。
-
那么不要将
' '与前面的fscanf(file, "%d\n", &file_vezes_preparado);一起消费 提示:不要将fgets()与*scanf()混用
标签: c