Getting Segmentation fault when trying to read a json file
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
add a comment |
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
2
indexis not initialized before use. UB.
– babon
Nov 12 '18 at 4:59
4
char cshould beint c-fgetcreturns anint
– 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
add a comment |
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
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
c json linux ubuntu jsmn
edited Jan 8 at 5:14
Cœur
18.3k9108148
18.3k9108148
asked Nov 12 '18 at 4:57
user2156513user2156513
366
366
2
indexis not initialized before use. UB.
– babon
Nov 12 '18 at 4:59
4
char cshould beint c-fgetcreturns anint
– 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
add a comment |
2
indexis not initialized before use. UB.
– babon
Nov 12 '18 at 4:59
4
char cshould beint c-fgetcreturns anint
– 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
add a comment |
1 Answer
1
active
oldest
votes
Found the solution:
index is not initialized before use
char c should be int c - fgetc returns an int
For large files, suggestint index;-->size_t index = 0;
– chux
Nov 12 '18 at 7:10
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Found the solution:
index is not initialized before use
char c should be int c - fgetc returns an int
For large files, suggestint index;-->size_t index = 0;
– chux
Nov 12 '18 at 7:10
add a comment |
Found the solution:
index is not initialized before use
char c should be int c - fgetc returns an int
For large files, suggestint index;-->size_t index = 0;
– chux
Nov 12 '18 at 7:10
add a comment |
Found the solution:
index is not initialized before use
char c should be int c - fgetc returns an int
Found the solution:
index is not initialized before use
char c should be int c - fgetc returns an int
answered Nov 12 '18 at 5:22
user2156513user2156513
366
366
For large files, suggestint index;-->size_t index = 0;
– chux
Nov 12 '18 at 7:10
add a comment |
For large files, suggestint 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
indexis not initialized before use. UB.– babon
Nov 12 '18 at 4:59
4
char cshould beint c-fgetcreturns anint– 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