管理缓冲区内的数据

发布于 2024-08-11 07:52:32 字数 1220 浏览 2 评论 0原文

我正在使用此代码将文件读入缓冲区。该文件充满了evaluacion类型的struct(包括一些charint变量)。 现在我将整个文件放在缓冲区中,现在如何找到缓冲区中一个变量的值? 例如 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);
}
{
}
}
}

I'm using this code to read a file into a buffer. The file is full of structs of evaluacion type (including some char and int variables).
Now I have the whole file in a buffer, how can I find the values of one variable in the buffer now?
For example buf.notamedia < 4. There are supposed to be many of them inside the file.

#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)
    {
        /* ... */
    }
}

Any idea is very welcome


I'm doing as you said, but I'm only getting one iteration, I don't know what I'm doing wrong :(

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);
}
{
}
}
}

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

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

发布评论

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

评论(2

胡渣熟男 2024-08-18 07:52:32

最简单的方法是将缓冲区定义为指向数据结构的指针,并使用它来取消引用数据(尽管您应该确保文件大小是结构大小的倍数)。

即,

evaluacion* buffer = (evaluation*)malloc(datos.st_size);
if(buffer[0].notamedia >= 4.5)

您可以增加索引来访问您加载的其他结构。

Easiest to define the buffer as a pointer to the data structure and use that to dereference the data (although you should ensure the file size is a multiple of the structure size).

i.e.

evaluacion* buffer = (evaluation*)malloc(datos.st_size);
if(buffer[0].notamedia >= 4.5)

You can then increment the index to access other structures you loaded.

铁憨憨 2024-08-18 07:52:32

感谢您的评论,我想我解决了问题,我修改了代码:

#include <unistd.h>
#include <sys/stat.h>
int revisanotas(int fd)
{
int nbytes=1;
int nbytese=0;
int i=0;
int n=0;
struct stat datos;
fstat(fd, &datos);
evaluacion buf;
printf("File size =  %d \n", datos.st_size);
evaluacion* buffer=(evaluacion*)malloc(datos.st_size);
int actual = read(fd,buffer,datos.st_size);

do 
{
i++;
if (buffer[i].notamedia >= 4.5 && buffer[i].notamedia < 5)
{
n=n+1;
/*printf("Notamedia = %f\n",buffer[i].notamedia);
*/
buffer[i].notamedia=5;
}

}while (i<(datos.st_size/(sizeof(evaluacion)))); 
nbytese=write(fd,buffer,datos.st_size);
printf("Written bytes = %d\n",nbytese);
return(n);
}

现在,如果条件匹配,我正在修改缓冲区。一旦我读取了所有结构,我就会再次将文件写入磁盘,但是我仍然遇到问题,每次都不是将文件写入相同的位置,而是好像我在旧的文件之后添加了相同的信息,所以如果我读取该文件一次,我得到 3.5Mb,两次 7MB,依此类推:S。
知道我能做什么吗?
谢谢

Thanks for the comments, I think I solved the problem, I modified the code:

#include <unistd.h>
#include <sys/stat.h>
int revisanotas(int fd)
{
int nbytes=1;
int nbytese=0;
int i=0;
int n=0;
struct stat datos;
fstat(fd, &datos);
evaluacion buf;
printf("File size =  %d \n", datos.st_size);
evaluacion* buffer=(evaluacion*)malloc(datos.st_size);
int actual = read(fd,buffer,datos.st_size);

do 
{
i++;
if (buffer[i].notamedia >= 4.5 && buffer[i].notamedia < 5)
{
n=n+1;
/*printf("Notamedia = %f\n",buffer[i].notamedia);
*/
buffer[i].notamedia=5;
}

}while (i<(datos.st_size/(sizeof(evaluacion)))); 
nbytese=write(fd,buffer,datos.st_size);
printf("Written bytes = %d\n",nbytese);
return(n);
}

Now, If the condition is matched, I'm modifying the buffer. Once I read all the structs I write the file in the disk again, but I still have a problem, every time, instead of write the file in the same position, seems like I'm adding the same information after the old one, so if I read the file once I get 3.5Mb, two times 7MB and so on :S.
Any idea what can I do?
Thanks

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