Thread: retreiving variable from within loop.

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Hmm, the program compiles when I fix this error:
    Code:
    char key[16] = "aeiluozcgraolifi";
    //27 initializer-string for array of chars is too long
    Other than that there may be better ways to get the size of the file than getting 150000 characters and then counting how many there actually is.

    And then
    Code:
    for(x = 0; x < count; x++){
                encrypt[x] = file[x] ^ key[x];
                }
    The length of key was only 16. On my particular run the program didn't crash, but since there apparently were 0-bites beyond key, only the beginning of the text got encrypted.
    I would use x%KEYSIZE to get the index for key[] in the loop.
    Last edited by anon; 08-04-2006 at 04:39 PM.

  2. #17
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Alright, Ill try these suggestions, thanks.

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    So I gather you're experimenting with XOR encryption? (Sometimes it's easier -- and better -- to state the problem, and/or the "big picture", rather than perceived issues with assumed solutions.) Anyways, a number of your latest posts seems to point me toward that.

    And it's been kinda slow, and I've never written one that I can remember, so I decided to play along at home. Here is something to compare with after you've finished or as you continue developing your code. I'm sure others will be along soon to nitpick it as well.
    Code:
    #include <stdio.h>
    /*
     * argv[1] - name of input file to encrypt/decrypt
     * argv[2] - name of output file
     * argv[3] - crypt key
     */
    int main(int argc, char *const *argv)
    {
       if ( argc == 4 )
       {
          FILE *input  = fopen(argv[1], "rb");
          FILE *output = fopen(argv[2], "wb");
          if ( input != NULL && output != NULL )
          {
             unsigned char buffer[BUFSIZ];
             size_t count, i, j = 0;
             do {
                count = fread(buffer, sizeof *buffer, sizeof buffer, input);
                for ( i = 0; i < count; ++i )
                {
                   buffer[i] ^= argv[3][j++];
                   if ( argv[3][j] == '\0' )
                   {
                      j = 0; /* restart at the beginning of the key */
                   }
                }
                fwrite(buffer, sizeof *buffer, count, output);
             } while ( count == sizeof buffer );
             fclose(input);
             fclose(output);
          }
       }
       return 0;
    }
    Doing something like this...
    H:\Forums>xorcrypt main.c file.bin key
    H:\Forums>xorcrypt file.bin file.txt key
    You should find that main.c matches file.txt.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    for(x = 0; x < count; x++){
                encrypt[x] = file[x] ^ key[x];
                }
                printf("%s", encrypt);
    KABLAM.

    Surely one of the XOR operations will produce zero, by chance. encrypt[x] is non-zero up to a certain x. You will only see the first x characters printed. After that, well, there goes. printf() will die there and then because it has encountered a zero character.

    Listen everyone, it gets better...

    And what if no zero character is produced? Then printf() will print all 150,000 bytes of encrypt and perhaps more, until it does reach a zero.


    Toodles.
    ~
    Last edited by jafet; 08-05-2006 at 08:28 PM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #20
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    so I should use puts()?

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jafet
    Code:
    for(x = 0; x < count; x++){
                encrypt[x] = file[x] ^ key[x];
                }
                printf("%s", encrypt);
    KABLAM.

    Surely one of the XOR operations will produce zero, by chance. encrypt[x] is non-zero up to a certain x. You will only see the first x characters printed. After that, well, there goes. printf() will die there and then because it has encountered a zero character.
    No it won't:
    Code:
    #include<stdio.h>
    int main( void )
    {
        char foo[] = { 0 };
        printf("%s", foo );
        return 0;
    }
    It will simply print nothing. A string is defined as zero or more characters, terminated by a null character. If all you have is the null, you have what is considered an empty string. It's still a legal string. Thus, any Standard C function which handles strings, will still be able to handle it.
    Quote Originally Posted by jafet
    And what if no zero character is produced? Then printf() will print all 150,000 bytes of encrypt and perhaps more, until it does reach a zero.
    So? As long as you're not running off the end of your allocated space, who cares how much it prints? It's still legal, assuming you are in fact staying in bounds of your allocated space.


    Quzah.
    Last edited by quzah; 08-06-2006 at 08:47 AM.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Yep, so printf only prints part of the string... that's prolly why the OP thought the rest of the data had "vanished"...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop through a 2D array - just for select rows
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 08:34 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Loop variable
    By anirban in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 10:36 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. for loop not letting me input variable data
    By RealityFusion in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 04:29 PM