user1781966
user1781966

Reputation:

Reading a file into a C program

The program I have reads the file, displays it accordingly, but then when trying to view the contents again, they are gone.

Structure:

typedef struct friends_contact {
    char *First_Name;
    char *Last_Name;
    char *home;
    char *cell;
} fr;

Main:

int main() {
    fr friends[5];
    char buffer[BUFFSIZE];
    int counter = 0;
    int i = 0;

    menu(friends, &counter, i, buffer);

    getch();
    return 0;
} 

If statement in the menu function used to open up the file:

if (user_entry1 == 1) {
  printf("Please enter a file name");
  scanf("%s", user_entry3); 

  read = fopen(user_entry3, "r");
}

If statement that allows the user to choose to look at the contacts of the address book.

if (user_entry == 4 ) {
  print_contact(friends, counter, i, user_entry3);
  if (user_entry1 == 1) {
    file2(friends, counter, i, buffer, read);
  }
} 

Read function:

char file2(fr *friends, int *counter, int i, char buffer[], FILE *read) {
  while (fscanf(read, "%s", buffer) != EOF) {
    friends[i].First_Name = malloc(BUFFSIZE * strlen(buffer));
    strcpy(friends[i].First_Name, buffer);
    return printf("\n""%s ", friends[i].First_Name);       
  }  
  fclose(read);
}

If you need me to provide any more code. I'm trying to get it to read the content from the file into the contacts list so I can view, search, or delete them.

P.S. I understand some of the other code may not be perfect, but right now I'm just concerned about figuring this out :).

Upvotes: 2

Views: 445

Answers (3)

Sedat Kapanoglu
Sedat Kapanoglu

Reputation: 47640

You have to reset your file pointer using fseek to the beginning of the file again. Your read function should start like:

fseek(read, 0, SEEK_SET); // set file pointer to the beginning of the file

Upvotes: 1

Adam Liss
Adam Liss

Reputation: 48280

The file2 function reads each line of the file into friends[i] but never increments i. So each new record would overwrite the previous one ... except that the function returns during the first iteration of the loop. Better to use if than while in this case.

Other suggestions:

Instead of using }else; you can just close the if block with }. An if needn't be followed by else.

Instead of the idiom

if (foo == 1) {
  // do stuff
} else if (foo == 2) {
  // do other stuff
} else {
  // do default stuff
}

you can use

switch (foo)  {
case 1:
  // do stuff
  break;
case 2:
  // do other stuff
  break;
default:
  // do default stuff
}

Instead of

printf("\n""%s ",friends[i].First_Name);

it's more common to use a single string, with the newline at the end:

printf("%s\n", friends[i].First_Name);

Upvotes: 2

Alberto Bonsanto
Alberto Bonsanto

Reputation: 18022

You shouldn't forget to test if the file could be opened or not:

if ( user_entry1==1 ){

    printf( "Please enter a file name" );
    scanf( "%s", user_entry3 ); 

    if ( ( read = fopen( user_entry3, "r" ) ) != NULL ){
        printf( "The file couldn't be opened" );
    }
    else{
       .
       .
       .
    }
}

Upvotes: 0

Related Questions