开发者

How would I output to a file using multiple chars

开发者 https://www.devze.com 2023-02-01 09:03 出处:网络
#define numeric_b \'0\' #define numeric_e \'9\' /** init string intervals ---*/ static char c0=numeric_b;
#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;}
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}


Sorry @zartag but this is some seriously obfuscated code. Please just tell us in a paragraph what you're trying to do and what you think your code is doing.

The most obvious thing I can see wrong with your code with respect to the question title ("output to a file") is that you are using printf instead of fprintf. They behave almost identically, except that printf prints to standard output, and fprintf prints to a file stream (e.g. to your list.txt). See the documentation on fprintf. In your case it should be

FILE * fp = fopen("list.txt","w");

switch(length){
    case 0:
        fprintf(fp, "%c\n",c0);break;
    ..snip

But seriously that code is in dire need of refactoring (e.g. it looks like the whole switch block can be replaced with a for loop). And please, when you ask a question here, give us a little more to go on than a code listing and question title.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号