Nie wytrzymałem i zacząłem pisać program on nowa ,i znowu mam problemy... , problem polega na tworzeniu tablic struktur linie i poruszania sie po niej ,
program4.c:8:2: error: two or more data types in declaration specifiers
program4.c:11:1: error: expected identifier or ‘(’ before ‘}’ token
program4.c: In function ‘main’:
program4.c:40:16: error: array type has incomplete element type
program4.c:40:16: warning: unused variable ‘wektory’ [-Wunused-variable]
program4.c: In function ‘zainicjuj_wektor’:
program4.c:80:39: error: dereferencing pointer to incomplete type
program4.c:83:8: error: dereferencing pointer to incomplete type
program4.c:83:42: error: dereferencing pointer to incomplete type
program4.c:84:11: error: dereferencing pointer to incomplete type
program4.c:88:8: error: dereferencing pointer to incomplete type
program4.c:89:8: error: dereferencing pointer to incomplete type
program4.c: In function ‘powiekszam’:
program4.c:93:32: error: dereferencing pointer to incomplete type
program4.c:93:51: error: dereferencing pointer to incomplete type
program4.c:93:72: error: dereferencing pointer to incomplete type
program4.c:96:8: error: dereferencing pointer to incomplete type
program4.c:97:8: error: dereferencing pointer to incomplete type
program4.c: In function ‘dodaj’:
program4.c:101:11: error: dereferencing pointer to incomplete type
program4.c:101:25: error: dereferencing pointer to incomplete type
program4.c:104:8: error: dereferencing pointer to incomplete type
program4.c:104:25: error: dereferencing pointer to incomplete type
program4.c:105:8: error: dereferencing pointer to incomplete type
program4.c: In function ‘wypisz_skorowidz’:
program4.c:112:5: error: invalid use of undefined type ‘struct linie’
program4.c:112:16: error: dereferencing pointer to incomplete type
program4.c:114:12: error: invalid use of undefined type ‘struct linie’
program4.c:114:29: error: dereferencing pointer to incomplete type
program4.c:115:3: error: invalid use of undefined type ‘struct linie’
program4.c:115:22: error: dereferencing pointer to incomplete type
Działanie programu polega na wyszukiwaniu slow w pliku i wypisywanie w ktorych liniach wystapila dane slowo np
./a.out dane.txt co tam
slowo co wystapila w 6,14,15
slowo tam wystapila 10 90 ,102:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 8192
struct linie
int * elementy;
int size;
int licznik;
};
typedef struct {
int ile_slow;
char ** slowa;
} skorowidz_t;
struct linie * zainicjuj_wektor( void );
void dodaj( struct linie * wektor, int linia );
struct linie * powiekszam( struct linie * wektor );
void zainicjuj_skorowidz( int argc, char ** argv, skorowidz_t * s );
void wypisz_skorowidz( skorowidz_t * s, struct linie * wektory );
int
main( int argc, char ** argv ) {
int i;
int ile_linii;
char buf[ BUFSIZE ];
FILE * in = argc > 1 ? fopen( argv[ 1 ], "r" )
: stdin;
skorowidz_t skorowidz;
zainicjuj_skorowidz( argc, argv, & skorowidz );
struct linie wektory[ skorowidz.ile_slow ];
for( i = 0; i < skorowidz.ile_slow; i++ ) {
wektory[ i ] = zainicjuj_wektor();
}
if( skorowidz.ile_slow == 0 ) {
fprintf( stderr, "%s: błąd: proszę podać słowa do wyszukiwania\n", argv[ 0 ] );
return EXIT_FAILURE;
}
if( in == NULL ) {
fprintf( stderr, "%s: błąd: nie mogę czytać pliku %s\n", argv[ 0 ], argv[ 1 ] );
return EXIT_FAILURE;
}
ile_linii = 0;
while( fgets( buf, BUFSIZE, in ) != NULL ) {
ile_linii++;
for( i = 0; i < skorowidz.ile_slow; i++ )
if( strstr( buf, skorowidz.slowa[ i ] ) != NULL ) {
dodaj( wektory[ i ], ile_linii );
}
}
wypisz_skorowidz( & skorowidz, wektory );
return EXIT_SUCCESS;
}
void zainicjuj_skorowidz( int argc, char ** argv, skorowidz_t * skorowidz ) {
int i;
skorowidz->ile_slow = argc - 2;
skorowidz->slowa = malloc( skorowidz->ile_slow * sizeof * skorowidz->slowa );
for( i = 2; i < argc; i++ ) {
skorowidz->slowa[ i - 2 ] = argv[ i ];
}
}
struct linie * zainicjuj_wektor( void ) {
struct linie * wektor = malloc( sizeof * wektor );
if( wektor == NULL )
return NULL;
wektor->elementy = malloc( 1 * sizeof * wektor->elementy );
if( wektor->elementy == NULL ) {
free( wektor );
return NULL;
}
wektor->size = 1;
wektor->licznik = 0;
return wektor;
}
struct linie * powiekszam( struct linie * wektor ) {
int * wektornowy = realloc( wektor->elementy, 2 * wektor->size * sizeof * wektor->elementy );
if( wektornowy == NULL )
return NULL;
wektor->size *= 2;
wektor->elementy = wektornowy;
return wektor;
}
void dodaj( struct linie * wektor, int linia ) {
if( wektor->size <= wektor->licznik )
if( powiekszam( wektor ) == NULL )
return;
wektor->elementy[ wektor->licznik ] = linia;
wektor->licznik++;
return;
}
void wypisz_skorowidz( skorowidz_t * skorowidz, struct linie * wektory ) {
int i, j;
for( i = 0; i < skorowidz->ile_slow; i++ ) {
if( wektory[ i ]->licznik > 0 ) {
printf( "słowo \"%s\" wystąpiło w liniach:", skorowidz->slowa[ i ] );
for( j = 0; j < wektory[ i ]->licznik; j++ )
printf( "%d", wektory[ i ]->elementy[ j ] );
printf( "\n" );
} else {
printf( "nie napotkano słowa \"%s\"\n", skorowidz->slowa[ i ] );
}
}
}