2014-03-28 17 views
0

下面的函數需要一串字符和一個數字列表。然後它使用一個循環來根據數字排列字符。問題是,當我使用x和一個while循環打印字符數組時,它會輸出正確的值「Moderation」。當我以相同的方式打印置換值時,它正確地打印正確的數字「4560978123」。但是,無論何時打印字符[0],它都會打印'n'而不是'M'。我完全失去了爲什麼會發生這種情況。C字符數組不工作我如何期待它

char* permute(const char* characters, const int permutationValues[]) { 

    static char decryption[10]; 

    int x = 0; 
    while(x < 10) { 
    decryption[x] = characters[permutationValues[x]]; 
    printf("%c", characters[0]); 

    x++; 
    } 

    return decryption; 
} 

我對C語言相當陌生,所以我希望它只有一個簡單的語法錯誤。

這是我的代碼完整。

#include <stdio.h>  // for I/O functions 
#include <stdlib.h>  // generally useful 
#include <stdbool.h> // for the bool type 
#include <stdint.h>  // for exact-width integer types 
#include <inttypes.h> // for exact-width integer output 

//Permutation function declaration 
char* permute(const char* characters, const int permutationValues[]); 

int main() { 

bool endOfFile = false; //Specifies if the end of the file was found 
int encryption[10]; //This is the array that holds the encryption values 
char text[10]; //The input text array that gets passed to be permuted 
char* cText = text; 
int x = 0; //The loop counter variable 

FILE *cryptFile = fopen("TextIn.txt", "r"); //Open input file in "reading" mode 
FILE *outputFile = fopen("TextOut.txt", "w"); //Open ouput file in "write" mode 

//Reads in the first line of values into the program 
fscanf(cryptFile, "%i %i %i %i %i %i %i %i %i %i", &encryption[0], &encryption[1], &encryption[2], &encryption[3], 
&encryption[4], &encryption[5], &encryption[6], &encryption[7], &encryption[8], &encryption[9]); 

//Writes the first line of data to the outputfile 
fprintf(outputFile, "%3i%3i%3i%3i%3i%3i%3i%3i%3i%3i\n", encryption[0], encryption[1], encryption[2], encryption[3], encryption[4], encryption[5], encryption[6], encryption[7], encryption[8], encryption[9]); 

//Reads in and sets the permutation numbers from the second line into the array 
fscanf(cryptFile, "%i %i %i %i %i %i %i %i %i %i", &encryption[0], &encryption[1], &encryption[2], &encryption[3], 
&encryption[4], &encryption[5], &encryption[6], &encryption[7], &encryption[8], &encryption[9]); 

//Writes the second line, the permutation values, to the outputfile 
fprintf(outputFile, "%3i%3i%3i%3i%3i%3i%3i%3i%3i%3i\n", encryption[0], encryption[1], encryption[2], encryption[3], 
encryption[4], encryption[5], encryption[6], encryption[7], encryption[8], encryption[9]); 

while(!endOfFile) { 
//Pull in characters from file 
int x = 0; 
while(x < 10) { 
    text[x] = fgetc(cryptFile); 
    x++; 
    if(text[x] == EOF) { 
    endOfFile = true; 
    } 
} 

//Permutes the 10 character text array using the given permutation 
cText = permute(text, encryption); //Permutes the text until end of file 

//Prints the permuted results 
fprintf(outputFile, "%c%c%c%c%c%c%c%c%c%c", cText[0], cText[1], cText[2], cText[3], cText[4], cText[5], 
cText[6], cText[7], cText[8], cText[9]); 
} 

fclose(cryptFile); //Close the file 

} 


/* 
* This function takes in a character array of values and a permutation sequence 
* then will return a permuted array of characters. 
* Pre: 
*  Takes a pointer to a character array as the first argument that contains 
*  the text that needs to be permutated. The second argument takes the 
*  permutation values as a pointer to a character array that will be used to 
*  permutate the given character array. 
* Post: 
*  Returns a pointer to a character array that contains the permutated characters. 
*  will return the same size array that was entered. 
*/ 
char* permute(const char* characters, const int permutationValues[]) { 

//DEREFERENCE LATER DO NOT FORGET 
static char decryption[10]; 

int x = 0; 
while(x < 10) { 
decryption[x] = characters[permutationValues[x]]; 

printf("%c", characters[x]); 

    x++; 
} 

return decryption; 
} 

作爲一個說明,我這運行使用GCC在Linux CentOS的發行

回答

3

我複製你的代碼完全一樣,你把它放在an example。它確實打印了字符'M',甚至十次。也許我沒有正確運行這個功能,或者你不是。

#include <stdio.h> 

char* permute(const char* characters, const int permutationValues[]) { 

    static char decryption[10]; 

    int x = 0; 
    while(x < 10) { 
    decryption[x] = characters[permutationValues[x]]; 
    printf("%c", characters[0]); 

    x++; 
    } 

    return decryption; 
} 

int main(void) { 

    int array[] = {4, 5, 6, 0, 9, 7, 8, 1, 2, 3}; 

    permute("Moderation", array); 

    return 0; 
} 

發佈完整的例子,如果它不適合你,我們會幫你。

編輯

#include <stdio.h>  // for I/O functions 
#include <stdlib.h>  // generally useful 
#include <stdbool.h> // for the bool type 
#include <stdint.h>  // for exact-width integer types 
#include <inttypes.h> // for exact-width integer output 

//Permutation function declaration 
char* permute(const char* characters, const int permutationValues[]); 

int main() { 

bool endOfFile = false; //Specifies if the end of the file was found 
int encryption[10]; //This is the array that holds the encryption values 
char text[10]; //The input text array that gets passed to be permuted 
char* cText = text; 
int x = 0; //The loop counter variable 

//Reads in the first line of values into the program 
fscanf(stdin, "%i %i %i %i %i %i %i %i %i %i", &encryption[0], &encryption[1], &encryption[2], &encryption[3], 
&encryption[4], &encryption[5], &encryption[6], &encryption[7], &encryption[8], &encryption[9]); 

//Writes the first line of data to the outputfile 
fprintf(stdout, "%3i%3i%3i%3i%3i%3i%3i%3i%3i%3i\n", encryption[0], encryption[1], encryption[2], encryption[3], encryption[4], encryption[5], encryption[6], encryption[7], encryption[8], encryption[9]); 

//Reads in and sets the permutation numbers from the second line into the array 
fscanf(stdin, "%i %i %i %i %i %i %i %i %i %i", &encryption[0], &encryption[1], &encryption[2], &encryption[3], 
&encryption[4], &encryption[5], &encryption[6], &encryption[7], &encryption[8], &encryption[9]); 

//Writes the second line, the permutation values, to the outputfile 
fprintf(stdout, "%3i%3i%3i%3i%3i%3i%3i%3i%3i%3i\n", encryption[0], encryption[1], encryption[2], encryption[3], 
encryption[4], encryption[5], encryption[6], encryption[7], encryption[8], encryption[9]); 

//while(!endOfFile) { 
//Pull in characters from file 

while(x < 10) { 
    text[x] = fgetc(stdin); 
    printf("captured: %c\n", text[x]); 
    x++; 
    if(text[x] == EOF) { 
    endOfFile = true; 
    } 
} 

//Permutes the 10 character text array using the given permutation 
cText = permute(text, encryption); //Permutes the text until end of file 

//Prints the permuted results 
fprintf(stdout, "%c%c%c%c%c%c%c%c%c%c", cText[0], cText[1], cText[2], cText[3], cText[4], cText[5], 
cText[6], cText[7], cText[8], cText[9]); 
//} 

} 


/* 
* This function takes in a character array of values and a permutation sequence 
* then will return a permuted array of characters. 
* Pre: 
*  Takes a pointer to a character array as the first argument that contains 
*  the text that needs to be permutated. The second argument takes the 
*  permutation values as a pointer to a character array that will be used to 
*  permutate the given character array. 
* Post: 
*  Returns a pointer to a character array that contains the permutated characters. 
*  will return the same size array that was entered. 
*/ 
char* permute(const char* characters, const int permutationValues[]) { 

//DEREFERENCE LATER DO NOT FORGET 
static char decryption[10]; 

int x = 0; 
while(x < 10) { 
decryption[x] = characters[permutationValues[x]]; 

printf("first: %c\n", characters[1]); 

    x++; 
} 

return decryption; 
} 

這實際上有點如你預期它會,檢查出來的作品。

+0

當我用x代替0運行我的代碼時「printf(」%c「,characters [x]);」我得到「溫和適度,永遠是美德」。當我用0代替x運行時,我得到「nnnnnnnn」。我會將我的代碼發佈爲上述問題的編輯。這個問題似乎與我的功能是孤立的。 – cmw2379

+0

無法在沒有輸入文件的情況下對其進行測試。你能否提供我們的文件內容,或者更好的做一個不從文件讀取的例子,只是爲了測試你的功能? – ArthurChamz

+0

這是我使用的確切輸入文件。 https://www.dropbox.com/s/ci56mqg20rerfjw/TextIn.txt – cmw2379

相關問題