#include <stdio.h> FILE *fp; |
#include <stdio.h> FILE *fopen(char *filename, char *mode); |
NOTE:
|
#include <stdio.h>
FILE *fp;
fp = fopen("filename", "r");
|
|
fscanf(fp, "format string", &variable1 [, &variable2, ...] ); |
|
FILE *fp;
char line[100];
int i;
float f;
double d;
fp = fopen("textFile", "r");
fscanf(fp, "%s", line); // line = &line[0]
fscanf(fp, "%d", &i);
fscanf(fp, "%f", &f);
fscanf(fp, "%lf", &d);
|
#include <stdio.h> FILE *fopen(char *filename, "w"); // Overwrite file with new data FILE *fopen(char *filename, "a"); // Append new data to file |
#include <stdio.h>
FILE *fp;
fp = fopen("filename", "w"); // Overwrites (existing) file
fp = fopen("filename", "a"); // Appends to (existing) file
|
fclose(fp); |
fprintf(fp, "format string", &variable1 [, &variable2, ...] ); |
FILE *outputfp;
char line[100];
int i;
float f;
double d;
strcpy(line, "Hello");
i = 4;
f = 3.1415;
d = 2.718281828;
outputfp = fopen("outFile", "w"); // Try changing to "a"
fprintf(outputfp, "%s %d %f %lf", &line[0], i, f, d);
fclose(outputfp);
|
(By induction, that means that you must read all the things preceding something first)
#include <stdio.h> fseek(fp, offset, whence); |
SEEK_SET |
The new position is equal to offset bytes from the beginning of the file |
SEEK_CUR |
The new position is equal to offset bytes from the current read position in the file |
SEEK_END |
The new position is equal to offset bytes from the end of the file |
fscanf(fp, "%d", &i); |
will read something unexpected if the next item is NOT an integer...