如何正确使用 fstat() 函数以及它的限制是什么?

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

所以我对 C 很陌生。我用 Java 做过很多编程,但发现学习 C 非常困难。

目前我被分配从我们的终端窗口读取一个文件,其中将包含一个整数列表。我们必须从这个列表中读取值并计算平均值,我相信我做对了。

我唯一的问题是我不明白如何正确使用 fstat(),我阅读了手册页但仍然很难理解。在我下面的代码中,我想使用 fstat() 来查找正在读取的文件的大小,这样我就可以为我的数组分配正确的空间量,我在其中存储从输入文件读取的值。我只需要知道 fstat() 的正确用法和语法,我相信我可以从那里取得重大进展。提前致谢!

    char *infile;
    int fileSize;
    int fd;
    int i;
    int j;
    int temp;
    int sum = 0;
    int average;
    /* enforce proper number of arguments
    */
    if (argc != 1)
    {
            fprintf(stderr, "Too many arguments.\n");
            exit(1);
    }

    infile = argv[1];

    //open file
    fd = open(infile, O_RDONLY);

    //exit if open fails
    assert (fd > -1);

    //find size of file
    fileSize = fstat(fd, blksize_t st_blksize); 

    //fine perfect size for array
    temp =  malloc(temp * sizeof(int));

    //create file of perfect size
    int intArray[temp];

    //scan integers into our array
    for (i = 0; i < fileSize; i++)
    {
            fscanf(infile, "%d", intArray[i]);
    }
    fclose(fd);

    //add all integers into the array up
    for (j = 0; j < fileSize; j++);
     {
            sum = sum + intArray[j];
     }

    //calculat average
    average = (sum)/fileSize;

    printf("Number of numbers averaged: %d\n Average of numbers: %d\n", fileSize, average);

    if ( close(fd) == -1 )
     {
             fprintf(stderr, "error closing file -- quitting");
             exit(1);
     }

return 0;

}

c arrays malloc
3个回答
4
投票

库函数

fstat()
不返回文件大小,成功则返回0。它通过填充作为参数传递的结构来通知文件大小。

if (fstat( fd, &buf))
    printf("Bad call\n");
else
    printf("File size : %ld\n", buf.st_size);

但是正如@chux(已删除的帖子)回答的那样,它以字节为单位告诉您文件大小,而不是整数。函数

fscanf()
输入来自text的数据,因此文件大小和字段数之间没有直接关联。

很遗憾,在回答您的标题问题时,使用

fstat()
确定文件大小对您没有实际用处。您的第二个隐含问题是如何为数组分配足够的内存。我发布了一个答案,在不同的上下文中,数组大小一开始是未知的。 C 读取由空格分隔的文本文件,字长无限制

但在这里我使用了一种更简单的技术——解析文件两次以找出它包含多少文本整数。然后它倒带文件并为数组分配内存,尽管在这个例子中,数组不是计算值的总和和平均值所必需的,而且双重文件解析也不是必需的,除非你打算这样做更多的价值观。

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

void fatal(char *msg) {
    printf("%s\n", msg);
    exit (1);
    }

int main(int argc, char *argv[])
{
    FILE *fil;
    int *array;
    int items = 0;
    int sum = 0;
    int avg;
    int value;
    int i;

    if (argc < 2)                                       // check args
        fatal ("No file name supplied");
    if ((fil = fopen (argv[1], "rt")) == NULL)          // open file
        fatal ("Cannot open file");
    while (fscanf(fil, "%d", &value) == 1)              // count ints
        items++;
    printf ("Found %d items\n", items);
    if (items == 0)
        fatal ("No integers found");
    if ((array = malloc(items * sizeof (int))) == NULL) // allocate array
        fatal ("Cannot allocate memory");

    if (fseek (fil, 0, SEEK_SET))                       // rewind file
        fatal ("Cannot rewind file");
    for (i=0; i<items; i++) {
        if (fscanf(fil, "%d", &value) != 1)             // check int read
            fatal ("Cannot read integer");
        array[i] = value;
        sum += value;
    }
    fclose(fil);
    printf ("Sum = %d\n", sum);
    printf ("Avg = %d\n", (sum+items/2) / items);       // allow rounding
    free(array);
    return 0;
}

输入文件:

1 2   3
4 5
6
   -1   -2

程序输出:

Found 8 items
Sum = 18
Avg = 2

3
投票

您声称已阅读

fstat()
的联机帮助页,这似乎与:
fileSize = fstat(fd, blksize_t st_blksize);

您需要在函数范围内声明一个
struct stat
,并将指向它的指针传递给
fstat()

struct stat finfo;
fstat(fd, &finfo);

然后你可以从

struct stat
读取文件大小:

off_t filesize = finfo.st_size;

我还建议使用

size_t
而不是
int
来处理与对象大小有关的所有事情。


0
投票

对于文件指针,需要这样的东西。

FILE *fp = fopen("fileName.png", "rb");

// get file size from file info
struct stat finfo;
if (fstat(fileno(fp), &finfo)) printf("fail");
else off_t pngDataLength = finfo.st_size;
© www.soinside.com 2019 - 2024. All rights reserved.