Writing to file is really simple in C there's only three functions(fopen, fprintf, fclose) which you should know and one structure(FILE). This code snippet creates a file with 0 - 100 lines of numbers.
GeSHi (c):
#include <stdio.h>
int main(int argc, char **argv) {
FILE *fp;
char *filename = "database";
// Open file for writing
fp = fopen(filename, "w");
// Check for problems with opening file
if (fp == NULL) {
printf("Problem with opening file %s\n", filename
);
return 1;
}
int i;
for (i = 0; i <= 100; i++) {
// Write to file similar to function printf
fprintf(fp, "%d\n", i);
}
fclose(fp);
return 0;
}
Created by GeSHI 1.0.7.20
And the reading is also easy new function is scanf() and constant EOF.
GeSHi (c):
#include <stdio.h>
int main(int argc, char **argv) {
FILE *fp;
char *filename = "database";
// Open file for reading
fp = fopen(filename, "r");
// Check for problems with opening file
if (fp == NULL) {
printf("Problem with opening file %s\n", filename
);
return 1;
}
int temp;
// EOF stands for end of file, so it checks if
// it's not already in the end
while(fscanf(fp, "%d", &temp) != EOF) {
// Make some arithmetics
temp += 10;
}
// Close file
fclose(fp);
return 0;
}
Created by GeSHI 1.0.7.20