我正在使用此代碼將文件讀入緩衝區。該文件完整爲evaluacion
類型的structs
(包括一些char
和int
變量)。 現在我將整個文件放在一個緩衝區中,現在如何在緩衝區中找到一個變量的值? 例如buf.notamedia < 4
。文件中應該有很多。管理緩衝區內的數據
#include <unistd.h>
#include <sys/stat.h>
int revisanotas(int fd)
{
int nbytes = 1;
int nbytese = 0;
evaluacion buf;
struct stat datos;
fstat(fd, &datos);
printf("Size of file = %d \n", datos.st_size);
char *buffer = (char *)malloc(datos.st_size);
int actual = read(fd, buffer, datos.st_size);
printf("actual = %d\n", actual);
if (buf.notamedia >= 4.5 && buf.notamedia < 5)
{
/* ... */
}
}
任何想法是非常可喜的
正如你所說的我做,但我只得到一個迭代,我不知道我做錯了什麼:(
evaluacion* buffer=(evaluacion*)malloc(datos.st_size);
int actual = read(fd,buffer,datos.st_size);
printf("Number of structs = %d", (datos.st_size/(sizeof(evaluacion))));
for (i=0;i<(datos.st_size/(sizeof(evaluacion)));i++);
{
printf("Notamedia = %f\n",buffer[i].notamedia);
if (buffer[i].notamedia >= 4.5 && buffer[i].notamedia < 5)
{
printf("Notamedia = %f\n",buffer[i].notamedia);
}
{
}
}
}
不要忘了把你的閱讀循環讀取整個文件。 此外,它可能更容易只是對mmap()文件。 – Suppressingfire 2009-11-14 18:30:28
@Suppressingfire:無論如何,他執行單個讀取來獲取整個文件,就像執行fstat()函數時一樣。 – 2009-11-14 18:47:34