文件指針問題(2)

今天又開了一下文件指針,發現還是有很多東西不明白,我今天寫的這個程序是隨機生成幾個數字,然後將他們轉換成字符串存到文件中,寫出來發現存在文件中的是個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掉!


發佈了105 篇原創文章 · 獲贊 10 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章