Portál AbcLinuxu, 8. listopadu 2025 09:03
char *pole_soubor[254];
...
...
pocet_radku++;
if (pocet_radku==254) {
if ((pole_soubor=realloc (pole_soubor, pocet_radku*2))==NULL) {
fprintf (stderr,"Chyba realokace!\n");
return 1;
}
pole_soubor[pocet_radku]=(char *) malloc (strlen (radek_zaloha)+1);
pole_soubor[pocet_radku]=radek_zaloha;
}
.
pole_soubor jsou řádky, kam načítáš, a radek_zaloha je jeden řetězec, ve kterém je uložen zrovna načtený řádek, tak musíš použít:
strcpy(pole_soubor[pocet_radku], radek_zaloha);
místo pole_soubor[pocet_radku]=radek_zaloha;.
Realokace toho pole vypadá dobře.
man realloc
pocet_radku, jestlize se dostane na 254 tak se ma pole realokovat na vyssi hodnotu.Alokace pro kazdy radek mam udelane a funkcni. Opravdu problem je v tom ze nejak nechapu vami uvedeny bod 2 a 3. To bude asik to proc se s tim nedokazu domluvit. Nyni jsem se dostal do nasledujici podminky if ((pole_soubor=realloc (pole_soubor, sizeof (char)*pocet_radku*2))==NULL) a dostavam pri prekdladu nasledujici hlasku incompatible types in assignment coz prelozeno znamena>>Nekompatybilni typ v priprazeni. To vsechno chapu ale jaky tam mam dat typ!? Jsem z toho magor jak to udelat.Prosim poradte nekdo nejak tak jako pro blbe(mne).
char **pole_radku;
/*definujeme pole polí znaků*/ char **pole_radku; /*alokujeme pole pro pocet_radku řádků*/ pole_radku = (char **) malloc (sizeof(char *) * pocet_radku);
pole_radku je potom možné použít jako parametr funkce realloc, kdežto staticky definované pole char *pole_radku[254] realokovat nejde.
bod 3: velikost je v bajtech, ale každý řádek je ukazatel na pole znaků, tzn. má velikost 4B. Tím pádem bys svým reallocem velikost změnil na pocet_radku/4 řádků. Správné volání je tedy:
pole_radku = (char **) realloc (pole_radku, sizeof(char*) * pocet_radku *2);
(doufám, že tam nemám chybu
)
Jinak doporučuju nastudovat si něco o pointerech a namalovat si to.
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <malloc.h>
int getNthLine(int n, int *lines, int termcount, char *file_ptr, int filesize, char **line_ptr, int *linelen){
int begin, end;
if (n == 0){
begin = 0;
}
else{
begin = lines[n - 1] + 1;
}
if (n == termcount){
end = filesize;
}
else{
end = lines[n];
}
*line_ptr = file_ptr + begin;
*linelen = end - begin;
return 0;
}
int main(int argc, char **argv){
char *filename = argv[1]; /*UGLY*/
char *file_ptr = NULL;
int *lines = NULL;
char *line_ptr = NULL;
int fdstdout, linelen, filesize, fd, i, termcount = 0, allocated = 0;
struct stat filestat;
int alloc_ahead = getpagesize(); /* allocated memory will always be
multiple of page size*/
fd = open(filename, O_RDONLY);
fstat(fd, &filestat);
filesize = filestat.st_size;
printf("size is %d bytes\n", filesize);
file_ptr = mmap(0, filesize, PROT_READ, MAP_PRIVATE, fd, 0);
if (!file_ptr){
printf("mmap failed\n");
return -1;
}
close(fd);
for (i=0; i<filesize; i++){
if ('\n' == file_ptr[i]){
if (termcount >= allocated){
allocated = termcount + alloc_ahead;
lines = (int *)realloc(lines, allocated*sizeof(lines));
printf("(re)allocated memory for %d items\n", allocated);
}
lines[termcount] = i;
termcount++;
}
}
fdstdout = fileno(stdout);
for(i=0; i<=termcount; i++){
getNthLine(i, lines, termcount, file_ptr, filesize, &line_ptr, &linelen);
printf("%3d.line(%3d bytes): ", i + 1, linelen);
fwrite(line_ptr, linelen, 1, stdout);
fwrite("\n", 1, 1, stdout);
}
munmap(file_ptr, filestat.st_size);
return 0;
}
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.