求助,不知道为什么char[]不能通过%s输出。可能是指针问题?

jin349620491 2014-10-27 11:07:54
在下学生,这是我的一个作业。要求读取Input.txt,处理内容,排序筛选并写入Output.txt。我采用了二叉树的结构。问题与这个树有关。
以下是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct TreeNode *PtrToNode;
typedef PtrToNode Tree;

struct TreeNode
{
char name[9];
int age;
int networth;
Tree Left;
Tree Right;
};

Tree GetContent(char str[21])
{
Tree temp;
temp = (Tree)malloc(sizeof(struct TreeNode));
int n = strlen(str) - 1;
int i;
int count = 0;
char Age[4];
char Networth[8];
for(i = 0; i < n; i++)
{
if(str[i] != ' ')
{
temp->name[i] = str[i];
printf("temp->name[%d] = %c\n",i,temp->name[i]);//********************for testing
system("pause");
}
else{break;}
}
temp->name[i] = '\0';
printf("temp->name[%d] = %c\n",i,temp->name[i]);//********************for testing
system("pause");
printf("temp->name = %s\n",i, temp->name);//********************for testing
system("pause");
for(++i; i < n; i++)
{
if(str[i] != ' '){Age[count++] = str[i];}
else{break;}
}
Age[count] = '\0';
count = 0;
temp->age = atoi(Age);
for(++i; i < n; i++)
{
Networth[count++] = str[i];
}
Networth[count] = '\0';
temp->networth = atoi(Networth);
temp->Left = NULL;
temp->Right = NULL;
printf("temp->name = %c, temp->age = %d, temp->networth = %d",temp->name,temp->age,temp->networth);//********************for testing
system("pause");
return temp;
}

void AttachTree(Tree Mtree, Tree Atree)
{
if(Mtree == NULL){Mtree = Atree;}
else if(Atree->networth > Mtree->networth)
{
if(Mtree->Left == NULL){Mtree->Left = Atree;}
else{AttachTree(Mtree->Left,Atree);}
}
else if(Atree->networth < Mtree->networth)
{
if(Mtree->Right == NULL){Mtree->Right = Atree;}
else{AttachTree(Mtree->Right,Atree);}
}
else if(Atree->age < Mtree->age)
{
if(Mtree->Left == NULL){Mtree->Left = Atree;}
else{AttachTree(Mtree->Left,Atree);}
}
else if(Atree->age > Mtree->age)
{
if(Mtree->Right == NULL){Mtree->Right = Atree;}
else{AttachTree(Mtree->Right,Atree);}
}
else if(strcmp(Atree->name, Mtree->name) < 0)
{
if(Mtree->Left == NULL){Mtree->Left = Atree;}
else{AttachTree(Mtree->Left,Atree);}
}
else if(strcmp(Atree->name, Mtree->name) > 0)
{
if(Mtree->Right == NULL){Mtree->Right = Atree;}
else{AttachTree(Mtree->Right,Atree);}
}
else
{
printf("Attach tree failed, this two trees are the same.");
system("pause");
}
}

void OutPut(char queries[12], Tree tree)
{
PrintTree(tree);
system("pause");
int M, Amin, Amax, count, i, n;
char num[4];
n = strlen(queries) - 1;
for(i = 0; i < n; i++)
{
if(queries != ' '){num[count++] = queries[i];}
else{break;}
}
num[count] = '\0';
M = atoi(num);
count = 0;
for(++i; i < n; i++)
{
if(queries != ' '){num[count++] = queries[i];}
else{break;}
}
num[count] = '\0';
Amin = atoi(num);
count = 0;
for(++i; i < n; i++)
{
if(queries != ' '){num[count++] = queries[i];}
else{break;}
}
num[count] = '\0';
Amax = atoi(num);
count = 0;
strcpy(num,"");
printf("M = %d, Amin = %d, Amax = %d, n = %d\n",M,Amin,Amax,n);
system("pause");
Travel(tree, Amin, Amax, &count, M);
}

void Travel(Tree tree, int Amin, int Amax, int *count, int M)
{
if(count < M)
{
if(tree->Left != NULL){Travel(tree->Left, Amin, Amax, count, M);}
else
{
if(Amin < tree->age < Amax)
{
TreeOut(tree);
*count++;
}
if(tree->Right != NULL){Travel(tree->Right, Amin, Amax, count, M);}
}
}
}

void TreeOut(Tree tree)
{
FILE* fp;
fp=fopen("C:/Output.txt","w");
fprintf(fp,"%s %d %d\n",tree->name,tree->age,tree->networth);
fclose(fp);
printf("%s %d %d\n",tree->name,tree->age,tree->networth);
}

void PrintTree(Tree tree)//********************for testing
{
printf("%s %d %d\n",tree->name,tree->age,tree->networth);
}

int main()
{
int N, K;
int n, i, j;
int count = 0;
char ch;
char filename[100];
char str[100];
char num[10];
char queries[12];
char content[21];
FILE* fpr;
FILE* fpw;
//printf("\nEnter a filepath/filename:\n");
//gets(filename);
strcpy(filename,"D:/Input.txt");//********************for testing
fpr = fopen(filename,"r");
if(fpr == NULL)//if can't open the file
{
printf("File open error!\n");
system("pause");
return -1;
}
if(fgets(str,100,fpr) != NULL)
{
n = strlen(str) - 1;
if(str[0] == ' ' || str[0] < '0' || str[0]> '9')
{
printf("Can't get N.(Unexpected input in the file)");
puts(str[0]);
system("pause");
return -2;
}
else
{
ch = str[0];
num[count++] = ch;
}
for(i = 1; i < n; i++)
{
ch = str[i];
if(ch != ' ' && '0' <= ch <= '9')
{
num[count++] = ch;
num[count] = '\0';
}
else if(ch == ' '){break;}
else
{
printf("Can't get N.(Unexpected input in the file)");
system("pause");
return -2;
}
}
N = atoi(num);
if(N < 0)
{
printf("The number N is wrong.It shouldn't be %d, please input another N.",N);
system("pause");
}
strcpy(num, "");
count = 0;
ch = str[++i];
if(ch < '0' || ch > '9')
{
printf("Can't get K.(Unexpected input.)");
system("pause");
return -3;
}
for(; i < n; i++)
{
ch = str[i];
if(ch < '0' || ch > '9')
{
printf("Can't get K.(Unexpected input.)");
system("pause");
return -3;
}
else
{
num[count++] = ch;
num[count] = '\0';
}
}
K = atoi(num);
if(K < 0)
{
printf("The number K is wrong.It shouldn't be %d, please input another K.",K);
system("pause");
}
}
else//if the file is empty
{
printf("File read error!(Unexpected end of file)");
system("pause");
return -5;
}
printf("N = %d, K = %d\n",N,K);//********************for testing
Tree tree;
tree = (Tree)malloc(sizeof(struct TreeNode));
for(i = 0; i < N; i++)
{
fgets(content,100,fpr);
AttachTree(tree, GetContent(content));
//puts(content);
//system("pause");
}
PrintTree(tree);//********************for testing
//printf("-----------------------\n");
for(j = 1; j <= K; j++)
{
fpw=fopen("C:/Output.txt","w");
fprintf(fpw,"Case #%d:\n",j);
fclose(fpw);
fgets(queries,20,fpr);
puts(queries);
system("pause");
OutPut(queries, tree);
//system("pause");
}
fclose(fpr);
system("pause");
return 0;
}

读入的文件内容为:
12 4
Zoe_Bill 35 2333
Bob_Volk 24 5888
Anny_Cin 95 999999
Williams 30 -22
Cindy 76 76000
Alice 18 88888
Joe_Mike 32 3222
Micheal 5 300000
Rosemary 40 5888
Dobby 24 5888
Billy 24 5888
Nobody 5 0
4 15 45
4 30 35
4 5 95
1 45 50
第一行的两个数字分别为源数据的行数和对源数据的处理参数。暂且不提,问题出在第37行左右。树中有个name[9]成员。用来提取Input.txt从第二行到第十三行的人名。调试时注意到,读入第二行的时候,name[0]='Z', name[1]='o', name[2]='e'......name[7]='l', name[8]='\0'。但是紧接着的printf("temp->name = %s\n",i, temp->name);却输出了一大堆乱码。甚至因为没有找到末尾而直接导致程序崩溃。这让我很是摸不着头脑。难道它不是一个字符串数组?还是因为有个temp->的缘故让它不符合%s的定义了?求解。
(Ps:在下的电脑出了点问题,VC不能用,然后用的啊哈C进行编程。但啊哈C并不提供调试功能,所以可能有些其实很显眼的地方没能看到。)
(再Ps:在下的C语言功底并不很深厚,很多东西都是自己瞎琢磨的,如果各位大侠发现有什么地方不对的或者不好的,请不吝指出,多谢!)
...全文
428 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
shlvshe00 2014-10-30
  • 打赏
  • 举报
回复
i...........
xwuno1 2014-10-30
  • 打赏
  • 举报
回复
printf("temp->name = %s\n",i, temp->name)中缺少了一个控制符哈
xionggch 2014-10-28
  • 打赏
  • 举报
回复
printf("temp->name = %s\n",i, temp->name);//********************for testing 少了 i 对应的输出格式,变成成%s和i对应了所以乱码
勤奋的小游侠 2014-10-28
  • 打赏
  • 举报
回复
printf("temp->name = %s\n",i, temp->name); 楼上的确实说对了,为什么人的代码还有个i在里面?是copy进没有改好吧
math_zhiguo 2014-10-27
  • 打赏
  • 举报
回复
没看懂。。。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧