Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 19th, 2004, 12:04 AM   #1
Spaam
Newbie
 
Join Date: Oct 2004
Posts: 2
Rep Power: 0 Spaam is on a distinguished road
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;
}
Spaam is offline   Reply With Quote
Old Oct 19th, 2004, 3:08 AM   #2
Daggerhex_Flynn
Programmer
 
Join Date: Oct 2004
Location: Canada
Posts: 82
Rep Power: 5 Daggerhex_Flynn is on a distinguished road
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);
 }
}
Daggerhex_Flynn is offline   Reply With Quote
Old Oct 19th, 2004, 9:44 AM   #3
mici
Programmer
 
Join Date: Oct 2004
Posts: 67
Rep Power: 5 mici is on a distinguished road
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.
mici is offline   Reply With Quote
Old Oct 25th, 2004, 11:04 PM   #4
Spaam
Newbie
 
Join Date: Oct 2004
Posts: 2
Rep Power: 0 Spaam is on a distinguished road
Thanks a lot for the answers. Problem solved.
Spaam is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:30 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC