理解 C 中的 fseek()

问题描述 投票:0回答:1

我正在学习 C 中的文件 I/O,并且有兴趣使用它通过

fwrite()
fread()
函数读取和写入文件结构,现在在我的代码成功运行后,我想知道我是否可以从中读取特定结构一组结构并将其放入某个给定的结构中。


这是我的尝试

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

typedef struct tools {
    int recordno;
    char toolname[50];
    int quantity;
    float cost;
} tools;

void recordprinter(tools a) {
    printf("%d %s %d %f\n", a.recordno, a.toolname, a.quantity, a.cost);
}

int main() {
    FILE * fp;
    
    fp = fopen("file.txt", "rb+");
    tools * a = (tools * ) malloc(100 * sizeof(tools));
    for (int i = 0; i < 100; i++) {
        a[i].cost = 0;
        a[i].toolname[0] = 'a';
        a[i].toolname[1] = '\0';
        a[i].quantity = 0;
        a[i].recordno = i + 1;
    }
    for (int i = 0; i < 100; i++) {
        fwrite(a + i, sizeof(tools), 1, fp);
        fseek(fp, sizeof(tools), SEEK_CUR);
        // I used fseek here just because fwrite doesnot move the cursor when\
        it writes something to the file.(and fwrite(a + i, sizeof(tools), 100, fp) gives weird gliches)
    }

    fseek(fp, 0, SEEK_SET); // to bring cursor back to start of the file.

    fread(a, sizeof(tools), 1, fp);

    fseek(fp, sizeof(tools) * 50, SEEK_SET); // now I expect the cursor to be at 51th structure.

    fread(a + 3, sizeof(tools), 1, fp); // I am now writing the 51th structure in a[3]    

    recordprinter(a[3]);
    // this gives output 26 and not 51   

    return 0;
}

现在,当我运行程序时,我期望

51 a 0 0.00000
作为输出, 但令我惊讶的是,它选择了第 26 个结构并将其放入 [3]
任何帮助都会得到帮助!

c file fwrite fread fseek
1个回答
0
投票

尝试将

fopen
更改为使用
w+
而不是
rb+

另外,在创建文件时去掉

fseek
,如前所述,
fwrite
写入数据后肯定会增加文件偏移量(前提是
fwrite
确实写入数据)。

这是使用下面修改后的代码观察到的输出。

gcc main.c
./a.out
51 a 0 0.000000
// main.c

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

typedef struct tools {
    int recordno;
    char toolname[50];
    int quantity;
    float cost;
} tools;

void recordprinter(tools a) {
    printf("%d %s %d %f\n", a.recordno, a.toolname, a.quantity, a.cost);
}

int main() {
    FILE * fp;
    
    fp = fopen("file.txt", "w+");
if (fp==NULL) return 1;
    tools * a = (tools * ) malloc(100 * sizeof(tools));
    for (int i = 0; i < 100; i++) {
        a[i].cost = 0;
        a[i].toolname[0] = 'a';
        a[i].toolname[1] = '\0';
        a[i].quantity = 0;
        a[i].recordno = i + 1;
    }
    for (int i = 0; i < 100; i++) {
        fwrite(a + i, sizeof(tools), 1, fp);
        //fseek(fp, sizeof(tools), SEEK_CUR);
        // I used fseek here just because fwrite doesnot move the cursor when
        // it writes something to the file.(and fwrite(a + i, sizeof(tools), 100, fp) gives weird gliches)
    }

    fseek(fp, 0, SEEK_SET); // to bring cursor back to start of the file.

    fread(a, sizeof(tools), 1, fp);

    fseek(fp, sizeof(tools) * 50, SEEK_SET); // now I expect the cursor to be at 51th structure.

    fread(a + 3, sizeof(tools), 1, fp); // I am now writing the 51th structure in a[3]    

    recordprinter(a[3]);
    // this gives output 26 and not 51   

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.