How I can write a EOF?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

How I can write a EOF?

跳至解决方案
2,176 次查看
soli
Contributor I

Hello,

 

I initialize the io-driver for memory with this code. That works well:

 

 

uint8_t memory [4000]; uint32_t mem_init (void){    MQX_FILE_PTR    drive_p        = NULL;    _mqx_uint       error_code      = 0;     if ( (error_code = _io_mem_install(NAME_OF_FILE, memory, sizeof(memory))) == MQX_OUT_OF_MEMORY)                               {                                               printf("could not install drive\n");                                               return 1;                               }     //!____Open_the_drive______________________________________________________    drive_p = _io_fopen( NAME_OF_FILE, "r");    if (drive_p == NULL )    {        printf("unable to open %s\n", NAME_OF_FILE);        return 1;    }   //!___close________________________________________________________________    _io_fclose(drive_p);    return 0;}

 

 

 

After that, I open again with:

pDataFile  = fopen(NAME_OF_FILE, "w+");

 

 

and write some lines with:

fprintf(pDataFile, "%s\n", buffer);

 

 

Until this point everything works well. Now I read out all lines until the function “_io_fgetline()” receive “IO_EOF”. But I get much more (empty) lines as I have written before.

 

Why the driver does not set the EOF automatically?

Can I set explicit the “End Of File”?

 

Thank you for response.

 

0 项奖励
回复
1 解答
1,286 次查看
DavidS
NXP Employee
NXP Employee

Hello,

I think if after the fprintf() call you do a fflush() it will help.

I've seen other post where once you write to file, then fclose(), then re open.

Regards,

David

在原帖中查看解决方案

0 项奖励
回复
4 回复数
1,286 次查看
c0170
Senior Contributor III

Hello nico,

fprintf returns number of bytes written. Check return value of fprintf and afterwards read written characters back from file and compare if numbers are equal.

As david pointed out, fflush also might help to write remaining unwritten data if this situation happened in your case.

If any of these help, get back to us with code snippet what we actually can test.

Regards,

MartinK

0 项奖励
回复
1,287 次查看
DavidS
NXP Employee
NXP Employee

Hello,

I think if after the fprintf() call you do a fflush() it will help.

I've seen other post where once you write to file, then fclose(), then re open.

Regards,

David

0 项奖励
回复
1,286 次查看
soli
Contributor I

Noone can help?

 

0 项奖励
回复
1,286 次查看
Monica
Senior Contributor III

Nico!

Thanks for your patience, and apologizes for the delay.

Out technicians will be shortly addressing your questions!

Regards!

Monica

0 项奖励
回复