나는 모든 종류의 데이터를 저장하는이 구조체를 만들었습니다. 이제 배열 (사용자 *)을 만들고 데이터를 가져 오는 중입니다. 파일이, 이 내가 내 메인 프로그램의 시작에서 할 것입니다 :구조체의 배열을 만들고 파일에서 데이터로드하기
void loadUsers(User** users,int* amount2){
int amount, i = 0, j;
char temp[STRING_SIZE], temp2[STRING_SIZE];
FILE* f;
f = fopen("input.txt", "r");
if (!f){
return;
}
if (fileEmpty(f)){
return;
}
fscanf(f, "%d", &amount);
*amount2 = amount;
*users = malloc(sizeof(User)*amount);
/**users = (User*)malloc(sizeof(User)*amount);*/
while (!feof(f)){
fscanf(f, "%s", temp);
users[i]->ID = (char*)malloc(sizeof(char)*(strlen(temp) + 1));
strcpy(users[i]->ID, temp);
fscanf(f, "%s", temp);
users[i]->f_name = (char*)malloc(sizeof(char)*(strlen(temp) + 1));
strcpy(users[i]->f_name, temp);
fscanf(f, "%s", temp);
users[i]->l_name = (char*)malloc(sizeof(char)*(strlen(temp) + 1));
strcpy(users[i]->l_name, temp);
i++;
}
경우 :
int amount = 0;
User* users = NULL;
// read from file functions
loadUsers(&users,&amount);
그리고 기능 (사용자의 양이 내 txt 파일의 첫 번째 줄입니다) 어떤 이유로 내가 오류가 발생하고 디버깅 중에 할당이 잘못 된 것을 알기 때문에 사용자의 배열 있어야 같은 양만이 1보다
내 표적이 경우에도, users[0]
하지 users[1]
있는 것은 각 셀 사용자 인 배열을 가질 것이다.
어떤 이유가있을 수 있습니까?
편집 : 사용자 구조체 : 이중 포인터를 통해
struct User{
char* ID;
char* f_name;
char* l_name;
int age;
char gender;
char* username;
char* password;
char* description;
char** hobbies;
}typedef User;
당신이 _i 무엇을 의미합니까 정의되지 않은 동작 방지 할 몇 가지 검사와 자신의 코드 [0]'_? –
input.txt는 어떻게 생겼습니까? – timrau
는'User' 구조체를 보여줍니다. –