Segmentation fault (core dumped) when execute my program
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
i am begginer at c programming and i wrote program and trying to run my program via terminal
from terminal:
$ ./Countries 2 /home/david/CLionProjects/untitled/dddd.txt
i get the error:
message: Segmentation fault (core dumped)
but when i run my program with my Clion its work perfectly.
i tried to debug with dbg and i found the error:
Program received signal SIGSEGV, Segmentation fault.
_IO_fgets (buf=0x7fffffffdc00 "", n=300, fp=0x0) at iofgets.c:47
47 iofgets.c: No such file or directory.
its seems that my program cant read the file dddd.txt and i cant understand why .
my main:
#include <stdio.h>
#include "Countries.h"
#include "Countries.c"
#include <string.h>
int main(int argc, char *argv) {
{
int fortest= atoi(argv[1]); //number of Countries
char *useForString; // use us to points to string use like name of country or city or food
int incrementCountry = -1;// increment every time when enter country from input
FILE *fp = fopen(argv[2], "r");
int chooseOfclient; //input from client number between 1-6
char singLine[300]; //char array for the line from text
Country **Atlas = buildCountry(fortest); //build Countries arraywhile ((ch != 'n') && (ch != EOF))
while (fgets(singLine, sizeof(singLine), fp))
{//while1
if (singLine[0] != 't') {//if
incrementCountry++;
useForString = cutNameOfcountry(singLine);
Atlas[incrementCountry] = inputNewCountry(useForString, cutCordiantesLeft(singLine),
cutCordiantesRight(singLine));
free(useForString); // free the memory from heap that allocate inside cutNameOfCountr
continue;
}//if
City *pCity = cutCityData(singLine);
buildCity(Atlas[incrementCountry], pCity);
}//while
return (0)
}
c
add a comment |
i am begginer at c programming and i wrote program and trying to run my program via terminal
from terminal:
$ ./Countries 2 /home/david/CLionProjects/untitled/dddd.txt
i get the error:
message: Segmentation fault (core dumped)
but when i run my program with my Clion its work perfectly.
i tried to debug with dbg and i found the error:
Program received signal SIGSEGV, Segmentation fault.
_IO_fgets (buf=0x7fffffffdc00 "", n=300, fp=0x0) at iofgets.c:47
47 iofgets.c: No such file or directory.
its seems that my program cant read the file dddd.txt and i cant understand why .
my main:
#include <stdio.h>
#include "Countries.h"
#include "Countries.c"
#include <string.h>
int main(int argc, char *argv) {
{
int fortest= atoi(argv[1]); //number of Countries
char *useForString; // use us to points to string use like name of country or city or food
int incrementCountry = -1;// increment every time when enter country from input
FILE *fp = fopen(argv[2], "r");
int chooseOfclient; //input from client number between 1-6
char singLine[300]; //char array for the line from text
Country **Atlas = buildCountry(fortest); //build Countries arraywhile ((ch != 'n') && (ch != EOF))
while (fgets(singLine, sizeof(singLine), fp))
{//while1
if (singLine[0] != 't') {//if
incrementCountry++;
useForString = cutNameOfcountry(singLine);
Atlas[incrementCountry] = inputNewCountry(useForString, cutCordiantesLeft(singLine),
cutCordiantesRight(singLine));
free(useForString); // free the memory from heap that allocate inside cutNameOfCountr
continue;
}//if
City *pCity = cutCityData(singLine);
buildCity(Atlas[incrementCountry], pCity);
}//while
return (0)
}
c
It seems that the problem is related tofgets()
receivingNULL
as pointer to file (fp=0x00 in the error), so check the result offopen()
withperror()
, also#include <std
lib.h> if you are usingfree()
– Keine Lust
Nov 17 '18 at 7:07
yeah , this is the problem : Error: : No such file or directory Segmentation fault (core dumped) idea how can i fix it?
– David Zaltsman
Nov 17 '18 at 7:08
Have you checked the result ofperror("fopen")
? "No such file" is pretty descriptive :P
– Keine Lust
Nov 17 '18 at 7:13
i added if after the line FILE *fp = fopen(argv[2], "r"); if(fp==NULL) perror("Error: "); and than tried to execute again and got : Error : No such file or directory but if build and run my program via Clion , its work perfectly
– David Zaltsman
Nov 17 '18 at 7:19
add a comment |
i am begginer at c programming and i wrote program and trying to run my program via terminal
from terminal:
$ ./Countries 2 /home/david/CLionProjects/untitled/dddd.txt
i get the error:
message: Segmentation fault (core dumped)
but when i run my program with my Clion its work perfectly.
i tried to debug with dbg and i found the error:
Program received signal SIGSEGV, Segmentation fault.
_IO_fgets (buf=0x7fffffffdc00 "", n=300, fp=0x0) at iofgets.c:47
47 iofgets.c: No such file or directory.
its seems that my program cant read the file dddd.txt and i cant understand why .
my main:
#include <stdio.h>
#include "Countries.h"
#include "Countries.c"
#include <string.h>
int main(int argc, char *argv) {
{
int fortest= atoi(argv[1]); //number of Countries
char *useForString; // use us to points to string use like name of country or city or food
int incrementCountry = -1;// increment every time when enter country from input
FILE *fp = fopen(argv[2], "r");
int chooseOfclient; //input from client number between 1-6
char singLine[300]; //char array for the line from text
Country **Atlas = buildCountry(fortest); //build Countries arraywhile ((ch != 'n') && (ch != EOF))
while (fgets(singLine, sizeof(singLine), fp))
{//while1
if (singLine[0] != 't') {//if
incrementCountry++;
useForString = cutNameOfcountry(singLine);
Atlas[incrementCountry] = inputNewCountry(useForString, cutCordiantesLeft(singLine),
cutCordiantesRight(singLine));
free(useForString); // free the memory from heap that allocate inside cutNameOfCountr
continue;
}//if
City *pCity = cutCityData(singLine);
buildCity(Atlas[incrementCountry], pCity);
}//while
return (0)
}
c
i am begginer at c programming and i wrote program and trying to run my program via terminal
from terminal:
$ ./Countries 2 /home/david/CLionProjects/untitled/dddd.txt
i get the error:
message: Segmentation fault (core dumped)
but when i run my program with my Clion its work perfectly.
i tried to debug with dbg and i found the error:
Program received signal SIGSEGV, Segmentation fault.
_IO_fgets (buf=0x7fffffffdc00 "", n=300, fp=0x0) at iofgets.c:47
47 iofgets.c: No such file or directory.
its seems that my program cant read the file dddd.txt and i cant understand why .
my main:
#include <stdio.h>
#include "Countries.h"
#include "Countries.c"
#include <string.h>
int main(int argc, char *argv) {
{
int fortest= atoi(argv[1]); //number of Countries
char *useForString; // use us to points to string use like name of country or city or food
int incrementCountry = -1;// increment every time when enter country from input
FILE *fp = fopen(argv[2], "r");
int chooseOfclient; //input from client number between 1-6
char singLine[300]; //char array for the line from text
Country **Atlas = buildCountry(fortest); //build Countries arraywhile ((ch != 'n') && (ch != EOF))
while (fgets(singLine, sizeof(singLine), fp))
{//while1
if (singLine[0] != 't') {//if
incrementCountry++;
useForString = cutNameOfcountry(singLine);
Atlas[incrementCountry] = inputNewCountry(useForString, cutCordiantesLeft(singLine),
cutCordiantesRight(singLine));
free(useForString); // free the memory from heap that allocate inside cutNameOfCountr
continue;
}//if
City *pCity = cutCityData(singLine);
buildCity(Atlas[incrementCountry], pCity);
}//while
return (0)
}
c
c
asked Nov 17 '18 at 6:32
David ZaltsmanDavid Zaltsman
63
63
It seems that the problem is related tofgets()
receivingNULL
as pointer to file (fp=0x00 in the error), so check the result offopen()
withperror()
, also#include <std
lib.h> if you are usingfree()
– Keine Lust
Nov 17 '18 at 7:07
yeah , this is the problem : Error: : No such file or directory Segmentation fault (core dumped) idea how can i fix it?
– David Zaltsman
Nov 17 '18 at 7:08
Have you checked the result ofperror("fopen")
? "No such file" is pretty descriptive :P
– Keine Lust
Nov 17 '18 at 7:13
i added if after the line FILE *fp = fopen(argv[2], "r"); if(fp==NULL) perror("Error: "); and than tried to execute again and got : Error : No such file or directory but if build and run my program via Clion , its work perfectly
– David Zaltsman
Nov 17 '18 at 7:19
add a comment |
It seems that the problem is related tofgets()
receivingNULL
as pointer to file (fp=0x00 in the error), so check the result offopen()
withperror()
, also#include <std
lib.h> if you are usingfree()
– Keine Lust
Nov 17 '18 at 7:07
yeah , this is the problem : Error: : No such file or directory Segmentation fault (core dumped) idea how can i fix it?
– David Zaltsman
Nov 17 '18 at 7:08
Have you checked the result ofperror("fopen")
? "No such file" is pretty descriptive :P
– Keine Lust
Nov 17 '18 at 7:13
i added if after the line FILE *fp = fopen(argv[2], "r"); if(fp==NULL) perror("Error: "); and than tried to execute again and got : Error : No such file or directory but if build and run my program via Clion , its work perfectly
– David Zaltsman
Nov 17 '18 at 7:19
It seems that the problem is related to
fgets()
receiving NULL
as pointer to file (fp=0x00 in the error), so check the result of fopen()
with perror()
, also #include <std
lib.h> if you are using free()
– Keine Lust
Nov 17 '18 at 7:07
It seems that the problem is related to
fgets()
receiving NULL
as pointer to file (fp=0x00 in the error), so check the result of fopen()
with perror()
, also #include <std
lib.h> if you are using free()
– Keine Lust
Nov 17 '18 at 7:07
yeah , this is the problem : Error: : No such file or directory Segmentation fault (core dumped) idea how can i fix it?
– David Zaltsman
Nov 17 '18 at 7:08
yeah , this is the problem : Error: : No such file or directory Segmentation fault (core dumped) idea how can i fix it?
– David Zaltsman
Nov 17 '18 at 7:08
Have you checked the result of
perror("fopen")
? "No such file" is pretty descriptive :P– Keine Lust
Nov 17 '18 at 7:13
Have you checked the result of
perror("fopen")
? "No such file" is pretty descriptive :P– Keine Lust
Nov 17 '18 at 7:13
i added if after the line FILE *fp = fopen(argv[2], "r"); if(fp==NULL) perror("Error: "); and than tried to execute again and got : Error : No such file or directory but if build and run my program via Clion , its work perfectly
– David Zaltsman
Nov 17 '18 at 7:19
i added if after the line FILE *fp = fopen(argv[2], "r"); if(fp==NULL) perror("Error: "); and than tried to execute again and got : Error : No such file or directory but if build and run my program via Clion , its work perfectly
– David Zaltsman
Nov 17 '18 at 7:19
add a comment |
0
active
oldest
votes
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%2f53348854%2fsegmentation-fault-core-dumped-when-execute-my-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53348854%2fsegmentation-fault-core-dumped-when-execute-my-program%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
It seems that the problem is related to
fgets()
receivingNULL
as pointer to file (fp=0x00 in the error), so check the result offopen()
withperror()
, also#include <std
lib.h> if you are usingfree()
– Keine Lust
Nov 17 '18 at 7:07
yeah , this is the problem : Error: : No such file or directory Segmentation fault (core dumped) idea how can i fix it?
– David Zaltsman
Nov 17 '18 at 7:08
Have you checked the result of
perror("fopen")
? "No such file" is pretty descriptive :P– Keine Lust
Nov 17 '18 at 7:13
i added if after the line FILE *fp = fopen(argv[2], "r"); if(fp==NULL) perror("Error: "); and than tried to execute again and got : Error : No such file or directory but if build and run my program via Clion , its work perfectly
– David Zaltsman
Nov 17 '18 at 7:19