정말 이해할 수없는 문제가 있습니다. 나는 초보자의 C 프로그래머 해요, 나는 대략 이렇게되면 프로그램이 있습니다문자열에 대한 포인터 배열 - C
void filetostr(FILE *, char *s[]);
void strtofile(char *s[], FILE *);
void XORstr(char *, int);
void XORtext(char *s[], int);
void printext(char *s[]);
int main(int args, char *argv[]) {
char *s[MAXLENGTH];
char ln[MAXLENGTH];
FILE *input, *xorred, *rexorred, *out;
input = fopen("input.txt", "r");
filetostr(input, s);
fclose(input);
printext(s);
XORtext(s, KEY);
}
void filetostr(FILE *fp, char *s[]) {
char ln[MAXLENGTH];
char *p;
int i = 0;
while (fgets(ln, MAXLINE, fp)) {
p = (char *) malloc(strlen(ln) * sizeof(char));
strcpy(p, ln);
s[i++] = p;
}
}
void printext(char *s[]) {
while (*s) {
printf("%s", *s);
s++;
}
}
void XORstr(char *s, int key) {
int c;
while (c = *s)
*s++ = key^c;
}
void XORtext(char *txt[], int key) {
while (*txt) {
XORstr(*txt, key);
txt++;
}
}
그리고 나는이 두 가지 문제가 있습니다 : 나는 문자열에 대한 포인터의 배열을 빌드 할 때,
이- 처음
filetostr
을 사용하면 작동하지만 텍스트 가운데 두 줄이 반복됩니다 (배열에 두 개의 참조가 있으므로printext
과 함께 두 번 인쇄됩니다). 어떻게 가능합니까? malloc에 잘못된 호출이 있습니까? - 둘째, 방금 언급 한 행을 XOR하려고하면 XORred가 한 번만 발생하므로 각 중복 행에 대해 XORred 행과 일반 행으로 끝납니다.
1 인에 의존 할 수
해야한다 'malloc()'함수의 값. 'if (p == NULL) {/ * 메모리 없음 */exit (1); }'와 2)'s' 배열에'p'를 저장할 충분한 공간이 있다면; (i> = MAXLENGTH) {/ * p를 넣을 공간이 없다. * /}'응용 프로그램을 중단시킬 수 있습니다. – Jack