简体   繁体   English

fprintf 不打印

[英]Fprintf not printing

I've been trying to do file excercises in C but when I run programs from the professor or tutorials (which should work), the file always comes out blank.我一直在尝试用 C 语言做文件练习,但是当我运行教授或教程(应该有效)的程序时,文件总是空白。 Is there a solution to this?这个问题有方法解决吗?

My computer is pretty old, but it can still run various programs, I don't undersand why it doesn't work with files我的电脑很旧,但它仍然可以运行各种程序,我不明白为什么它不能处理文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*program that prints on a file your shopping list*/
int main(int argc, char **argv)
{
    FILE *fp= fopen("Shopping list.txt", "w");
    int end= 0; //have you finished writing the articles
    char article[80];   //the article you want to buy
    int n;      //quantity

    while(!end){
        printf("What do you need to buy? ");
        fgets(article, 80, stdin);
        article[strlen(article)- 1]= '\0';
        fprintf(fp, "%s ", article);

        printf("How much of it? ");
        scanf("%d", &n);
        fprintf(fp, "%d\n", n);

        printf("Are you done? (1= Yes, 0= No) ");
        scanf("%d%*c", &end);
    }
    fclose(fp);
}

It should print out on the file (which is created and remains empty) your input.它应该在文件(已创建并保持为空)上打印出您的输入。 There is no error message没有错误信息

我通过简单地在 fopen 中指定绝对路径解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM