:
int remove(const char *filename);
, filename. , .
:
int rename(const char *oldfilename, const char *newfilename);; , . 0 (NULL) .
feof(), <stdio.h>. (1), ( EOF), (0), .
int feof(FILE * filename);: Neg.dat
#include <stdio.h>
#include <stdlib.h>
void main(){
FILE *f_n;
int kol_vo = 0; // -
int value;
randomize();
f_n = fopen(Neg.dat, rb) //
if(!f_n) // 0 ( )
{puts( !\n);
exit(1);
}
while (!foef(f_n)); //
{fread(&value, sizeof(int), 1, f_n);
if(value < 0) kol_vo++;
}
printf( - = %d\n, kol_vo);
fclose(f_n);
}
-
/ <stdio.h> <cstdio>, /. / .2. / . : stdin, stdout, stderr.
2. , .
stdaux | / |
stderr | |
stdin | |
stdout | |
stdprn |
stdin , stdout stderr .
/ :
- ;
- ;
- .
.
/ , . ( ) /. ftell() fgetpos() fseek() fsetpos(). . / (. .3).
|
|
3. / .
fread | |
fwrite | |
getc, fgetc | |
getchar | stdin |
putc, fputc | |
putchar | stdout |
fgets | |
gets | stdin |
fputs | |
puts | stdout |
fscanf | |
scanf | stdin |
sscanf | |
fprintf | |
printf | stdout |
sprintf |
1) -
- :
int fgetc(FILE *fp);fp , .
int fp. , EOF.
int fputc(int c, FILE*fp);fp , ;
c int, .
fp int. , EOF.
.
#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ FILE *f; int c; char *filename="t.txt"; if ((f=fopen(filename,"r"))==0) perror(filename); else while((c = fgetc(f))!= EOF) putchar(c); // fclose(f); system("pause"); return 0;}
2) -
- :
char *fgets(char *s, int n, FILE *f);char *s , ;
int n ;
FILE *f , .
(n-1) '\n'. . '\0'. , 0.
int fputs(char *s, FILE *f);char *s , ;
FILE *f , .
('\0') . EOF, , .
. f1.txt f2.txt.
#include "stdafx.h"#include <iostream>using namespace std;#define MAXLINE 255 // int _tmain(int argc, _TCHAR* argv[]){// in out FILE *in, // *out; // char buf[MAXLINE]; //, in=fopen("f1.txt","r"); // out=fopen("f2.txt","w"); // while(fgets(buf, MAXLINE, in)!=0) // in buf fputs(buf, out); // buf out fclose(in); // fclose(out);// system("pause"); return 0;}3) -
|
|
- :
int fread(void *ptr, int size, int n, FILE *f);void *ptr , ;
int size ;
int n ;
FILE *f , .
, EOF.
int fwrite(void *ptr,int size, int n, FILE *f);void *ptr , ;
int size ;
int n ;
FILE *f , .
, EOF.
.
#include "stdafx.h"#include <iostream>using namespace std;struct Employee {char name[30]; char title[30]; float rate; };int _tmain(int argc, _TCHAR* argv[]){ Employee e; FILE *f; if((f=fopen("text.dat","w"))==NULL) { printf("\n "); } int n; // printf("\n N="); scanf("%d",&n); for(int i=0;i<n;i++) { // printf(":");scanf("%s",&e.name); printf(":");scanf("%s",&e.title); printf(":");scanf("%f",&e.rate); // fwrite(&e,sizeof(Employee),1,f); } fclose(f); // if((f=fopen("text.dat","rb"))==NULL) printf("\n "); while(fread(&e,sizeof(Employee),1,f)) printf("%s, %s, %f;\n", e.name, e.title, e.rate); fclose(f); system("pause"); return 0;}4) -
, .. , . -:
int fprintf(FILE *f, const char *fmt, par1, par2,...);FILE*f , ;
const char *fmt ;
par1, par2,... , .
int fscanf(FILE *f, const char *fmt, &par1, &par2,...);
FILE*f , ;
const char *fmt ;
par1, par2,... , .
.
#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ FILE *f; int n, nn,m; if((f=fopen("int.dat","w"))==0) perror("int.dat"); for(n=1;n<11;n++) fprintf(f, "\n%d %d", n, n*n); fclose(f); if ((f=fopen("int.dat","r"))==0) perror("int.dat"); m=1; while(fscanf(f, "%d %d",&n, &nn)&& m++<11) printf("\n%d %d",n,nn); fclose(f); system("pause"); return 0;3.2.2 :
, \n.
:
-
- .
, (FAT).
|
|
.
, fprintf() fscanf()
/ fprintf() fscanf() printf() scanf(), , .
, .
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main(){
FILE *f;
int dig;
if (f = fopen(inp_f, r) == NULL) //
{ printf( !\n);
exit(0);
}
fscanf(f, %d, &dig); // dig
fclose(f); //
f = fopen(out_f, w);// .
fprintf(f, %d, dig);
fclose(f); //
}
, . . .
: , , .
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void main(int argc, char *argv[]){
FILE *f_in, *f_out;
int ch;
char *name; //
int count = 0; //
if (argc<2) //
{ printf( );
gets(name);
}
else name = argv[1]; //
if ((f_in = fopen(name, r))!= NULL) //
{ strcat(name, .out); // .out
//
f_out = fopen(name, w); //
while((ch = fgetc(f_in)!=EOF)
if(count++%3 == 0)
fputc(ch, f_out); // 3-
fclose(f_in);
fclose(f_out);
}
else printf( !\n);
}
, .
.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void main(int argc, char *argv[]){
FILE *f_in;
char buffer[256]; // - 255
char *name; //
if (argc<2) //
{ printf( );
gets(name);
}
else name = argv[1]; //
if ((f_in = fopen(name, r))!= NULL) //
{ while(fgets(buffer, 255, f_in))!= NULL)
{fputs(buffer, stdout);
fputs(\n, stdout);
}
fclose(f_in);
else printf( !\n);
}
while : fgets() fputs()
, . , , , , , . . fflush(f_out). flushall().
|
|
fclose(f_out) , .. .
. , . , . ( ): 20 , 5 , , 40 .
s, mon () . .
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
FILE *fi, *fo;
if ((fi = fopen(d:\\c\\file.txt, r)) == 0){
cout << ; return 1;}
if ((fo = fopen(d:\\c\\binfile.out, w+b)) == 0){
cout << ; return 1;}
const int dl = 80;
char s [dl];
struct{
char type [20];
int opt, rozn;
char comm[40];} mon;
int kol = 0; //
while (fgets (s, dl, fi)){
//
strcnpy(mon.type, s, 19);
mon.type[19]=\0;
mon.opt = atoi(&s[20]);
mon.rozn = atoi(&s[25]);
strcnpy(mon.comm, &s[30], 40);
fwrite(&mon, sizeof mon, 1, fo);
kol++;
}
fclose(fi);
int i; cin >>i; //
if (i >= kol){ cout << ; return 1;}
// i:
fseek (fo, (sizeof mon)*i, SEEK_SET);
fread (&mon, sizeof mon,1, fo);
cout << type << mon.type <<opt << mon.opt
<< rozn << mon.rozn << endl;
flose (fo);
return 0;
}