![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2004
Posts: 2
Rep Power: 0
![]() |
I am trying to read a text file into a structure of arrays. Every second line should go into different members of the structure, name and number. However, as soon as I try to assign a value to any member, I get a segmentation error.
Strangely I can get this to work using only ONE member, it's when I use two it stops working. Second question. At first I tried declaring the structure members as "char name[]" and "char name[100]" but this didn't work. For some reason I CAN however treat these char pointers as if they were arrays, which they are not (if I use only one...) how come? Any input into as to why I get a segmentation error or general tips as to how I might better be able to solve this problem would be greatly appreciated. I am at a total loss. Thanks. ps. this function is part of a bigger program. Obviously the function is pointless as it is, I just need to get this thing working before I can write anything else. It gets called as as follows: "read_file(&fp, dbcontacts);" fp being a file pointer and dbcontacts the structure Code follows: #include <stdio.h> #define TRUE 1 #define FALSE 0 #define MAX 200 typedef struct { char *name; char *number; } database; void read_file(FILE *fp, database dbcontacts[]) { int c, t_names=0, t_chars=0, read_name=TRUE, t_numbers=0, t_digits=0; while ((c = getc(fp)) != EOF) { printf("\ndata: %s", dbcontacts[0].name); if (read_name == FALSE && c != '\n') { dbcontacts[t_numbers].number[t_digits] = c; t_digits++; } if (read_name == FALSE && c == '\n') { dbcontacts[t_numbers].number[t_digits] = '\0'; t_digits = 0; t_numbers++; read_name = TRUE; } if (read_name == TRUE && c != '\n') { dbcontacts[t_names].name[t_chars] = c; t_chars++; } if (read_name == TRUE && c == '\n') { dbcontacts[t_names].name[t_chars] = '\0'; t_chars = 0; t_names++; read_name = FALSE; } } printf("\n"); return; } |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Oct 2004
Location: Canada
Posts: 82
Rep Power: 5
![]() |
You always have to show your datafile for IO questions, so I made an arbitrary example of a file with fixed width fields:
MakeSureTo 422 LeaveTwelve 433 SpacesFor 432 EachName 455 Field 663 #include<stdio.h>
#define SIZE 20
struct dataBase_t {
char name [13];
int number;
};
void readFile( FILE *, struct dataBase_t [] );
int main() {
FILE *fptr = fopen("datafile","r");
struct dataBase_t database[SIZE];
readFile(fptr, database);
fclose(fptr);
return 0;
}
void readFile( FILE * fptr, struct dataBase_t database[] ) {
char buffer[255];
int i=0;
for (i = 0; (fgets(buffer, 255, fptr)) != NULL && i < SIZE; ++i) {
sscanf(buffer, "%12s%d",database[i].name, &database[i].number);
printf("%s\n%d\n",database[i].name, database[i].number);
}
} |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2004
Posts: 67
Rep Power: 5
![]() |
Well as far as i can see your code lacks memory handling...
That is you've declared pointers in the struct... but you didn't give them room to fill. use something like dbcontacts[t_names].name =malloc(num_of_chars_max); or if you need to modify size of theese memory arreas you can later us realloc(); or in the end just go C++ and use vectors.
__________________
coffee is my heroin. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2004
Posts: 2
Rep Power: 0
![]() |
Thanks a lot for the answers. Problem solved.
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|