fprintf打开文件,但不写在上面

时间:2018-05-24 22:32:11

标签: c

我的程序会打开文件,但它不会写任何内容。这是结构和代码

typedef struct {
    int prioridade;
    int id;
    time_t data_criacao; 
    char descricao[MAX_STRING];
    int estado;
    pessoa *p;
    time_t prazo_conclusao;
    time_t data_conclusao;
} card;


typedef struct node_card {
    card *c;
    struct node_card*next;
} node_card;

typedef struct {
    node_card *header;
    int size;
} list_cards;

void save_file(list_cards *list){
    FILE *file;
    file = fopen("D:\\Users\\Diogo\\Desktop\\projectkanban\\kanbanv2\\cartoes.txt", "w");

    node_card *curr = list->header;
    if(curr == NULL){
        return;
    }

    while(curr != NULL){
        fprintf(file, "%d;", list->header->c->prioridade);
        fprintf(file, "%d;", list->header->c->id);
        fprintf(file, "%d;", list->header->c->data_criacao);
        fprintf(file, "%s;", list->header->c->descricao);
        fprintf(file, "%d;", list->header->c->estado);
        fprintf(file, "%d;", list->header->c->p->id);
        fprintf(file, "%d;", list->header->c->prazo_conclusao);
        fprintf(file, "%d\n", list->header->c->data_conclusao);

        curr= curr->next;
    }

    fclose(file);
}

奇怪的是当我打印所有这些变量时它确实有效,我不知道它为什么不写入文件(我在这里看到很多帖子,找不到能解决我问题的任何东西)。我的列表结构可能有问题吗?

提前致谢。

2 个答案:

答案 0 :(得分:0)

以下是一些有助于指导解决方案的修订。

首先,必要的包括&你的结构,

#include <stdio.h>
#include <time.h>

#define MAX_STRING (50)

typedef struct {
  int prioridade;
  int id;
  time_t data_criacao;
  char descricao[MAX_STRING];
  int estado;
  void *p; //pessoa *p; //not important to reporoduce
  time_t prazo_conclusao;
  time_t data_conclusao;
} card;

typedef struct node_card{
  card *c;
  struct node_card*next;
} node_card;

typedef struct {
  node_card* header;
  int size;
} list_cards;

以下是一些修订。请注意,您可能需要更改printf格式说明符,或者转换time_t。

//you really should pass in the filename, so you can use the filename
//both in the fopen, and in the error message
//const char* filename = "D:\\Users\\Diogo\\Desktop\\projectkanban\\kanbanv2\\cartoes.txt";
const char* filename = "cartoes.txt"; //write to a local file

int save_file(char* fn, list_cards *list)
{
  FILE *file;
  node_card *curr;
  //check for valid list before opening file
  if( !list || !(curr = list->header) ) {
    printf("empty list pointer passed\n");
    return -1;
  }
  //file open here, and test for NULL return
  if ( ! (file = fopen(fn, "a+")) ) { //change "a+" to "w+" later
    printf("error: failed to open file %s\n",filename); //should be fprintf to stderr, or better to a logging file
    return -2;
  }
printf("called save file on %p\n",list->header); //how many times does this print?
  for( curr=list->header; (curr != NULL); curr=curr->next) {
//change the fprintf's to printf's for testing, delete later
printf("%d;", curr->c->prioridade);
printf("%d;", curr->c->id);
printf("%ld;", (long)curr->c->data_criacao);
printf("%s;", curr->c->descricao);
printf("%d;", curr->c->estado);
printf("%p;", curr->c->p /*->id*/);
printf("%ld;", (long)curr->c->prazo_conclusao);
printf("%ld\n", (long)curr->c->data_conclusao);
    //use curr, not list->header
    fprintf(file, "%d;", curr->c->prioridade);
    fprintf(file, "%d;", curr->c->id);
    fprintf(file, "%d;", curr->c->data_criacao);
    fprintf(file, "%s;", curr->c->descricao);
    fprintf(file, "%d;", curr->c->estado);
    fprintf(file, "%d;", curr->c->p->id);
    fprintf(file, "%d;", curr->c->prazo_conclusao);
    fprintf(file, "%d\n", curr->c->data_conclusao);
  }
  if( file) fclose(file); //defensive programming
  return 0; //always make returns explicit
} //pay the extra $0.02 for the newlines

请注意,你几乎总是可以 eturn有用的信息,例如错误代码。

声明一些最小数据,以使示例有效,

card C = { 1,2,3, "hello", 5, NULL, 7, 8 };
node_card N = { &C, NULL };
list_cards L = { &N, 1 };

并且测试主要,以便我们可以观看它的工作。写入本地文件

int
main(int argc, char* argv[])
{
  card C = { 1,2,3, "hello", 5, NULL, 6, 7 };
  node_card N = { &C, NULL }; //list has one element
  list_cards L = { &N, 1 };
  save_file(filename, &L);
}

将上述内容放入文件中,编译并运行,然后查看大小。由于我使用了“a +”,因此每次运行都会附加。

cat cartoes.txt 
1;2;3;hello;5;0x0;6;7
1;2;3;hello;5;0x0;6;7

答案 1 :(得分:0)

以下情况:

if(curr == NULL){
    return;

在不关闭文件的情况下返回。所以在空文件上有一个打开的文件句柄。此后对该函数的任何调用都将无法打开该文件,因为操作系统可能不允许在同一文件上使用两个不同的写入句柄。

要避免此问题,您可以在打开之前将上述代码移至 文件;或在返回前添加fclose来电。

此外,您应该检查fopen的返回值,因为如果它失败,则fprintf命令会导致未定义的行为。

从技术上讲,您还应检查fclose的返回值,如果失败,可能会中止该程序。