我是C编程的初学者,我编写了程序,并试图通过终端运行程序
从终端:
$ ./Countries 2 /home/david/CLionProjects/untitled/dddd.txt
我得到了错误:
message: Segmentation fault (core dumped)
但是当我用Clion运行程序时,它的工作就完美了。 我尝试使用dbg进行调试,但发现了错误:
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.
似乎我的程序无法读取dddd.txt文件,我也无法理解为什么。
我的主:
#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)
}