fgets无法读取文本文件中以json格式存储的数据

时间:2019-06-23 07:35:10

标签: c json

我正在尝试从文本文件读取数据。数据以JSON格式存储。 我在ccs中使用了JSON库。 这是链接

当我使用fgets读取第一行时,它仅读取其中的一部分。 例如: 文件中的数据是:

{"line" : 85 ,"data" : "Jan 1 2017 00:00:00 75 822 96 548 85 76 82 93 78 82 64 89 899"}

但是fgets()仅读:

{"line" : 85 ,

我的JSON模板是:

char *info = "{""\"line\":int32,""\"data\":string}";

我的代码:

    /*
 *  ======== fatsd.c ========
 */
#include <file.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ti/utils/json/json.h>

#include <third_party/fatfs/ffcio.h>

#include <ti/display/Display.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SDFatFS.h>

/* Example/Board Header files */
#include "Board.h"

/* Buffer size used for the file copy process */
#ifndef CPY_BUFF_SIZE
#define CPY_BUFF_SIZE       2048
#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 cpy1[]="\r\n";
  char    conbuff[100];

/* File name prefix for this filesystem for use with TI C RTS */
char fatfsPrefix[] = "fat";

char cpy_buff[CPY_BUFF_SIZE];

int filesize=0;
int buffsize,pos;
char text[] = "Jan 1 2017 00:00:00 75 822 96 548 85 76 82 93 78 82 64 89 899"


/*
 *  ======== 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,""\"data\":string}";
    Json_createTemplate(&datalog, info,strlen(info));

  Json_Handle object;
  Json_createObject(&object, datalog, 1024);

  char *key = "\"line\"";
  uint32_t value=85 ;
  uint32_t value3 ;
  uint16_t valuesize = sizeof(value);
  Json_setValue(object,key,&value,valuesize);

  char *key1 = "\"data\"";
  char value1[100];
  char value2[100];

  uint16_t valuesize1 = sizeof(value1);
  uint16_t valuesize2 = sizeof(value2);
  strcpy(value1,text);

  Json_setValue(object,key1,&value1,valuesize1);

  uint16_t builtBuffSize =sizeof(cpy_buff);
  Json_build(object,cpy_buff,&builtBuffSize);


 /* Call driver init functions */
  GPIO_init();
  SDFatFS_init();

    /* Configure the LED pin */
    GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

    /* 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);

    /* Open the display for output */
    display = Display_open(Display_Type_UART, NULL);
    if (display == NULL) {
        /* Failed to open display driver */
        while (1);
    }

    /* Turn on user LED */
    GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);


    /* Mount and register the SD Card */
    sdfatfsHandle = SDFatFS_open(Board_SDFatFS0, DRIVE_NUM);
    if (sdfatfsHandle == NULL) {
        Display_printf(display, 0, 0, "Error starting the SD card\n");
        while (1);
    }
    else {
        Display_printf(display, 0, 0, "Drive %u is mounted\n", DRIVE_NUM);
    }

    /* Try to open the source file */
    new=fopen(copyfile,"r");
    if (!new) {
        strcpy(conbuff,"temp not opened");
        puts(conbuff);
    }

// calculate total no of lines
   fseek(new,0,SEEK_END);
   filesize = ftell(new);
   rewind(new);

 fclose(new);

 new=fopen(copyfile,"r");
 memset(cpy_buff, 0, sizeof(cpy_buff));

 fgets(cpy_buff,sizeof(cpy_buff),new);

 puts("value in buff after reading");
 puts(cpy_buff);

 Json_parse(object, cpy_buff, strlen(cpy_buff));
 puts("value in buff after parsing");
 puts(cpy_buff);

 Json_getValue(object, key1, &value2, &valuesize2);
 Json_getValue(object, key, &value3, &valuesize);
 puts("data is");
 puts(value2);

 fclose(new);


    /* Stopping the SDCard */
    SDFatFS_close(sdfatfsHandle);
    Display_printf(display, 0, 0, "Drive %u unmounted\n", DRIVE_NUM);


    return (NULL);
}


/*
 *  ======== fatfs_getFatTime ========
 */

int32_t fatfs_getFatTime(void)
{
    /*
     *  FatFs uses this API to get the current time in FatTime format.  User's
     *  must implement this function based on their system's timekeeping
     *  mechanism.  See FatFs documentation for details on FatTime format.
     */
    /* Jan 1 2017 00:00:00 */
    return (0x4A210000);
}

0 个答案:

没有答案