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的發行
當我用x代替0運行我的代碼時「printf(」%c「,characters [x]);」我得到「溫和適度,永遠是美德」。當我用0代替x運行時,我得到「nnnnnnnn」。我會將我的代碼發佈爲上述問題的編輯。這個問題似乎與我的功能是孤立的。 – cmw2379
無法在沒有輸入文件的情況下對其進行測試。你能否提供我們的文件內容,或者更好的做一個不從文件讀取的例子,只是爲了測試你的功能? – ArthurChamz
這是我使用的確切輸入文件。 https://www.dropbox.com/s/ci56mqg20rerfjw/TextIn.txt – cmw2379