从文件问题中读取

时间:2016-01-06 11:18:32

标签: c file-io graph

我正在尝试从文件中读取图形,我在节点的第一行找到了,我创建了一个代码,用于读取向量中的节点,直到行结束,然后在第二行,就像我得到了弧形,(%d%d)格式。我的问题是,当我尝试读取节点时,它会跳过第一个数字,第三个,依此类推。我无法弄清楚原因。 这是我的代码:

void readGraph() {
    int i=0, j, node1, node2, n,check1, check2,c;
    char nodes[MAXN];
    FILE *f;

    f = fopen("data.in","r");

    if (f == NULL) {
        printf("Error in opening file!");
    }
    else {

       while ((c = fgetc(f)) != EOF) {

            if ( c != ' ') {
                fscanf(f, "%d", &(nodes[i]));
                printf("%d, %d\n",i,nodes[i]);
                i++;
            }

            if (c == '\n')
                break;

        }
    }

    while (fscanf(f,"(%d %d)", &node1, &node2)) {

        if ( c != ' ')
            fscanf(f, "(%d %d) ", &node1, &node2);

        if (c == '\n')
            break;

   }
   fclose(f);
}

另外,我的data.in:

1 2 3 4 5 6
(1 3) (2 3) (3 4) (3 5) (3 6) (4 6) (5 6)

任何帮助将不胜感激。谢谢!

4 个答案:

答案 0 :(得分:0)

我猜,fgetc逐字节读取,而fscanf("%d")一次读取4个字节,所以这可能会导致一些问题。

答案 1 :(得分:0)

   while ((c = fgetc(f)) != EOF) {

        if ( c != ' ') {
            fscanf(f, "%d", &(nodes[i]));
            printf("%d, %d\n",i,nodes[i]);
            i++;
        }

        if (c == '\n')
            break;

    }

这里你从f读了两次(fgetc(f)被丢弃)

对于弧线(下面的代码),这也是一个问题,你测试c但是你不再读它了

    while (fscanf(f,"(%d %d)", &node1, &node2)) {

        if ( c != ' ')
            fscanf(f, "(%d %d) ", &node1, &node2);

        if (c == '\n')
            break;

   }

答案 2 :(得分:0)

void readGraph(void){
    int i = 0, j, node1, node2, n, check1, check2, c;
    int nodes[MAXN];
    FILE *f;

    f = fopen("data.in","r");

    if (f == NULL) {
        printf("Error in opening file!");
        return ;
    }

    while ((c = fgetc(f)) != EOF) {
        if( c == ' ')
            continue;
        else if( c == '\n')
            break;
        else {
            ungetc(c, f);//It is too reading. So Return the character to the stream.
            fscanf(f, "%d", &nodes[i]);
            printf("%d, %d\n", i, nodes[i]);
            i++;
        }
    }

    while (2 == fscanf(f,"(%d %d) ", &node1, &node2)) {
        printf("(%d, %d)\n", node1, node2);
    }
    fclose(f);
}

答案 3 :(得分:0)

尝试此解决方案,您可以看到我的c评论:

void readGraph() {
    int i=0, j, node1, node2, n,check1, check2,c;
    char nodes[128];
    FILE *f;

    f = fopen("data.in","r");

    if (f == NULL) {
        printf("Error in opening file!");
    }
    else {

       while (((c = fgetc(f)) != EOF) && (c != '\n')) {

            if ( c != ' ') {
                nodes[i] = c;
                /*fscanf(f, "%d", &(nodes[i]));*/ // you loose read c value when you read again without using value
                printf("%d, %c\n",i,nodes[i]);
                i++;
            }

            /*if (c == '\n')
                break;*/ 
            /* You loop 1 extra time */

        }
    }

    while (fscanf(f,"(%d %d)", &node1, &node2)) {

        if ( c != ' ')
            fscanf(f, "(%d %d) ", &node1, &node2);

        if (c == '\n')
            break;

   }
   fclose(f);
}