逐行读取数组中的文件

发布于 2024-09-09 08:19:07 字数 390 浏览 8 评论 0原文

您可以将数组的任何索引设置为起始索引,即从文件中读取的位置吗?我担心缓冲区是否会在此过程中损坏。

#include <stdio.h>

int main()
{
    FILE *f = fopen("C:\\dummy.txt", "rt");

    char lines[30]; //large enough array depending on file size

    fpos_t index = 0;

    while(fgets(&lines[index], 10, f)) //line limit is 10 characters
    {
        fgetpos (f, &index );
    }

    fclose(f);
}

Can you set any index of array as starting index i.e where to read from file? I was afraid if the buffer might get corrupted in the process.

#include <stdio.h>

int main()
{
    FILE *f = fopen("C:\\dummy.txt", "rt");

    char lines[30]; //large enough array depending on file size

    fpos_t index = 0;

    while(fgets(&lines[index], 10, f)) //line limit is 10 characters
    {
        fgetpos (f, &index );
    }

    fclose(f);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁我热情 2024-09-16 08:19:07

你可以,但由于你的代码试图读取文件的完整内容,你可以使用 fread 更直接地做到这一点:

char lines[30];

// Will read as much of the file as can fit into lines:
fread(lines, sizeof(*lines), sizeof(lines) / sizeof(*lines), f);

也就是说,如果你真的想逐行读取并安全地执行它,你应该更改你的 fgets线路至:

// As long as index < sizeof(lines), guaranteed not to overflow buffer
fgets(&lines[index], sizeof(lines) - index, f);

You can, but since your code is trying to read the full contents of the file, you can do that much more directly with fread:

char lines[30];

// Will read as much of the file as can fit into lines:
fread(lines, sizeof(*lines), sizeof(lines) / sizeof(*lines), f);

That said, if you really wanted to read line by line and do it safely, you should change your fgets line to:

// As long as index < sizeof(lines), guaranteed not to overflow buffer
fgets(&lines[index], sizeof(lines) - index, f);
怪我闹别瞎闹 2024-09-16 08:19:07

不是这样的。有一个名为 fseek 的函数可以将您带到文件中的不同位置。

您的代码会将文件读入缓冲区的不同部分(而不是读取文件的不同部分)。

Not like this no. There is a function called fseek that will take you to a different location in the file.

Your code will read the file into a different part of the buffer (rather than reading a different part of the file).

很快妥协 2024-09-16 08:19:07

lines[index] 是数组 lines 的第索引字符。它的地址不是索引行。

如果您想跳到特定行,例如第 5 行,那么为了读取第 5 行,读取 4 行并对它们不执行任何操作,它们会读取下一行并对其执行某些操作。

如果您需要跳到文件中的特定字节,那么您需要使用 fseek()。

另外:请注意,您告诉 fgets 为您读取的字节数 (10) 与您将行放入的数组大小 (30) 相同 - 所以现在情况并非如此。

如果您需要从该行中的某个字符开始读取该行的一部分,您仍然需要读取整行,然后选择从开头以外的某个位置开始使用它的一部分。

这两个例子就像从网站或图书馆请求文档的一部分 - 他们不会为你撕下一页,你得到整个文档,你必须翻到你想要的内容。

lines[index] is the index'th character of the array lines. Its address is not the index'th line.

If you want to skip to a particular line, say 5, then in order to read the 5th line, read 4 lines and do nothing with them, them read the next line and do something with it.

If you need to skip to a particular BYTE within a file, then what you want to use is fseek().

Also: be careful that the number of bytes that you tell fgets to read for you (10) is the same as the size of the array you are putting the line into (30) - so this is not the case right now.

If you need to read a part of a line starting from a certain character within that line, you still need to read the whole line, then just choose to use a chunk of it starting someplace other than the beginning.

Both of these examples are like requesting a part of a document from a website or a library - they're not going to tear out a page for you, you get the whole document, and you have to flip to what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文