Я сохранил данные в файле в формате json. Когда я пытаюсь прочитать строку за строкой и обработать ее в буфере, а затем проанализировать ее, она работает для некоторой строки. Но когда fgets () читает строку другой длины, тогда не разбирает.
Хорошо работает для нескольких строк. Проблема возникает при изменении длины строки
Вы можете видеть, что в ключе (строке данных) нет данных
Выход:
{"line" : 0 ,"datastring" : "Jan 1 2017 00:00:00 75 822 96 548 85 76 82 93 78 82 64 89 899"}
{"line" : 0 ,"datastring" : "Jan 1 2017 00:00:00 75 822 96 548 85 76 82 93 78 82 64 89 899"}
{"line" : 0 ,"datastring" : "Jan 1 2017 00:00:00 75 822 96 548 85 76 82 93 78 82 64 89 899"}
{"line" : 0 ,"datastring" : "Jan 1 2017 00:00:00 75 822"}
{"line" : 0 ,"datastring" : ""}
{"line" : 0 ,"datastring" : ""}
{"line" : 0 ,"datastring" : ""}
{"line" : 0 ,"datastring" : ""}
00:00 75 822 96 548 85 76 82 93 78 82 64 89 899"}
{"line" : 7 ,"datastring" : "Jan 1 2017 00:00:00 75 822"}
Код:
#ifndef CPY_BUFF_SIZE
#define CPY_BUFF_SIZE 200
#endif
/* String conversion macro */
#define STR_(n) #n
#define STR(n) STR_(n)
/* Drive number used for FatFs */
#define DRIVE_NUM 0
const char inputfile[] = "fat:"STR(DRIVE_NUM)":input1.txt";
const char outputfile[] = "fat:"STR(DRIVE_NUM)":output.txt";
const char copyfile[] = "fat:"STR(DRIVE_NUM)":tempp.txt";
char conbuff[100];
/* File name prefix for this filesystem for use with TI C RTS */
char fatfsPrefix[] = "fat";
char cpy_buff[CPY_BUFF_SIZE];
char mybuff[CPY_BUFF_SIZE];
char pub_buff[100];
int i=0,x,a,v,num_line=0;
uint16_t ret;
int filesize=0,fsize=0;
int buffsize,pos;
char text1[] = "Jan 1 2017 00:00:00 75 822 96 548 85 76 82 93 78 82 64 89 899";
char text2[] = "Jan 1 2017 00:00:00 75 822";
/*
* ======== mainThread ========
* Thread to perform a file copy
*
* Thread tries to open an existing file inputfile[]. If the file doesn't
* exist, create one and write some known content into it.
* The contents of the inputfile[] are then copied to an output file
* outputfile[]. Once completed, the contents of the output file are
* printed onto the system console (stdout).
*/
void *mainThread(void *arg0)
{
SDFatFS_Handle sdfatfsHandle;
/* Variables for the CIO functions */
FILE *new;
Json_Handle datalog;
char *info = "{""\"line\":int32,""\"datastring\":string}";
Json_createTemplate(&datalog, info,strlen(info));
Json_Handle object;
Json_createObject(&object, datalog, 1024);
char *key1 = "\"line\"";
char *key2 = "\"datastring\"";
uint32_t value1 ;
uint32_t o_value1 ;
char value2[100];
char o_value2[100]; // output string
char newbuff[100];
uint32_t s_value=0 ;
uint16_t value1_size = sizeof(value1);
uint16_t value2_size = sizeof(value2);
uint16_t builtBuffSize =sizeof(cpy_buff);
/* Call driver init functions */
SDFatFS_init();
/* add_device() should be called once and is used for all media types */
add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);
/* Mount and register the SD Card */
sdfatfsHandle = SDFatFS_open(Board_SDFatFS0, DRIVE_NUM);
if (sdfatfsHandle == NULL) {
puts("Error starting the SD card\n");
while (1);
}
else {
puts( "Drive %u is mounted\n");
}
new=fopen(copyfile,"r+");
fseek(new,0, SEEK_END);
fsize=ftell(new);
sprintf(conbuff,"size of file when opened in read and write mode=%d",fsize);
puts(conbuff);
rewind(new);
if (!new) {
strcpy(conbuff,"temp not opened");
puts(conbuff);
}
for(v=0;v<8;v++)
{
memset(o_value2, 0, sizeof(o_value2));
for(i=1;i<3;i++)
{
fgets(cpy_buff,sizeof(cpy_buff),new);
strcat(mybuff,cpy_buff);
memset(cpy_buff, 0, sizeof(cpy_buff));
}
x=strlen(mybuff);
sprintf(conbuff,"len of my buff %d",x);
puts(conbuff);
sprintf(conbuff,"pointer loc=%d",ftell(new));
Json_parse(object, mybuff, strlen(mybuff));
sprintf(conbuff,"pos of new after reading buff =%d",ftell(new));
puts(conbuff);
Json_getValue(object, key1, &o_value1, &value1_size);
Json_getValue(object, key2, &o_value2, &value2_size);
memset(pub_buff, 0, sizeof(pub_buff));
strcpy(pub_buff,o_value2);
puts(pub_buff);
puts("dsf");
Json_setValue(object,key1,&s_value,value1_size);
Json_setValue(object,key2,&o_value2,value2_size);
Json_build(object,newbuff,&builtBuffSize);
puts(newbuff);
sprintf(conbuff,"pos of new before writing into file =%d",ftell(new));
puts(conbuff); //3
fseek(new, -x, SEEK_CUR);
sprintf(conbuff,"pos of new after setting to -x =%d",ftell(new));
puts(conbuff);
fwrite(newbuff,1,strlen(newbuff),new);
fputs("\r\n",new);
sprintf(conbuff,"pos of new after writing into file =%d",ftell(new));
puts(conbuff);
fseek(new,0, SEEK_CUR);
sprintf(conbuff,"pos of new after setting to 0 =%d",ftell(new));
puts(conbuff);
memset(mybuff, 0, sizeof(mybuff));
}
fseek(new,-1, SEEK_CUR);
fclose(new);
/* Close both inputfile[] and outputfile[] */
strcpy(conbuff,"akjsdabsdas");
puts(conbuff);
/* Stopping the SDCard */
SDFatFS_close(sdfatfsHandle);
return (NULL);
}