Ok so what I want to do it store an array of ints, in this case:
Code:
int a1[16] = {5,10,15,20,25,30,35,40,45,50,55,60,65,70,75}
and later read them from file.
So right now I produces the file "data.dat" which is just a .dat file containing "051015202530354045505560657075"
Now I want to read from disk, but am having a problem. How do I read two spaces and store it into an int?
Here is my code with write_File commented out (so that it just reads):
Code:
#include <stdio.h>
#include <stdlib.h>
void write_File(int* a1);
void read_File (void);
int main()
{
int i;
int a1[16] = {5,10,15,20,25,30,35,40,45,50,55,60,65,70,75};
//write_File(a1);
read_File();
return(0);
}
void write_File(int* a1)
{
int i;
for(i=0; i<=14; i++)
{
FILE* fts;
fts = fopen("data.dat", "a");
if (fts)
{
fprintf(fts, "%d" , a1[i]);
fclose(fts);
}
else
{
printf("Error writing to file!\n");
}
}
}
void read_File (void)
{
int i;
int a2[16];
for(i=0; i<=14; i++)
{
FILE* fts;
fts = fopen("data.dat", "r");
if (fts)
{
fread(a2, sizeof(int), 15, fts);
}
else
{
printf("Error reading to file!\n");
}
for(i=0;i<15;i++)
{
printf("%d\n", a2[i]);
}
fclose(fts);
}
}