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

c程序中文件处理中的eof(f),c,json,filereader,file-handling,C,Json,Filereader,File Handling,我刚刚建立了一个程序,从一个名为text.txt的文件中读取数据,并通过JSON解析它,因为我知道我读取的文件的大小,所以在for循环中,我将i in for循环的值定义为647,请帮助我,如果该值增加或减少,那么我将如何管理它 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include "jsmn.h" void error(char

我刚刚建立了一个程序,从一个名为text.txt的文件中读取数据,并通过JSON解析它,因为我知道我读取的文件的大小,所以在for循环中,我将i in for循环的值定义为647,请帮助我,如果该值增加或减少,那么我将如何管理它

#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;
}
您首先需要知道,这将是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;
}

请缩进你的代码。另外,请参见。这里有几个例子。@numer请不要让读者阅读不相关的代码和注释,因为它们会使你的文章更难阅读(即不太可能得到回答)。