Try this
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MEMBERS 10
#define MEM_NAME_LEN 20
#define MEM_ADDR_LEN 20
typedef struct {
int membernumber;
char membername[MEM_NAME_LEN];
char memberaddress[MEM_ADDR_LEN];
int membervisits;
} mem_st;
mem_st MemberData[MAX_MEMBERS];
int main() {
char buff[BUFSIZ];
int i, counter1 = 0;
FILE *fp;
/* open the file, check for success */
fp = fopen( "sample.txt", "r" );
if ( fp == NULL ) {
perror( "Cant open" );
exit( 1 );
}
/* read all members, or until the array is full */
while ( counter1 < MAX_MEMBERS &&
fgets( buff, BUFSIZ, fp ) != NULL ) {
sscanf( buff, "Mem%d", &MemberData[counter1].membernumber );
/* read name and address directly into the array */
/* if you need to remove the newline, add the code */
/* if the fgets fails, we've reached the end of file unexpectedly */
if ( fgets( MemberData[counter1].membername, MEM_NAME_LEN, fp ) == NULL ) break;
if ( fgets( MemberData[counter1].memberaddress, MEM_ADDR_LEN, fp ) == NULL ) break;
/* read and scan the number of visits */
if ( fgets( buff, BUFSIZ, fp ) == NULL ) break;
sscanf( buff, "%d", &MemberData[counter1].membervisits );
/* successfully read a complete record */
counter1++;
}
fclose( fp );
for ( i = 0 ; i < counter1 ; i++ ) {
printf( "%d %s %s %d\n",
MemberData[i].membernumber,
MemberData[i].membername,
MemberData[i].memberaddress,
MemberData[i].membervisits );
}
return 0;
}
> Whats wrong with using feof? i was told on this board to use it!
Nothings wrong, it just doesn't tell you what you want to know, so its the wrong function to use in the context in which you were using it.