fscanf does not work off of EOF it returns what it gets,
Code:
while ( fscanf (stream, "%d%d%d", stuff1,stuff2,stuff3) == 3 ) { do stuff }
why didn't you make a function to pass your file ( stream ) into your function .. it is not exactly you per se' , But I see that a lot, doing everything in the function.it's what I'd call bad program design, and wonder what they are teaching you and the others that pay money to get taught this. When it looks to be no program design is even being introduced into the classes the ones in here are taking.
anyways, just a quick short example
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct MyStrutter
{
char *N;
int id;
}mystrutter;
int MyFunction1(FILE *f, char arrayA[])
{
int i = 0;
char buffer;
while ( (buffer = fgetc(f)) != EOF )
{
arrayA[i] = buffer;
i++;
}
return i;
}
void MyFunction2(mystrutter strut1[], FILE *f)
{
int i = 0;
char name[30];
memset(name, 0, sizeof(name));
while ( fscanf (f, "%s%d",( strut1[i].N = strdup(name)), &strut1[i].id ) == 2)
i++;
}
int main (int argc, const char **argv)
{
if ( argc < 2)
{
printf("no file yo %s\n", argv[1]);
return -1;
}
//Open file once in main, then pass it
//to functions to use it.
FILE *fp;
if ( (fp = fopen(argv[1], "r")) == NULL)
{
printf("FILE NULL\n");
return -1;
}
int size = 0;
char arr[100];
mystrutter strutter1[3];
memset(arr,0,sizeof(arr));
memset(strutter1,0,sizeof(mystrutter));
//returns amount of elements it
//actually used.
size = MyFunction1(fp,arr);
//use that same amount in the loop
for ( int i = 0; i < size; i++)
printf("%c",arr[i]);
//reset stream
fseek(fp,0,SEEK_SET);
printf("\n");
//use same stream in a different function
MyFunction2(strutter1, fp);
//is not an int so used size_t because of method of getting
//size of array.
for ( size_t i = 0; i < sizeof(strutter1)/sizeof(strutter1[0]); i++)
printf("%s %d\n",strutter1[i].N, strutter1[i].id);
fclose(fp);
return 0;
}
// file setup like this
(char) Name {where name is more than one letter} (int) Id
bob 23
sally 55
Jill 292
// or this
bob
23
sally
55
Jill
292
Not that the function cannot be written the way you have it in here, and all of the other post I've seen others have the function open file and take in data. Because rule of thumb is functions are to do one thing. Mains function function is to deal with all of the other functions on top of what else it does.