You are on page 1of 22

.

10

,
j
.
j C
FILE
stdio.h

j fopen j
(FILE*)
j
FILE * fopen ( const char * filename , const char * mode ) ;

filename - j j

mode -
| 10

2/22

r
w
a
r+
w+
a+


( )
(
)

(j )
(
j )

| 10

3/22


FILE * fp = fopen ( " C :\\ test . txt " , " r " ) ;

b j
. (rb)


j fclose

fclose ( fp ) ;

| 10

4/22


int fscanf ( FILE * fp , kontrolna niza , arguments_list )
int fgetc ( FILE * fp )


int fprtinf ( FILE * fp , kontrolna niza , arguments_list )
int fputc ( int c , FILE * fp )

| 10

5/22

a 1
1/2

j
.
j.
# include < stdio .h >
int is_letter ( char c ) {
return ( c >= a && c <= z ) || ( c >= A && c <= Z ) ;
}
int is_vowel ( char c ) {
c = tolower ( c ) ;
switch ( c ) {
case a :
case e :
case i :
case o :
case u :
return 1;
default :
return 0;
}
}

| 10

6/22

1
2/2
int main ( int argc , char * argv []) {
char c ;
int c ount_consonats = 0 , count_vowels = 0;
FILE * dat ;
// We need at least 2 arguments
if ( argc < 2) {
printf ( " Usage : % s \" file path \"\ n " , argv [0]) ;
return -1;
}
// Opening file
if (( dat = fopen ( argv [1] , " r " ) ) == NULL ) {
printf ( " The file % s cannot be open .\ n " , argv [1]) ;
return -1;
}
// Reading char by char until the EndOfFile ( EOF )
while (( c = fgetc ( dat ) ) != EOF ) {
if ( is_letter ( c ) ) {
if ( is_vowel ( c ) )
count_vowels ++;
else
count_consonats ++;
}
}
fclose ( dat ) ;
printf ( " Ratio vowel / consonats : % d /% d = %5.2 f \ n " , count_vowels ,
count_consonats ,
( float ) count_vowels / count_consonats ) ;
return 0;
}
| 10

7/22

a 2

j j
j
j j
.

j,
. j j 80
.

| 10

8/22

a 2
1/2

# include < stdio .h >


# define MAX 81
int main ( int argc , char * argv []) {
char line [ MAX ] , * c ;
FILE * input , * output ;
if ( argc < 3) {
printf ( " Usage : % s [ input_filename ] [ output_filename ]\ n " ,
argv [0]) ;
return -1;
}
if (( input = fopen ( argv [1] , " r " ) ) == NULL ) {
printf ( " File % s could not be open .\ n " , argv [1]) ;
return -1;
}
if (( output = fopen ( argv [2] , " w " ) ) == NULL ) {
printf ( " File % s could not be open \ n " , argv [2]) ;
return -1;
}

| 10

9/22

a 2
2/2

while (( fgets ( line , MAX , input ) ) != NULL ) {


int count = strlen ( line ) ;
fprintf ( output , " % d % s " , count , line ) ;
}
fclose ( input ) ;
fclose ( output ) ;
return 0;
}

| 10

10/22

a 3

j
matrica1.txt.
j
. j
j .

matrica2.txt .

| 10

11/22

a 3
1/2

# include < stdio .h >


# include < stdlib .h >
# define MAX 100
int main () {
int i , j , m , n ;
float a [ MAX ][ MAX ] , b [ MAX ][ MAX ];
FILE * input , * output ;
if (( input = fopen ( " matrica1 . txt " , " r " ) ) == NULL ) {
printf ( " Datotekata matrica1 . txt ne se otvora !\ n " ) ;
exit (1) ;
}
if (! feof ( input ) )
fscanf ( input , " % d % d " , &m , & n ) ;
if (( m > MAX ) || ( n > MAX ) ) {
printf ( " Mnogu golema matrica ! " ) ;
return ( -1) ;
}
for ( i = 0; i < m && ! feof ( input ) ; i ++)
for ( j = 0; j < n && ! feof ( input ) ; j ++)
fscanf ( input , " % f " , & a [ i ][ j ]) ;
fclose ( input ) ;

| 10

12/22

a 3
2/2

if ( i != m || j != n ) {
printf ( " Nema dovolno podatoci vo datotekata ! " ) ;
return ( -1) ;
}
for ( i = 0; i < m ; i ++)
for ( j = 0; j < n ; j ++)
b [ j ][ i ] = a [ i ][ j ];
if (( output = fopen ( " matrica2 . txt " , " w " ) ) == NULL ) {
printf ( " Datotekata matrica2 . txt ne se otvora !\ n " ) ;
exit (1) ;
}
fprintf ( output , " % d % d \ n " , n , m ) ; /* obratno */
for ( i = 0; i < n ; i ++)
for ( j = 0; j < m ; j ++)
fprintf ( output , " %7.2 f \ n " , b [ i ][ j ]) ;
fclose ( output ) ;
return (0) ;
}

| 10

13/22

a 4

KRSPrimer.txt.
j j
j , j
. j j 80 .

| 10

14/22

a 4

# include < stdio .h >


# include < stdlib .h >
int e_samoglaska ( char c ) {
return c == a || c == e || c == i || c == o || c == u ;
}
int main () {
int red = 0 , vkupno = 0;
FILE * dat ; char c ;
if (( dat = fopen ( " KRSPrimer . txt " , " r " ) ) == NULL ) {
printf ( " Datotekata KRSPrimer . txt ne se otvora " ) ;
exit ( -1) ;
}
int samoglaski = 0;
while (( c = fgetc ( dat ) ) != EOF ) {
if ( e_samoglaska ( c ) ) {
++ samoglaski ;
++ vkupno ;
}
if ( c == \ n ) {
if ( samoglaski > 10) {
red ++;
}
samoglaski = 0;
}
}
printf ( " Vkupno % d reda imaat povekje od 10 samoglaski \ n " , red ) ;
printf ( " Vo datotekata ima vkupno % d samoglaski .\ n " , vkupno ) ;
return 0;
}
| 10

15/22

a 5

j
j
( j ).
. j
j .
.
20 .
j.

, , , , ,

| 10

16/22

a 5
1/2

# include < stdio .h >


# include < ctype .h >
# include < stdlib .h >
# define WORDLEN 21
# define LINELEN 81
int i m a _ p o v ek e _o d 2i s ti ( char * w ) {
char * c ;
int isti ;
while (* w ) {
c = w + 1;
isti = 1;
while (* c ) {
if ( tolower (* w ) == tolower (* c ) )
isti ++;
c ++;
}
if ( isti > 2)
return 1;
w ++;
}
return 0;
}

| 10

17/22

a 5
2/2

int main ( int argc , char * argv []) {


char zbor [ WORDLEN ];
FILE * f ;
int brzb = 0;
if ( argc > 2) {
printf ( " Upotreba : % s ime_na_datoteka \ n " , argv [0]) ;
exit ( -1) ;
}
if ( argc == 1)
f = stdin ;
else if (( f = fopen ( argv [1] , " r " ) ) == NULL ) {
printf ( " Datotekata % s ne se otvora .\ n " , argv [1]) ;
return -1;
}
while ( fgets ( zbor , WORDLEN , f ) ) != NULL ) {
if ( i ma _ po v ek e _o d2 i st i ( zbor ) ) {
puts ( zbor ) ;
brzb ++;
}
}
printf ( " \ nVkupno % d zborovi .\ n " , brzb ) ;
fclose ( f ) ;
return 0;
}

| 10

18/22

a 6

j j
j
.
j.
j
, , Nevalidni
argumenti na komandna linija.

| 10

19/22

a 6
1/2

# include < stdio .h >


# include < stdlib .h >
# include < ctype .h >
int main ( int argc , char ** argv ) {
char c ;
int brPojavuvanja = 0;
FILE * dat ;
if ( argc != 3) {
printf ( " Nevalidni argumenti na komandna linija \ n " ) ;
exit ( -1) ;
} else {
if (( dat = fopen ( argv [1] , " r " ) ) == NULL ) {
printf ( " Datotekata % s ne se otvora !\ n " , argv [1]) ;
exit ( -1) ;
}
}

| 10

20/22

a 6
2/2

char * zbor = argv [2];


int i = 0 , count = 0;
while (( c = fgetc ( dat ) ) != EOF ) {
if ( isdigit ( c ) ) {
if ( c != zbor [ i ++]) {
if ( count == strlen ( zbor ) ) {
brPojavuvanja ++;
}
count = 0;
} else {
count ++;
}
} else {
if ( count == strlen ( zbor ) ) {
brPojavuvanja ++;
}
count = 0;
}
}
printf ( " Zborot % s se pojavuva % d pati vo datotekata \ n " , zbor ,
brPojavuvanja ) ;
return 0;
}

| 10

21/22

, , j
courses.finki.ukim.mk


https://github.com/tdelev/SP/tree/master/latex/src

j
forum.finki.ukim.mk

| 10

22/22