Reputation: 51
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. 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
Upvotes: 1
Views: 236
Reputation: 51
I've resolved the problem by simply specifying the absolute path in fopen
Upvotes: 1