不确定是什么问题,但是,我在文件“ m.fs182”中包含内容,当我通过命令行以及可执行文件“ ./P0 m.fs182”运行该文件时,我收到了'abort'消息如果文件指针返回NULL,将显示该内容。为何不这样做,为什么要这样做呢?
#include "display.h"
#include "traversals.h"
#include "tree.h"
#include "node.h"
using namespace std;
int main(int argc, char **argv) {
char buffer[100];
FILE *fp;
char* fb = (char *)malloc(strlen("out") + 7);
if(argc > 2) {
string sb = "out";
strcpy(fb, sb.c_str());
fp = tmpfile();
if(fp == NULL) {
abort();
}
do
{
fputs(buffer,fp);
}while(fgets(buffer, sizeof(buffer), stdin));
rewind(fp);
}
else if(argc == 2) {
char *filename = (char *)malloc(strlen(argv[1] + 7));
strcpy(filename, argv[1]);
strcpy(fb, argv[1]);
strcat(filename, ".fs182");
if((fp = fopen(filename, "r")) == NULL) {
abort();
}
}else {
printf("%s", "Invalid numbr of arguments\n");
return 1;
}
Node* root = buildTree(fp);
char* traversePre = (char *)malloc(strlen(fb)+17);
char* traverseIn = (char *)malloc(strlen(fb)+17);
char* traversePost = (char *)malloc(strlen(fb)+17);
strcpy(traversePre, fb);
strcat(traversePre, ".preorder");
strcpy(traverseIn, fb);
strcat(traverseIn, ".inorder");
strcpy(traversePost, fb);
strcat(traversePost, ".postorder");
printf("Preorder: ");
traversePreorder(root, 0, traversePre);
printf("\nInorder: ");
traverseInorder(root, 0, traverseIn);
printf("\nPostorder: ");
traversePostorder(root, 0, traversePost);
return 0;
}