eof(f)在c编程中的文件处理

时间:2016-09-02 11:41:07

标签: c json filereader file-handling

我刚刚构建了一个程序,它从一个名为“text.txt”的文件中读取数据并通过JSON解析它,因为我知道我正在读取字符的文件的大小因此在循环中我定义了值如果价值增加或减少那么请求帮我,那么我将如何管理它。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "jsmn.h"

void error(char *msg)
{
      perror(msg);
      exit(0);
}
static int jsoneq(const char *json, jsmntok_t *tok, const char *s)
{

    if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && strncmp(json + tok->start, s, tok->end - tok->start) == 0)
    {
        return 0;
    }
    return -1;
}
int main(int argc, char const *argv[])
{ 
    int i,n,r;
    char buf[1024];
    char JSON_STRING[5000];
    jsmn_parser p;
    jsmntok_t t[2048];
    char * fname;
    FILE *fp=fopen("text.txt","r+");
    FILE *ff;
    if(fp==NULL)
    {
        error("file opening error");
    }
    for(int i=0; i<647;i++)   /////////this if the for loop //////////
    {
        JSON_STRING[i] = getc(fp);
    }
    jsmn_init(&p);
    r = jsmn_parse(&p,JSON_STRING,  strlen(JSON_STRING), t,     sizeof(t)/sizeof(t[0]));
    if (r < 0){
        printf("\nFailed to parse JSON: %d\n", r);
        return 1;
    }
    else{
        for (i = 1; i < r; i++){
            if (jsoneq(JSON_STRING, &t[i], "RID") == 0)
            // for extracting the value of Rid from the complete string 
            {/* We may use strndup() to fetch string value */
            //printf("RID: '%.*s'\n",t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start);
            printf("- RID: %.*s\n", t[i+1].end-t[i+1].start,JSON_STRING + t[i+1].start);
            sprintf(fname,"%.*s",t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start);
            ff=fopen (fname, "w");
            fprintf(ff, "RID: '%.*s'\n",t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start );
            i++;
        }
    }
    else if (jsoneq(JSON_STRING, &t[i], "DID") == 0)    //for Extracting the value of DID from te string recived from the client JSON_STrING
    {
        /* We may additionally check if the value is either "true" or "false"*/
        printf("- DID: %.*s\n", t[i+1].end-t[i+1].start,JSON_STRING + t[i+1].start);
        fprintf(ff, "DID: '%.*s'\n",t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start );
        i++;
        //sprintf(fname,"%s_%.*s",fname,t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start); 
    }
    else if (jsoneq(JSON_STRING, &t[i],"TS") == 0)
    {
        /* We may want to do strtol() here to get numeric value */
        printf("- TS: %.*s\n", t[i+1].end-t[i+1].start,JSON_STRING + t[i+1].start);
        fprintf(ff, "TS: '%.*s'\n",t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start );
         // sprintf(fname,"%s_%.*s",fname,t[i+1].end - t[i+1].start, JSON_STRING+ t[i+1].start);

    }
    /*  else
    {
    printf("Unexpected key: %.*s\n", t[i].end-t[i].start,JSON_STRING + t[i].start);}*/
    }
        printf("JSON parsed : %d\n", r);
        printf("output have been generated to the file");
        fprintf(ff, "%s\n",JSON_STRING );
        fclose(ff);
    }
    return EXIT_SUCCESS;
    fclose(fp);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

首先需要compute the amount of characters in the file,这将是你的json缓冲区的大小

 //computing how much data is needed
fseek(fp, 0L, SEEK_END);
long int json_size = ftell(fp);
rewind(fp);

然后读取文件,直到到达缓冲区的末尾或读取的值为EOF。使用上面的代码,这两个条件都是相同的,但在其他条件下,您必须使用这两个测试。哪个给

JSON_STRING = (char*)malloc(json_size+1 * sizeof(char));
JSON_STRING[json_size] = '\0';
//reading the value
int val = 0;
int i = 0;
while(i < json_size && (val = getc(fp)) != EOF)
{
   JSON_STRING[i] = val;
   ++i;
}