.


:




:

































 

 

 

 


. ++




 

++ . .

 

3.2

 

ϳ (. 3.3), , , : ++, ѳ: , .

ѳ , , . , a 3×3 : double a[3][3];

' : 0, 1, 2. a[i], i , i - double*. , , a[i][j] a i j. - (, ).

. : , . ѳ , 䳿 .

, . ' new. ' . m n . ' new:

 

double *a;

int m, n;

...

a = new double[m * n];

 

: 0, 1 .., m - 1. n , , i j : i * n + j. ij, , i , , i × n , , j . , i j : a[i * n + j].

. , '. -, , 䳿; -, -', ' .

. , 4 × 4 × 2 : double a[4][4][2]; x, y, z : a[x][y][z].

ѳ .

FILE. , ' typedef " stdio.h ". :

FILE *fopen(const char *path, const char *mode);

path (, ' ), mode .

mode :

r ³
w ³ . , .
a ³ . , .
t ³ .
b ³ .
+ , .

 

ʳ :

FILE *f, *g, *h;...// 1. ³ "abcd.txt" f = fopen("abcd.txt", "rt");// 2. ³ "c:\Windows\Temp\tmp.dat"// g = fopen("c:/Windows/Temp/tmp.dat", "wb+");// 3. ³ //"c:\Windows\Temp\abcd.log"// h = fopen("c:\\Windows\\Temp\\abcd.log", "at");

 

fopen FILE, . . (, ) . errno, " errno.h ", . , . :

#include <stdio.h>#include <errno.h>...FILE *f = fopen("filnam.txt", "rt");if (f == NULL) { printf(" %d\n", errno);...}

fopen FILE. , , . " stdio.h " NULL void:

#define NULL ((void *) 0)

 

NULL , , .

perror, . , - , perror. :

#include <stdio.h>...FILE *f = fopen("filnam.txt", "rt");if (f == 0) { perror(" ");...}

, , . . fread fwrite. , ( , ): , ' '.

fread :

size_t fread(char *buffer, // size_t elemSize, // size_t numElems, // FILE *f // FILE);

 

size_t . numElems , f FILE, elemSize. , , numElems, . f fopen . fread:

FILE *f;double buff[100];size_t res;f = fopen("tmp.dat", "rb"); // ³ if (f == 0) { // // perror(" "); exit(1); // 1}// 100 res = fread(buff, sizeof(double), 100, f);// res

 

" tmp.dat " , 100 8 . fread , 100.

fread '. ! , . .

fwrite fread. :

size_t fwrite(char *buffer, // size_t elemSize, // size_t numElems, // FILE *f // FILE);

 

, , numElems, , . fwrite:

FILE *f;double buff[100];size_t num;...f = fopen("tmp.res", "wb"); // ³ "tmp.res"if (f == 0) { // // perror(" "); exit(1); // 1}// 100 res = fwrite(buff, sizeof(double), 100, f);// res == 100

 

' . , , , . (, .) , . ' ( -'), . fclose :

int fclose(FILE *f);

 

fclose , - (, EOF, ). perror, :

FILE *f;f = fopen("tmp.res", "wb"); // ³ "tmp.res"if (f == 0) { // // perror(" "); exit(1); // 1}...// if (fclose(f) < 0) { // perror(" ");

 

fread, ' ', fscanf .

fprintf . .

fread :

%d ֳ int
%lf ij double
%c char
%s . , ' \n '. . ʳ .

 

fscanf:

int n, m; double a; char c; char str[256]; FILE *f;...fscanf(f, "%d", &n); // fscanf(f, "%lf", &a); // fscanf(f, "%c", &c); // fscanf(f, "%s", str); // ( // ) fscanf(f, "%d%d", &n, &m); //

 

:

%d
%10d , 10 ,
%lf double
%.3lf double
%12.3lf double , 12
%c
%s , . ʳ

 

fprintf :

 

int fprinf(FILE *f, const char *format,...);

 

Windows . . . . , .. | . :

ab | cd | ef

 

, (, , ). , > <, . , :

abcd > tmp.res

 

abcd " tmp.res ", . ³, :

abcd < tmp.dat

 

abcd " tmp.dat " . :

abcd < tmp.dat > tmp.res

 

, : " tmp.dat ", " tmp.res ".

ѳ . FILE *. :

stdin . ;

stdout . ;

stderr . ³ .

stdin, stdout, stderr , " stdio.h ". - , , :

fscanf(stdin, "%d", &n);

 

n . :

fprintf(stdout, "n = %d\n", n);

 

n . :

fprintf(stderr, " \n");

 

stderr, , , . perror stderr.

, . > 䳺 . , :

abcd > tmp.res

 

abcd " tmp.res ", , , . " tmp.log " ,

abcd 2> tmp.log

 

( > !). , . 0, 1, 2. stderr, stdout, , . :

abcd 2> tmp.log > tmp.res

 

, , .

, , , , , - ѳ scanf printf.

- ѳ sscanf sprintf , ( ), ' '. sscanf sprintf . , :

char txt[256] = "-135.76"; double x;sscanf(txt, "%lf", &x);

 

, txt, , x. , :

char txt[256]; int x = 12345;sprintf(txt, "%d", x);

 

x txt, " 12345 ", .

- ѳ -. :

-
int fgetc(FILE *f); f
int fputc(int c, FILE *f); f
-
char *fgets(char *line,int size, FILE *f); f
char *fputs(char *line, FILE *f); f

 

int fseek(FILE *f, long offset, int whence); f
long ftell(FILE *f); f
int feof(FILE *f); , f

 

.

, , . , , .

1. , 20 . .

 

2. , 15 .

 

3. , 21 .

 

4. , 17 : , 䳿 .

 

5. , 20 :

 

䳿:

 

6. , 25 : 䳿 , .

 

7. , 20 : 䳿: .

 

8. , 17 : 䳿 : .

 

9. , 18 : . .

 

10. , 20 : .

 

11. , 20 . . - , .

 

12. , 25 , . .

 

13. , 30 . 0, , .

 

14. , 17 : , 䳿 .

 

15. , 20 :

 

䳿:

 

16. , 25 : 䳿 , .

 

17. , 50 : 䳿: .

 

18. , 20 : 䳿 : .

 

19. , 18 : . .

 

20. , 20 : .

 

21. , 20 :

 

䳿: - .

 

22. , 24 : 䳿 , .

 

23. , 20 : 䳿: .

 

 

24. , 50 : 䳿: .

 

25. , 20 : 䳿 : .

 

26. , 18 : . .

 





:


: 2016-12-06; !; : 597 |


:

:

.
==> ...

1568 - | 1394 -


© 2015-2024 lektsii.org - -

: 0.086 .