파일에서 구조체 배열의 요소로 읽은 C 문자열을 복사하려고하는데 복사하지 않습니다. 인쇄를 시도 할 때 그 단어가 없습니다. 나는 C에 익숙하다. 내 코드는 다음과 같다. 당신의 도움에 많은 감사드립니다.문자열을 구조체 배열의 요소에 복사합니다.
typedef struct Tree{
int numTimes; //number of occurrences
char* word; //the word buffer
}Node;
#include "proj2.h"
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]){
FILE* readIn; //read file pointer
FILE* writeOut; //write file pointer
char buffer[18]; //allocate buffer ***please do not fuzz
int length = 0;
int count = 0;
Node* array = (Node*) malloc(sizeof(Node));
/*if(argc < 3){ //if the number of command line arguments is < 3, return EXIT_FAILURE
return EXIT_FAILURE;
}*/
argv[1] = "/Users/magnificentbastard/Documents/workspaceCPP/proj2/Password.txt"; //****testing
argv[2] = "outFile.txt"; //****testing
readIn = fopen(argv[1], "r"); //opens the selected argument file for reading
writeOut = fopen(argv[2], "w"); //opens the selected argument file for writing
if(readIn == NULL){ //if there
printf("ERROR: fopen fail.\n");
return EXIT_FAILURE; //exits if the file opens
}
while(fscanf(readIn, "%18s", buffer) == 1){ //loop to read in the words to the buffer
count++; //counts the words coming in
modWord(buffer); //modifies the words coming in
array = (Node*)realloc(array, sizeof(Node));
for(int i = 0; i < count; i++){ //****not copying over...HELP
strcpy(array[i].word, buffer);
}
}
//Node array[count];
fprintf(stderr, "%d ", count); //***for testing purposes only
int elements = sizeof(array)/sizeof(array[0]); //***testing assigns num elements
fprintf(stderr, "%d ", elements); //***testing prints num elements
fclose(readIn); //closes the in-file
fclose(writeOut); //closes the out-file
return EXIT_SUCCESS;
}
불확실한 동안 자동 저장 기간이있는 객체의 값을 사용하는 경우 정의되지 않은 동작입니다. – EOF
@ JohnColeman 원래 코드에서 단어에 대한 포인터가 있었지만 복사하지 않았습니다. 그건 내 문제가 아니야. –
@ JohnColeman 내 구조체에 문자 버퍼를 가지고 있지만 strcpy를 시도했지만 복사하지 않았습니다. –