简体   繁体   中英

reading from a json file using fscanf() in c

I was wondering if someone could explain why it is reading zeros and how to fix it

#include <stdio.h>

int main(void) {
  FILE* f = fopen("text.json","r");
  int l,x=0;
  fscanf(f,"%d %d ",&l,&x);
  printf("%d %d ",l,x);
 
  return 0;
}

my text.json:

[ 1, 2, 3, 4 ]

when run this prints two 0s. I don't really understand why or how I would make it print numbers.

use fread instead of fscanf

char buffer[1024] = {0};

fread(buffer, 1024, 1, f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM