0
#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;}
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
1
  • 2
    Is this a question or something? Commented Dec 26, 2010 at 4:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

the purpose of this is to output every combination of a character set in to a file. this code is a result of a algorithm that i came up with.
lets start with 3 char intervals interval[a],[b],[c]. now output the initialized intervals with their starting char for example i will use numeric ('0'-'9')so the starting char = '0'.i than loop interval[c] until it reaches the ending char which equals to '9'. when [c] reaches ending == en than [c] = starting and [b] than increments and keeps a repeated loop until how many intervals have been reached.constantly repeat a loop of printf()s but every output is different from the one before. now how would i output all the possible outcomes to 1 signal file while having a newline after every result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.