Take a good hard look at how fscanf is being used without the 'file' included in it.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct storagemanipulation
{
char namn[100];
int lagersaldo;
int varunummer;
};
// pass opened file into function for reading into struct array
int readFromFile(FILE *fp, int antal_varor, struct storagemanipulation *inventory)
{
int i = 0;
for ( ; i < antal_varor; i++)
fscanf(fp,"%s%d%d",inventory[i].namn, &inventory[i].lagersaldo, &inventory[i].varunummer);
// actually size gotten, it cannot over ride passed in size to get.
// so a means to actaully know how many records needed to be allowed
//for is needed, or malloc to make extra space if it goes over given size
return i;
}
void saveToFile(FILE *fp, int antal_varor, struct storagemanipulation *inventory)
{ //make sure it is set to
//end of file before writing to it,
//to prevent over writing of data.
fseek(fp, 0, SEEK_END);
for (int i = 0; i < antal_varor; i++)
fprintf(fp, "%s %d %d\n", inventory[i].namn, inventory[i].lagersaldo, inventory[i].varunummer);
}
int main (void)
{
struct storagemanipulation inventory[4];
char file[30], buffer[1024], name[100];
int count = 0, size;
FILE *fp;
//gets user speified file name;
printf("Enter filename\n");
scanf(" %s", file);
//Open for append; open
//(or create if the file does not exist)
//for update at the end of the file.
if ((fp=fopen(file,"a+") ) == NULL )
{
printf("cannot create file\n");
exit(1);
}
// check to see if file is empty
// for first open if it is a fresh file.
// just to play it safe.
fseek (fp, 0, SEEK_END); // go to end of file
size = ftell(fp); // get size
// put back to front of file just in case
// that part all depends on how your file is, if it is a new file,
//already has information in it, and you do not want to over write it
// then you'll need to set it accordingly.
fseek(fp,0,SEEK_SET);
if ( size == 0 )
{
printf("File is empty\n"
"please add data to file\n");
while ( count < 4)
{
printf("enter name, item nuber, and number\n");
fgets(buffer, sizeof buffer, stdin);
scanf(" %s%d%d", strcpy(inventory[count].namn,name), &inventory[count].lagersaldo,
&inventory[count].varunummer);
count++;
}
}
else
readFromFile(fp, 4, inventory);
if ( size > 0)
{
// size_t instead of int
// for poratabiltiy, because sizeof/sizeof returns
// long int on linux, and I've been told it
// can retrun a different data type on others
// so size_t takes care of that.
size_t check = 0;
// returns actual size of array
while ( check < sizeof(inventory)/sizeof(inventory[0]) )
{
printf("Varunummer: Namn %s\nLagersaldo %d varunummer %d\n",inventory[count].namn , inventory[count].lagersaldo, inventory[count].varunummer);
check++;
}
}
else
{
saveToFile(fp, 4, inventory);
for ( int f = 0; f < 4; f++)
printf("%s %d %d\n", inventory[f].namn, inventory[f].lagersaldo,
inventory[f].varunummer);
}
fclose(fp);
return 0;
}
results
Code:
userx@slackwhere:~/bin
$ ./temp2
Enter filename
filedata
Varunummer: Namn name1
Lagersaldo 10 varunummer 10
Varunummer: Namn name1
Lagersaldo 10 varunummer 10
Varunummer: Namn name1
Lagersaldo 10 varunummer 10
Varunummer: Namn name1
Lagersaldo 10 varunummer 10
userx@slackwhere:~/bin
$ ./temp2
Enter filename
filedata1
File is empty
please add data to file
enter name, item nuber, and number
name30 30 30
enter name, item nuber, and number
mane40 40 40
enter name, item nuber, and number
mane50 50 50
enter name, item nuber, and number
mane60 60 60
name30 30 30
mane40 40 40
mane50 50 50
mane60 60 60
userx@slackwhere:~/bin
$