如何使用多个字符输出到文件 #定义数字_b“0” #定义数字_e“9” /**初始化字符串间隔---*/ 静态字符c0=数字字符; 静态字符c1=数字字符; 静态字符c2=数字字符; 静态字符c3=数字字符; 静态字符c4=数值_b; 静态字符c5=数值_b; 静态字符c6=数字字符; 静态字符c7=数字字符; /**初始化开始和结束----------------*/ 静态常量char en=数值_e+1; 静态常量字符st=数值_b+1; 空字符串长度(整数长度){ 文件*fp=fopen(“list.txt”,“w”); 开关(长度){ 案例0: printf(“%c\n”,c0);中断; 案例1: printf(“%c%c\n”,c0,c1);中断; 案例2: printf(“%c%c%c\n”,c0、c1、c2);中断; 案例3: printf(“%c%c%c%c\n”,c0、c1、c2、c3);中断; 案例4: printf(“%c%c%c%c%c\n”,c0、c1、c2、c3、c4);中断; 案例5: printf(“%c%c%c%c%c%c%c\n”,c0、c1、c2、c3、c4、c5);中断; 案例6: printf(“%c%c%c%c%c%c%c%c\n”,c0、c1、c2、c3、c4、c5、c6);中断; 案例7: printf(“%c%c%c%c%c%c%c%c\n”,c0、c1、c2、c3、c4、c5、c6、c7);中断; } fclose(fp); } 无效排列(整数长度){ 虽然(c0
Sorry@zartag),但这是一些严重混淆的代码。请在一段中告诉我们您正在尝试做什么,以及您认为您的代码正在做什么 关于问题标题(“输出到文件”),我能看到的代码中最明显的错误是,您使用的是如何使用多个字符输出到文件 #定义数字_b“0” #定义数字_e“9” /**初始化字符串间隔---*/ 静态字符c0=数字字符; 静态字符c1=数字字符; 静态字符c2=数字字符; 静态字符c3=数字字符; 静态字符c4=数值_b; 静态字符c5=数值_b; 静态字符c6=数字字符; 静态字符c7=数字字符; /**初始化开始和结束----------------*/ 静态常量char en=数值_e+1; 静态常量字符st=数值_b+1; 空字符串长度(整数长度){ 文件*fp=fopen(“list.txt”,“w”); 开关(长度){ 案例0: printf(“%c\n”,c0);中断; 案例1: printf(“%c%c\n”,c0,c1);中断; 案例2: printf(“%c%c%c\n”,c0、c1、c2);中断; 案例3: printf(“%c%c%c%c\n”,c0、c1、c2、c3);中断; 案例4: printf(“%c%c%c%c%c\n”,c0、c1、c2、c3、c4);中断; 案例5: printf(“%c%c%c%c%c%c%c\n”,c0、c1、c2、c3、c4、c5);中断; 案例6: printf(“%c%c%c%c%c%c%c%c\n”,c0、c1、c2、c3、c4、c5、c6);中断; 案例7: printf(“%c%c%c%c%c%c%c%c\n”,c0、c1、c2、c3、c4、c5、c6、c7);中断; } fclose(fp); } 无效排列(整数长度){ 虽然(c0,c,file,generator,C,File,Generator,Sorry@zartag),但这是一些严重混淆的代码。请在一段中告诉我们您正在尝试做什么,以及您认为您的代码正在做什么 关于问题标题(“输出到文件”),我能看到的代码中最明显的错误是,您使用的是printf而不是fprintf。它们的行为几乎相同,只是printf打印到标准输出,而fprintf打印到文件流(例如,到您的list.txt).见。你的情况应该是这样的 #define numeric_b '0' #define numeric_e '9' /** init string interv
printf
而不是fprintf
。它们的行为几乎相同,只是printf打印到标准输出,而fprintf打印到文件流(例如,到您的list.txt).见。你的情况应该是这样的
#define numeric_b '0'
#define numeric_e '9'
/** init string intervals ---*/
static char c0=numeric_b;
static char c1=numeric_b;
static char c2=numeric_b;
static char c3=numeric_b;
static char c4=numeric_b;
static char c5=numeric_b;
static char c6=numeric_b;
static char c7=numeric_b;
/** init start & end ----------------*/
static const char en = numeric_e +1;
static const char st = numeric_b +1;
void str_in(int length){
FILE * fp = fopen("list.txt","w");
switch(length){
case 0:
printf("%c\n",c0);break;
case 1:
printf("%c%c\n",c0,c1);break;
case 2:
printf("%c%c%c\n",c0,c1,c2);break;
case 3:
printf("%c%c%c%c\n",c0,c1,c2,c3);break;
case 4:
printf("%c%c%c%c%c\n",c0,c1,c2,c3,c4);break;
case 5:
printf("%c%c%c%c%c%c\n",c0,c1,c2,c3,c4,c5);break;
case 6:
printf("%c%c%c%c%c%c%c\n",c0,c1,c2,c3,c4,c5,c6);break;
case 7:
printf("%c%c%c%c%c%c%c%c\n",c0,c1,c2,c3,c4,c5,c6,c7);break;
}
fclose(fp);
}
void permute(int length){
while(c0<=en){
str_in(length);
c0++;
if(c0==en && length==0){break;}
if(c0==en){
c0=st;
c1++;
if(c1==en && length==1){break;}
if(c1==en){
c1=st;
c2++;
if(c2==en && length==2){break;}
if(c2==en){
c2=st;
c3++;
if(c3==en && length==3){break;}
if(c3==en){
c3=st;
c4++;
if(c4==en && length==4){break;}
if(c4==en){
c4=st;
c5++;
if(c5==en && length==5){break;}
if(c5==en){
c5=st;
c6++;
if(c6==en && length==6){break;}
if(c6==en){
c6=st;
c7++;
if(c7==en && length==7){break;}
}
}
}
}
}
}
}
}
}
但严重的是,代码急需重构(例如,看起来整个开关块都可以用for循环替换)。当你在这里提问时,请给我们更多的信息,而不是代码列表和问题标题。这是一个问题还是什么?其目的是将字符集的每个组合输出到一个文件中。此代码是我提出的一个算法的结果。让我们从3个字符间隔[a],[b],[c]开始。现在输出初始化的间隔及其起始字符。例如,我将使用数字('0'-'9'),因此起始字符='0'。我将使用循环间隔[c],直到它到达等于'9'的结束字符。当[c]到达结束==en时,将使用[c]=开始和[b]然后递增并保持一个重复循环,直到达到多少间隔。不断重复printf()s的循环,但每个输出都不同于之前的输出。现在,如何在每个结果后有一个换行符的同时将所有可能的结果输出到一个信号文件
FILE * fp = fopen("list.txt","w");
switch(length){
case 0:
fprintf(fp, "%c\n",c0);break;
..snip