Getting Segmentation fault when trying to read a json file










0















I'm trying to parse a json file using jsmn but I get segmentation fault when I run my application. I'm using C and compiling on Ubuntu machine.



Please find the code snippet below:




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

#define JSON_FILE_PATH "/home/admin/Desktop/test/server/dataFile.json"
#define BUFFER_SIZE 5000
#define MAX_TOKEN_COUNT 128

// Read files
void readfile(char* filepath, char* fileContent)

FILE *f;
char c;
int index;
f = fopen(filepath, "r");
***while((c = fgetc(f)) != EOF)*** ------> seg fault
fileContent[index] = c;
index++;

fileContent[index] = '';


// This is where the magic happens
int parseJSON(char *filepath, void callback(char *, char*))

// Only prints the key and value
void mycallback(char *key, char* value)
printf("%s : %sn", key, value);


int main()

parseJSON(JSON_FILE_PATH, mycallback);
return 0;




I get segmentation fault after the line as indicated when the file operation of reading is done.



On debugging, at this point f contains 0x00 value which means it is not able to identify the file.



Why this might be happening when file is present at that location?



I tried changing paths but still same issue.










share|improve this question



















  • 2





    index is not initialized before use. UB.

    – babon
    Nov 12 '18 at 4:59






  • 4





    char c should be int c - fgetc returns an int

    – Rishikesh Raje
    Nov 12 '18 at 5:00











  • thankyou for the help..It is working now !!

    – user2156513
    Nov 12 '18 at 5:09











  • @user2156513 So, in that case, write an answer for your own question and mark it as accepted. This way, in future other people searching for the same problem will get a solution.

    – Sourav Ghosh
    Nov 12 '18 at 5:14















0















I'm trying to parse a json file using jsmn but I get segmentation fault when I run my application. I'm using C and compiling on Ubuntu machine.



Please find the code snippet below:




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

#define JSON_FILE_PATH "/home/admin/Desktop/test/server/dataFile.json"
#define BUFFER_SIZE 5000
#define MAX_TOKEN_COUNT 128

// Read files
void readfile(char* filepath, char* fileContent)

FILE *f;
char c;
int index;
f = fopen(filepath, "r");
***while((c = fgetc(f)) != EOF)*** ------> seg fault
fileContent[index] = c;
index++;

fileContent[index] = '';


// This is where the magic happens
int parseJSON(char *filepath, void callback(char *, char*))

// Only prints the key and value
void mycallback(char *key, char* value)
printf("%s : %sn", key, value);


int main()

parseJSON(JSON_FILE_PATH, mycallback);
return 0;




I get segmentation fault after the line as indicated when the file operation of reading is done.



On debugging, at this point f contains 0x00 value which means it is not able to identify the file.



Why this might be happening when file is present at that location?



I tried changing paths but still same issue.










share|improve this question



















  • 2





    index is not initialized before use. UB.

    – babon
    Nov 12 '18 at 4:59






  • 4





    char c should be int c - fgetc returns an int

    – Rishikesh Raje
    Nov 12 '18 at 5:00











  • thankyou for the help..It is working now !!

    – user2156513
    Nov 12 '18 at 5:09











  • @user2156513 So, in that case, write an answer for your own question and mark it as accepted. This way, in future other people searching for the same problem will get a solution.

    – Sourav Ghosh
    Nov 12 '18 at 5:14













0












0








0








I'm trying to parse a json file using jsmn but I get segmentation fault when I run my application. I'm using C and compiling on Ubuntu machine.



Please find the code snippet below:




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

#define JSON_FILE_PATH "/home/admin/Desktop/test/server/dataFile.json"
#define BUFFER_SIZE 5000
#define MAX_TOKEN_COUNT 128

// Read files
void readfile(char* filepath, char* fileContent)

FILE *f;
char c;
int index;
f = fopen(filepath, "r");
***while((c = fgetc(f)) != EOF)*** ------> seg fault
fileContent[index] = c;
index++;

fileContent[index] = '';


// This is where the magic happens
int parseJSON(char *filepath, void callback(char *, char*))

// Only prints the key and value
void mycallback(char *key, char* value)
printf("%s : %sn", key, value);


int main()

parseJSON(JSON_FILE_PATH, mycallback);
return 0;




I get segmentation fault after the line as indicated when the file operation of reading is done.



On debugging, at this point f contains 0x00 value which means it is not able to identify the file.



Why this might be happening when file is present at that location?



I tried changing paths but still same issue.










share|improve this question
















I'm trying to parse a json file using jsmn but I get segmentation fault when I run my application. I'm using C and compiling on Ubuntu machine.



Please find the code snippet below:




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

#define JSON_FILE_PATH "/home/admin/Desktop/test/server/dataFile.json"
#define BUFFER_SIZE 5000
#define MAX_TOKEN_COUNT 128

// Read files
void readfile(char* filepath, char* fileContent)

FILE *f;
char c;
int index;
f = fopen(filepath, "r");
***while((c = fgetc(f)) != EOF)*** ------> seg fault
fileContent[index] = c;
index++;

fileContent[index] = '';


// This is where the magic happens
int parseJSON(char *filepath, void callback(char *, char*))

// Only prints the key and value
void mycallback(char *key, char* value)
printf("%s : %sn", key, value);


int main()

parseJSON(JSON_FILE_PATH, mycallback);
return 0;




I get segmentation fault after the line as indicated when the file operation of reading is done.



On debugging, at this point f contains 0x00 value which means it is not able to identify the file.



Why this might be happening when file is present at that location?



I tried changing paths but still same issue.







c json linux ubuntu jsmn






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 8 at 5:14









Cœur

18.3k9108148




18.3k9108148










asked Nov 12 '18 at 4:57









user2156513user2156513

366




366







  • 2





    index is not initialized before use. UB.

    – babon
    Nov 12 '18 at 4:59






  • 4





    char c should be int c - fgetc returns an int

    – Rishikesh Raje
    Nov 12 '18 at 5:00











  • thankyou for the help..It is working now !!

    – user2156513
    Nov 12 '18 at 5:09











  • @user2156513 So, in that case, write an answer for your own question and mark it as accepted. This way, in future other people searching for the same problem will get a solution.

    – Sourav Ghosh
    Nov 12 '18 at 5:14












  • 2





    index is not initialized before use. UB.

    – babon
    Nov 12 '18 at 4:59






  • 4





    char c should be int c - fgetc returns an int

    – Rishikesh Raje
    Nov 12 '18 at 5:00











  • thankyou for the help..It is working now !!

    – user2156513
    Nov 12 '18 at 5:09











  • @user2156513 So, in that case, write an answer for your own question and mark it as accepted. This way, in future other people searching for the same problem will get a solution.

    – Sourav Ghosh
    Nov 12 '18 at 5:14







2




2





index is not initialized before use. UB.

– babon
Nov 12 '18 at 4:59





index is not initialized before use. UB.

– babon
Nov 12 '18 at 4:59




4




4





char c should be int c - fgetc returns an int

– Rishikesh Raje
Nov 12 '18 at 5:00





char c should be int c - fgetc returns an int

– Rishikesh Raje
Nov 12 '18 at 5:00













thankyou for the help..It is working now !!

– user2156513
Nov 12 '18 at 5:09





thankyou for the help..It is working now !!

– user2156513
Nov 12 '18 at 5:09













@user2156513 So, in that case, write an answer for your own question and mark it as accepted. This way, in future other people searching for the same problem will get a solution.

– Sourav Ghosh
Nov 12 '18 at 5:14





@user2156513 So, in that case, write an answer for your own question and mark it as accepted. This way, in future other people searching for the same problem will get a solution.

– Sourav Ghosh
Nov 12 '18 at 5:14












1 Answer
1






active

oldest

votes


















0














Found the solution:



index is not initialized before use
char c should be int c - fgetc returns an int






share|improve this answer























  • For large files, suggest int index; --> size_t index = 0;

    – chux
    Nov 12 '18 at 7:10










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256169%2fgetting-segmentation-fault-when-trying-to-read-a-json-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Found the solution:



index is not initialized before use
char c should be int c - fgetc returns an int






share|improve this answer























  • For large files, suggest int index; --> size_t index = 0;

    – chux
    Nov 12 '18 at 7:10















0














Found the solution:



index is not initialized before use
char c should be int c - fgetc returns an int






share|improve this answer























  • For large files, suggest int index; --> size_t index = 0;

    – chux
    Nov 12 '18 at 7:10













0












0








0







Found the solution:



index is not initialized before use
char c should be int c - fgetc returns an int






share|improve this answer













Found the solution:



index is not initialized before use
char c should be int c - fgetc returns an int







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 5:22









user2156513user2156513

366




366












  • For large files, suggest int index; --> size_t index = 0;

    – chux
    Nov 12 '18 at 7:10

















  • For large files, suggest int index; --> size_t index = 0;

    – chux
    Nov 12 '18 at 7:10
















For large files, suggest int index; --> size_t index = 0;

– chux
Nov 12 '18 at 7:10





For large files, suggest int index; --> size_t index = 0;

– chux
Nov 12 '18 at 7:10



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256169%2fgetting-segmentation-fault-when-trying-to-read-a-json-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ