对于我的家庭作业,我需要逐行从用户那里输入输入并将其打印在输出文件中,但是我总是得到空文件。
当它从文件中读取结果并将结果写入文件时,我使其能够在不同的情况下工作。但是在这种情况下,我无法弄清楚我在做什么错。
while(-1 != (nchr = getline(&buffer, &buffer_size, stdin)))
{
if(buffer == NULL){
return EXIT_FAILURE;
}
//last line in file without \n ??
buffer[--nchr] = '\0';
char tmp[nchr];
strcpy(tmp, buffer);
if(optionI){
convertToLow(buffer);
}
if(optionS){
deblank(buffer);
}
if(optionO){
// !!!!!!!!!!!!!!!!!!!
// won't print in file
// !!!!!!!!!!!!!!!!!!!
if (is_ok(buffer)) {
fprintf(outfile,"%s is ok\n", tmp);
} else {
fprintf(outfile,"%s is not ok\n", tmp);
}
} else{
if (is_ok(buffer)) {
printf("%s is ok\n", tmp);
} else {
printf("%s is not ok\n", tmp);
}
}
}
free(buffer);
}
fclose(outfile);
fflush(stdout);
fflush(stdin);