发生代码片段问题,调用子程序Create_PC_List()
以main()
创建计算机列表。
void Router :: Create_PC_List (char *fname) //Reads from file & creates list of computers
{
char C;
char* IPAD=NULL;
FILE* fp;
FILE* msg;
int counter=0;
fp = fopen (fname,"r"); //Reading from Argv parameter file - list of computers in the network
if(fp == NULL)
cout << "Opening file failed: %s\n" << strerror(errno) << endl;
msg = fopen (fname, "r"); //Reading from Argv parameter file - list of computers in the network
while((C=fgetc(fp)) != EOF) //Running trougth the file
{
...
输出:
Opening file failed: No such file or directory
followed by segmentation fault.
我们可以看到fopen()
failed with error "No such file or directory"
。因此,您需要找到一种方法将三个文本文件添加到代码块中,并使其可用于fopen()
.
分段错误的第二个原因是提供给的 fpgetc()
是一个FILE *
包含NULL地址的指针,该地址不是有效的FILE对象。