Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中重用数组以快速写入大文件_C_Arrays - Fatal编程技术网

如何在C中重用数组以快速写入大文件

如何在C中重用数组以快速写入大文件,c,arrays,C,Arrays,我正在尝试快速写入一个大文件并使用数组。所以我必须多次使用相同的数组。我的部分计划如下 char buff[1024]; char *x= buff; fd = open("file.txt",O_CREAT|O_RDWR, S_IRWXU) ; void function(){ char temp[128]; sprintf(temp,"%s,%s,%d\n",src,dst,payload) ; x=myS

我正在尝试快速写入一个大文件并使用数组。所以我必须多次使用相同的数组。我的部分计划如下

    char buff[1024];
    char *x= buff;
    fd = open("file.txt",O_CREAT|O_RDWR, S_IRWXU) ;


    void function(){

        char temp[128];
        sprintf(temp,"%s,%s,%d\n",src,dst,payload) ;
        x=myStrCat(x,temp); // copy files from temp to buff

        //This is my question. if the buffer "buff" is full, how to  truncate the buffer for next loop and that too fast.
        if (strlen(buff) >= 1024){
            write(fd,buff, len);
        }
    }

    char * myStrCat(char * dest, char * src){
             while(*dest) dest++;
             while( *dest++ = *src++);
             return --dest;

     }

    int main(){
        //other codes
        while (true){        
            function();
            //conditions to break the loop untill then function() will be continuously in loop.
        }
        return 0;
    }
提前谢谢

strlen(buf)
永远不能>=1024,因为您只为它分配1024字节。C字符串要求结尾为NULL,因此会出现缓冲区溢出,从而导致未定义的行为。不过,您可能有1023+NULL

您的代码也不会检查
myStrCat
中是否存在缓冲区溢出。它将导致未定义的行为。如果您已经有1020个字符,并且想再添加10个字符,该怎么办

这样做的方法是保留一个数字,指示缓冲区中已有多少个字符。如果缓冲区无法容纳下一个字符串,请将数据写入文件并将字符计数归零。如果可以,从字符计数指示的位置开始将字符串复制到缓冲区,并获取下一个字符串

当然,最后将缓冲区中的内容写入文件

这样您就不会超过缓冲区限制


(你确定这比让操作系统处理写缓存快得多吗?

也许你正在寻找这样的东西:

    #define BUFFER_SIZE 1024

char buff[BUFFER_SIZE];
uint16_t buff_len = 0;

void function(){

    char temp[128];
    int len = sprintf(temp,"%s,%s,%d\n",src,dst,payload) ;

    if ((len+buff_len) > BUFFER_SIZE-1)
    {
        write(fd,buff, buff_len);

        buff_len = 0;
    }


    strcpy(&buff[buff_len],temp); // copy files from temp to buff

    buff_len += len;
}

你能说得更具体些吗?您的代码中是否有不起作用的特定内容?
如果(strlen(buff)>=1024)
是肯定的。我正在尝试使用libpcap工具捕获网络流量。因此,我需要将捕获的数据包写入一个文件。因此,在下面的函数“temp”中,我可以一次得到一个数据包的值。sprintf(临时、%s、%s、%d\n)、src、dst、有效负载);由于我的缓冲区(buff[1024])的大小是有限的,所以我的buff[1024]将不断被填充。一旦它满了,我想把buff[1024]的内容写入一个文件。然后我想再次使用buff[1024]来捕获更多的数据包。你想写“veryfast”还是写得很快?@LPs
buff
x
似乎是全局变量(至少看起来是这样,看看
x
函数()中是如何使用的),所以填充了0,而不是UB。谢谢你的输入,你能补充一下吗?我该怎么做才能在循环中一次又一次地重复使用相同的buff。我还没有完全比较过,但我试过一次,速度很快。NUL(
'\0'
)和NULL(
((void*)0)
)不是一回事,后者是一个字符串。@mctylr实际上每个文档都在谈论以NULL结尾的字符串和NULL字符,即使字符实际上是NUL。但还是有道理。谢谢你的帮助。我测试了它,它对我来说很好。