Reputation: 61
Given a text file:
I Angelina Jolie 1 7728323
I Mel Gibson 3 7809606 7733889 7724609
I Robert Redford 2 7721170 7731959
I Jennifer Aniston 4 2188989 2189898 2181020 2183456
I Jami Gertz 4 7734404 7774012 7773023 7921492
I Brad Pitt 2 7774017 7878485
R Sylvester Stallone 0
I Victoria Principal 3 7933045 7771234 7820987
R Jennifer Aniston 0
R Sean Penn 0
I Kevin Costner 1 7874014
Q
I need to read each line, separate the values by spaces, and create structs of each one. My current code is:
int main(){
int y;
FILE *data;
char action;
char line[100];
int counter = 0;
int index = 0;
struct number{
int phoneNumber;
struct number *next;
};
struct contact{
char fName[10];
char lName[10];
struct number *start;
};
struct number numbers[50];
struct contact directory[10];
if((data=fopen("hw6data.txt", "r")) != NULL){
while(fscanf(data, "%s", line) != EOF){
char s[2] = " ";
char *token;
token = strtok(line, s);
while(token != NULL){
if(counter==0){
if(s == "I") {
if(counter==1){
strcpy(directory[index].fName, s);
}
if(counter==2){
strcpy(directory[index].lName, s);
}
}
}
token = strtok(NULL, s);
}
}
}
for(y = 0; y < 10; y++){
printf("%s ", directory[y].fName);
printf("%s\n", directory[y].lName);
}
fclose(data);
return 1;
}
I'm trying to create a struct for each phone contact. The I or R indicates whether I should insert the contact or remove it. The directory is an array that contains up to 10 contacts. I can hold a total of 50 numbers. Each contact struct holds a pointer that should point to the first number in the numbers array of number structs. I'm creating an array-based linked list. I thought this code should create the contact structs. It compiles, but when I run it I get:
��f
�
ɷ�
�E
�����
�
��
.N=�
|�X�|���^�
�
Segmentation fault
Help?
Upvotes: 0
Views: 4552
Reputation: 4612
A few problems I can see just at a glance (not necessarily a complete list):
while (fscanf(data, "%s", line) != EOF)
does not read in an entire line at a time (which appears to be your intent, since you named your variable line
). You probably want to do while (fgets(data, 100, line) != NULL)
instead.if (s == "I")
. If you're just checking the first character, you can do if (s[0] == 'I')
(note that single quote marks (''
) are used here to denote a character literal, versus the double quote marks ("")
used to denote string literals.if (counter == 1)
and if (counter == 2)
nested inside if (counter == 0)
, so those conditions will never be true, unless you modify counter
at some point after the if (counter == 0)
and before the if (counter == 1)
.counter
and index
are never being incremented, so your entire while
loop is having no effect whatsoever on the directory
array. This is why you get garbage when you try to print out its values.Upvotes: 0
Reputation: 11963
An example that parse the "I" lines and print what's was read :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int y;
FILE *data;
char action;
char line[100];
int counter = 0;
int index = 0;
struct contact{
char fName[10];
char lName[10];
};
struct contact directory[10];
if((data=fopen("hw6data.txt", "r")) != NULL){
while(fgets(line,sizeof(line),data)){
char s[2] = " ";
char *token = strtok(line, s);
while(token != NULL) {
if(strcmp(token,"I")==0) {
counter = 0;
}
if(counter==1) {
strcpy(directory[index].fName, token);
}
if(counter==2) {
strcpy(directory[index].lName, token);
index++;
}
counter++;
token = strtok(NULL, s);
}
}
}
for(y = 0; y < index; y++){
printf("%s ", directory[y].fName);
printf("%s\n", directory[y].lName);
}
fclose(data);
return 1;
}
If it helps...
Upvotes: 1