gpt4 book ai didi

Reading a file using fgetc() but characters are skipped(使用fgetc()读取文件,但跳过字符)

转载 作者:bug小助手 更新时间:2023-10-25 19:11:15 28 4
gpt4 key购买 nike



Reading a file using fgetc() but characters are skipped. Only when I use fseek() to to move the cursor one step back then it works fine. My issue is I cannot for the life of me wrap my head around it why do I need to use fseek() to solve the problem. I need fresh eyes to explain since my brain is in a lock state.

使用fgetc()读取文件,但跳过字符。只有当我使用fSeek()将光标向后移动一步时,它才能正常工作。我的问题是,我无论如何都不能理解它,为什么我需要使用fSeek()来解决这个问题。我需要新鲜的眼睛来解释,因为我的大脑处于锁定状态。


Code below skips a character

下面的代码跳过一个字符


int file_chars = 0;
size_t hdr_size = 15;
char hdr_cook_val[hdr_size];
esp_err_t get_cookie = httpd_req_get_cookie_val(req,COOKIE_NAME,hdr_cook_val,&hdr_size);
if(get_cookie == ESP_ERR_NOT_FOUND && req->uri[0] == '/' && req->method == HTTP_GET){
FILE *index_file = fopen(HTML_PART_FOLDER"/index.txt","r");
char *hdr_val = malloc(sizeof(char)*MALLOC_DEFAULT_SIZE);
if(hdr_val==NULL){ESP_LOGE(TAG,"Malloc failed variable name: hdr_val");return ESP_OK;}
for(int i=0;;i++){
file_chars = fgetc(index_file);
if(file_chars==EOF){
httpd_resp_send_chunk(req,hdr_val,i);
httpd_resp_send_chunk(req,NULL,0);
break;
}
hdr_val[i] = file_chars;
if(i==9999){
httpd_resp_send_chunk(req,hdr_val,i);
i=-1;
vTaskDelay(pdMS_TO_TICKS(20));
}
}
free(hdr_val);
fclose(index_file);
return ESP_OK;
}

using the same code but only adding the line fseek(index_file,ftell(index_file)-1,SEEK_SET); all is good

使用相同的代码,但只添加行fSeek(index_file,ftell(Index_File)-1,ek_set);一切都很好


int file_chars = 0;
size_t hdr_size = 15;
char hdr_cook_val[hdr_size];
esp_err_t get_cookie = httpd_req_get_cookie_val(req,COOKIE_NAME,hdr_cook_val,&hdr_size);
if(get_cookie == ESP_ERR_NOT_FOUND && req->uri[0] == '/' && req->method == HTTP_GET){
FILE *index_file = fopen(HTML_PART_FOLDER"/index.txt","r");
char *hdr_val = malloc(sizeof(char)*MALLOC_DEFAULT_SIZE);
if(hdr_val==NULL){ESP_LOGE(TAG,"Malloc failed variable name: hdr_val");return ESP_OK;}
for(int i=0;;i++){
file_chars = fgetc(index_file);
if(file_chars==EOF){
httpd_resp_send_chunk(req,hdr_val,i);
httpd_resp_send_chunk(req,NULL,0);
break;
}
hdr_val[i] = file_chars;
if(i==9999){
httpd_resp_send_chunk(req,hdr_val,i);
fseek(index_file,ftell(index_file)-1,SEEK_SET);
i=-1;
vTaskDelay(pdMS_TO_TICKS(20));
}
}
free(hdr_val);
fclose(index_file);
return ESP_OK;
}

更多回答

Did you consider compiling your application with GCC -it has cross-compilers- invoked as gcc -Wall -Wextra -g then using the GDB debugger? (GDB can do remote debugging)

你有没有考虑过用GCC编译你的应用程序-它有交叉编译器-作为gcc -Wall -Wextra -g调用,然后使用GDB调试器?(GDB可以进行远程调试)

BTW, unrelated: sizeof (char) is 1 by definition. You don't need to multiply by 1

顺便说一句,无关:sizeof(Char)定义为1。你不需要乘以1

优秀答案推荐

The code loads until the 10,000th character, then only sends 9999 of them.

该代码加载到第10,000个字符,然后仅发送9999个字符。


Try this:

试试这个:


     int i = 0;
for( ;; ) {
file_chars = fgetc(index_file);
if( file_chars == EOF ) {
httpd_resp_send_chunk(req,hdr_val,i);
httpd_resp_send_chunk(req,NULL,0);
break;
}
hdr_val[ i ] = file_chars;
// count that character and test for complete batch
if( ++i == 10000 ) {
httpd_resp_send_chunk( req, hdr_val, i ); // Send 10000 per batch
i = 0;
vTaskDelay(pdMS_TO_TICKS(20));
}
}

And, learn to use the spacebar... Dense code is both difficult to read and provides shelter for bugs.

还有,学会使用空格键。密集的代码既难于阅读,又为错误提供了庇护所。


EDIT:

On reflection, if the source file is an exact multiple of 10000, the code above will send two empty 'chunks'. This is not good! The following is more compact while fixing that problem. PLUS, 10000 is a magic number that should be identified somehow.

编辑:在反射时,如果源文件正好是10000的倍数,则上面的代码将发送两个空的“块”。这真是不太好!在解决该问题时,以下内容更加紧凑。此外,10000是一个神奇的数字,应该以某种方式识别出来。


#define BYTES_PER_OUT_CHUNK  10000 // why 10000? Why not...

int i = 0;
while( ( file_chars = fgetc( index_file ) ) != EOF ) {
hdr_val[ i++ ] = file_chars;
if( i == BYTES_PER_OUT_CHUNK ) {
httpd_resp_send_chunk( req, hdr_val, i );
i = 0;
vTaskDelay( pdMS_TO_TICKS( 20 ) );
}
}
if( i )
httpd_resp_send_chunk( req, hdr_val, i );
httpd_resp_send_chunk( req, NULL, 0 );

更多回答

reading the file in chunks of 10000 (using malloc) to send a web page to the user since chunk encoding is supported by the http server. Your solution is more compact, efficient and works great thank you.

读取以10000为单位的块的文件(使用MARLOC)以向用户发送网页,因为HTTP服务器支持块编码。您的解决方案更紧凑、更高效,而且效果很好,谢谢。

Glad to be of service! :-) Cheers!

很高兴为您服务!:-)干杯!

That last chunk of data sent might be 9990 bytes... I don't know... Should there be a delay before the "null" chunk is pushed at the output channel???

发送的最后一块数据可能是9990字节...我不知道..。在将“空”块推送到输出通道之前是否应该有延迟?

i dont think its necessary the delay is there only for the freertos task not to trigger the watchdog and hold a task for too long.

我认为延迟是没有必要的,只是为了让自由实时操作系统的任务不会触发看门狗并保持任务太长时间。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com