Ëåêöèÿ 6


1. EOF è feof()






Ïðîãðàììà çàöèêëèâàåòñÿ, ïîòîìó ÷òî fscanf() íå ïðî÷èòàë ÷èñëî, íî è íå äîøåë äî êîíöà ôàéëà. Ïîýòîìó îí íå âîçâðàùàåò -1 (êîíñòàíòà EOF).


Ïðàâèëüíûé êîä:

#include <stdio.h>

int main(void) {

FILE *IN;

int n, res;

IN = fopen("EOF_1.dat", "r");

if (IN == NULL) {

printf("File not opened\n");

return -1;

}

while ((res = fscanf(IN, "%d", &n)) == 1) {

printf("res: %d, n: %d\n", res, n);

}

return 0;

}


feof():





Ïðîãðàììà çàöèêëèëàñü, ïîòîìó ÷òî feof() ñ÷èòàåò, ÷òî åùå íå êîíåö ôàéëà (ñèìâîë ñ êîäîì 0x1A).


Ïðàâèëüíûé êîä:


#include <stdio.h>

int main(void) {

FILE *IN;

int n, res;

IN = fopen("feof.dat", "r");

if (IN == NULL) {

printf("File not opened\n");

return -1;

}

while (fscanf(IN, "%d", &n) == 1) {

printf("res: %d, n: %d\n", res, n);

}

return 0;

}



Íàø ëèôò äîëæåí âûäåðæèâàòü ïîïàäàíèå â íåãî ñàìîãî íåîáó÷åííîãî àêàäåìèêà» (Ñòðóãàöêèå «Ñêàçêà î òðîéêå»)


Ïðîãðàììà äîëæíà ðàáîòàòü äëÿ ëþáûõ âõîäíûõ äàííûõ


2. Óêàçàòåëè

Óêàçàòåëü — ýòî ïåðåìåííàÿ, ñîäåðæàùàÿ àäðåñ ÿ÷åéêè ïàìÿòè.

Íàèìåíüøàÿ àäðåñóåìàÿ ÿ÷åéêà ïàìÿòè — áàéò (îáû÷íî 8 áèòîâ).

char – îäíîáàéòîâûé òèï

int, double – áîëüøå îäíîãî áàéòà.



int n = 0, m;

int *p = &n; /* * îáîçíà÷àåò, ÷òî p – ýòî óêàçàòåëü, int – óê.íà öåë. */

n = 1;

m = *p; /* m := 1 */

n = 2;

printf(“m: %f\n”, m); /* m: 1 */



2.1 Ðàáîòà ñ ìàññèâàìè ïðè ïîìîùè óêàçàòåëåé

Çàäà÷à 1. Çàïîëíèòü ìàññèâ ÷èñëîì 17

Ðåøåíèå 1:

int array[100];

int i;

for (i = 0; i < 100; i++) {

array[i] = 17;

}

Ðåøåíèå 2 (ðàáîòàåò áûñòðåå, òàê êàê íå íàäî êàæäûé ðàç âû÷èñëÿòü àäðåñ i-ãî ýëåììåíòà; îïåðàöèÿ óâåëè÷åíèÿ àäðåñà íà 1 ýëåìåíò ðàáîòàåò áûñòðåå, ÷åì ñëîæåíèå):

int array[100];

int *p = &array[0];

int i;

for (i = 0; i < 100; i++) {

*(p++) = 17; /* p óâåëè÷èòñÿ íà ðàçìåð ýëåìåíòà ìàññèâà */

}

Ðåøåíèå 3 (ðàáîòàåò áûñòðåå, òàê êàê íå íàäî êàæäûé ðàç âû÷èñëÿòü àäðåñ i-ãî ýëåììåíòà; îïåðàöèÿ óâåëè÷åíèÿ àäðåñà íà 1 ýëåìåíò ðàáîòàåò áûñòðåå, ÷åì ñëîæåíèå):

int array[100];

int *p = &array[0];

int *end = &array[100]; /* àäðåñ çà êîíöîì ìàññèâà */

while (p < end) {

*(p++) = 17; /* p óâåëè÷èòñÿ íà ðàçìåð ýëåìåíòà ìàññèâà */

}



int array[100];

/* int *p = &array[0]; */

int *p = array; /* p ïðèñâîèòñÿ àäðåñ ýëåìåíòà ìàññèâà ñ èíäåêñîì 0 */

(èìÿ ìàññèâà — ðàâíî åãî àäðåñó)



3. Äèíàìè÷åñêèå ìàññèâû

1. Çàõâàòèòü ïàìÿòü: malloc(<êîëè÷åñòâî áàéòîâ>) - çàõâàòèòü îáëàñòü ïàìÿòè ðàçìåðà <êîëè÷åñòâî áàéòîâ> (Memory ALLOC)

2. Åñëè ïàìÿòè íå õâàòèëî — çàõâàòèòü îáëàñòü ïàìÿòè ïîáîëüøå è ñêîïèðîâàòü òóäà ñîäåðæèìîå ïðåäûäóùåé çàõâà÷åííîé îáëàñòè, ïîòîì ïðåäûäóùóþ îñâîáîäèòü

3. Îñâîáîäèòü çàõâà÷åííóþ îáëàñòü ïàìÿòè — free(<àäðåñ çàõâà÷åííîé ïàìÿòè>)



Çàäà÷à. Ïðî÷èòàòü èç ôàéëà ÷èñëà, îòñîðòèðîâàòü èõ è âûâåñòè â äðóãîé ôàéë. ×èñëà ïðåäâàðèòåëüíî ïåðåñ÷èòàòü è çàõâàòèòü äëÿ íèõ îäèí ìàññèâ ïàìÿòè.

#include <stdio.h>

#include <stdlib.h>

void sort(double *array, int N);

/* void sort(double array[], int N); */

int main(void) {

FILE *IN, *OUT;

double x, *array;

int N = 0, i;

IN = fopen("input.txt", "r");

if (IN == NULL) {

printf("File not opened\n");

return -1;

}

while (fscanf(IN, "%lf", &x) == 1) {

++N;

}

fclose(IN);

IN = fopen("input.txt", "r");

if (IN == NULL) {

printf("File not opened\n");

return -1;

}

array = (double*)malloc(N * sizeof(double));

for (i = 0; i < N; i++) {

if (fscanf(IN, "%lf", &array[i]) != 1) {

printf("Missed numbers\n");

return -1;

}

}

fclose(IN);

sort(array, N);

OUT = fopen("output.txt", "w");

if (OUT == NULL) {

printf("File output.txt not opened\n");

return -1;

}

for (i = 0; i < N; i++) {

fprintf(OUT, "%lf ", array[i]);

}

fclose(OUT);

return 0;

}

void sort(double *array, int N) {

double buf;

int i, j;

for (j = 0; j < N; j++) {

for (i = 0; i < N - 1; i++) {

if (array[i] > array[i+1]) {

buf = array[i];

array[i] = array[i+1];

array[i+1] = buf;

}

}

}

}




Ïðåîáðàçîâàíèå òèïîâ â ÿçûêå C: (<íîâûé òèï>)<âûðàæåíèå ñòàðîãî òèïà>

Ïðèìåð 1.

double x;

int n = (int)x;

x = (double)n;



Íåÿâíîå ïðåîáðàçîâàíèå òèïîâ: âñå îïåðàíäû â âûðàæåíèè è åãî çíà÷åíèå ïðèâîäÿòñÿ ê îäíîìó òèïó (ê òèïó ïåðâîãî âñòðå÷åííîãî êîìïèëÿòîðîì îïåðàíäà)

int n = 5;

double x;

x = 1/n; /* x ïðèñâîèòñÿ 0 */

x = 1./n; /* x ïðèñâîèòñÿ 0.2 */

x = (double)1/n; /* ßâíîå ïðåîáðàçîâàíèå òèïîâ, x ïðèñâîèòñÿ 0.2 */