weixin_51175773 2021-03-13 16:04 采纳率: 40%
浏览 16

想问这段代码的意思,希望能一句句解释

int seq_gen(void){
    FILE* fd=fopen("./1000.txt","w");
    char* scope[]={"-1","0","1"};
    srand((unsigned)time(NULL));
    int i; 
    for( i=0;i<LEN;i++){
        fprintf(fd,"%s\n",scope[rand()%3]);
    }
    fclose(fd);
    return 0;
    
}

  • 写回答

2条回答 默认 最新

  • cpp_learners 2021-03-13 16:43
    关注

    差不多是这个意思。。。 

    int seq_gen(void) {
    
    	FILE* fd = fopen("./1000.txt", "w");	// 以写的方式打开1000.txt文件
    
    	//char* scope[] = { "-1","0","1" };		// 这样写有问题!!!
    	char scope[] = { '-1', '0', '1' };		// 定义字符数组,将字符1、字符0、字符1存储
    
    	srand((unsigned)time(NULL));	// 随机函数,参数随机值
    
    	int i;
    
    	for (i = 0; i < LEN; i++) {	// 循环LEN次
    		fprintf(fd, "%s\n", scope[rand() % 3]);	// 将scope字符数组中的字符按照 随机函数产生的 随机值 % 3 得到的索引,取得的字符写入文件中。
    	}
    
    	fclose(fd);	// 关闭文件
    
    	return 0;
    
    }
    评论

报告相同问题?