- 论坛徽章:
- 0
|
代码如下:
#include <stdio.h>;
char * getResult(char * cmd)
{
int i = 0;
char result[18];
char ch_record;
FILE *fp;
memset(result,0,sizeof(result));
if((fp = fopen(cmd,"r" ) == NULL){
printf("file read error!" ;
return NULL;
}
while (!feof(fp))
{
ch_record = fgetc(fp);
if(ch_record == '!'){
do{
result = fgetc(fp);
ch_record = result;
i++;
//printf("i = %d:result[i-1] = %c \n",i,result[i-1]);
if(i >; 7){
printf("error of array!\n" ;
return result;
}
}while(ch_record != '!');
//break;
}
}
result[i-1] = '\0';
printf("result = %s result's length is : %d\n",result,strlen(result));
//return result;
fclose(fp);
return &(result[0]);
}
void main()
{
char str[20] = {"result.txt"};
char str1[20];
char *p1 = str1;
printf("=====%s\n",getResult(str));
strcpy(p1,getResult(str));
printf("str1 = %s;\n",p1);
}
结果为:
result = 1000 result's length is : 4
=====1000
result = 1000 result's length is : 4
str1 = ;
为何可以打印不可以赋值?高人指点! |
|