fseek()将指针设置为错误的位置

时间:2015-11-01 01:25:31

标签: c++ fgets fseek ftell

我有一个.dat文件,其结构如下:

Object name | Area | Length |Form Factor

我正在尝试查找哪个对象具有最小区域。我读取整个文件并用最小区域跟踪对象,并用ftell()跟踪它的位置。当我读完文件后,我将流位置指示器设置为从ftell()获得的任何内容。然后我读到那条线作为我的最终结果,但是现在,我正在读错线。

objects.dat:

toy1 12785.00 550.70 23.22
toy2 11506.50 495.63 0.48
Pen 35008.65 1450.98 0.17
Pencil 42788.35 1773.42 0.209
card7 128433.00 1552.67 0.67

代码:

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

using namespace std;

int main()
{
    FILE *cfPtr;
    errno_t err;

    char name[15];
    double area, length, fF;
    double minArea = 100000.0;
    long address, minAreaAdd;

    char string[100];

    if ((err = fopen_s(&cfPtr, "objects.dat", "r")) != 0)   // Check if we can reach the file
        printf("The file 'objecs.dat' was not opened to read.\n");
    else
    {
        while (fgets(string, 100, cfPtr)) 
        {
            address = ftell(cfPtr);
            sscanf_s(string,"%s %lf %lf %lf", &name, sizeof name, &area, &length, &fF);

            if (area < minArea)
            {
                minArea = area;
                minAreaAdd = address;
            }
        }

        cout << "Min Area: " << minArea << " Address: " << minAreaAdd << "\n\n";

        fseek(cfPtr, minAreaAdd, SEEK_SET);
        fgets(string, 100, cfPtr);
        sscanf_s(string, "%s %lf %lf %lf", &name, sizeof name, &area, &length, &fF);

        cout << "Name: " << name << " Area : " << area << " Length: " << length << " Form factor: " << fF << "\n";

        fclose(cfPtr);
    }
    return 0;
}

输出:

Min Area: 11506.5 Address: 55

Name: Pen Area : 35008.7 Length: 1450.98 Form factor: 0.17

2 个答案:

答案 0 :(得分:3)

当您获得文件位置并将其存储到address后,您已经阅读了该行,所以当您寻求获得答案时,您将会这样做在正确的之后获取行。

答案 1 :(得分:1)

正如&#34; 1201ProgramAlarm&#34;所提到的,在读完要保存的行后,您将获得该地址。也就是说,您在ftell之后使用fgets

fgets在读取数据时移动文件位置指示器时会发生什么。这是因为fgets需要为下一次读取操作准备文件,否则您将被卡在同一位置,读取相同的数据。

你需要扭转这种逻辑。这是一个例子:

address = -1
do
{
    if (address >= 0) {
        sscanf_s(string,"%s %lf %lf %lf", &name, sizeof name, &area, &length, &fF);

        if (area < minArea)
        {
            minArea = area;
            minAreaAdd = address;
        }
    }
    address = ftell(cfPtr);
} while (ret = fgets(string, 100, cfPtr));