. . , , , . .
int fread(void*buf, int bytes, int n, FILE*fptr);
buf , ;
bytes ( );
n , ;
fptr .
, ( n).
int fwrite(void*buf, int bytes, int n, FILE*fptr);
4. ( ), .
# include <stdio.h>
# include <string.h>
void main()
{ FILE *stream;
char msg[ ]=this is a test;
char buf[20];
if ((stream=fopen(DUMMY.FILL,w+))==NULL)
{puts( \n); return;}
//
fwrite(msg, strlen(msg)+1, 1, stream);
//
fseek(stream, 0, SEEK_SET);
//
fread(buf, strlen(msg)+1, 1, stream);
printf(%s \n,buf);
fclose(stream);
}
w+ ( ). . .
int fseek(_ , , _);
, stdio.h:
SEEK_SET ( 0) ;
SEEK_CUR ( 1) ;
SEEK_END ( 2) .
, . , . long.
, . , , . :
int fprintf (__, _, _);
printf () fprintf (). printf () stdin, . , printf () fprintf (stdin, ).
, .
|
|
5. , 1 10. .
//
#include <stdio.h>
#include <iostream.h>
#include <math.h>
void main()
{ FILE *fp;
int x;
fp = fopen(test.dat, w);
//
printf(\t \n);
fprintf(fp, \t \n );
printf(\t x\t\tsqrt(x) \n);
fprintf(fp, \t x\t\tsqrt(x) \n );
\\
\\
for (x = 1; x<=10; x++)
{ printf(\t%f\t%f\n, float(x), sqrt(float(x)));
fprintf(fp, \t%f\t%f\n, float(x), sqrt(float(x)));
}
fclose(fp); }
fscanf (), :
int fscanf(__, _, __);
, .
test.dat, , x y. . test.dat . .
6.
\\
#include <stdio.h>
#include <iostream.h>
#include <math.h>
void main()
{ FILE *fp;
int i;
float x [10], y [10];
fp = fopen(test.dat,r);
for (i = 0; i<10; i++)
{ fscanf(fp, %f%f, &x[i], &y[i]);
printf(%f %f\n, x[i], y[i]);
}
fclose(fp);
}
1. .
2. .
1. .
2. 2 10.
32