今天又开了一下文件指针,发现还是有很多东西不明白,我今天写的这个程序是随机生成几个数字,然后将他们转换成字符串存到文件中,写出来发现存在文件中的是个0
最后发现是我在程序中间无意间修改了随机数的值导致上述的问题
看程序:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <ctype.h>
char *buffer()
{
int number[10];
char **string = (char **)malloc(sizeof(char *)*10);
for(int i=0;i<10;i++)
{
int count = 0;
int temp;
number[i] = rand();
temp = number[i];
#if 0
printf("number:%d\n",number[i]);
#endif
while(temp!=0)
{
count++;
temp/=10;
}
#if 0
printf("count=%d\n",count);
#endif
string[i] = (char *)malloc(sizeof(char)*(count+1));
itoa(number[i],string[i],10);
#if 0
printf("after number = %d\n",number[i]);
printf("string:");
printf("%s\n",string[i]);
#endif
}
return *string;
}
void main()
{
srand(time(0));
FILE *fp;
if ((fp=fopen("E:\\3.txt","w+"))==NULL)
{
printf("wrong location!\n");
exit(-1);
}
// int *ch;
// ch = buffer();
for (int i=0;i<10;i++)
{
fputs(buffer(),fp);
fputs("\n",fp);
}
system("pause");
}
中间被我干点的行基本上都是调试用的可以pass掉!