Ich versuche eine C-Zeichenfolge zu kopieren, die aus einer Datei in ein Element eines struct-Arrays eingelesen wird, aber nicht kopiert. Wenn ich versuche zu drucken, ist das Wort nicht da. Ich bin etwas neu in C. Unten ist mein Code. Vielen Dank für deine Hilfe.Kopiere Zeichenkette in Element des struct Arrays
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;
}
Nicht definiertes Verhalten für die Verwendung des Werts eines Objekts mit automatischer Speicherdauer, während es unbestimmt ist. – EOF
@JohnColeman Ich hatte einen Zeiger auf Word in meinem ursprünglichen Code, es hat einfach nicht kopiert. Das war nicht mein Problem. –
@JohnColeman Ich habe versucht, einen Zeichenpuffer in meiner Struktur und gerade strcpy als auch, aber es kopiert nicht. –