Dobrý den, hledal jsem to různě na internetu, ale nikde jsem to nenašel. Je možné použít ve funkci dvakrát getchar? Potřeboval bych totiž spočítat řádky a potom znovu znaky načítat. Děkuji.
Fórum › C / C++
Dvakrát getchar()

Pole som už veľmi dávno nepoužíval, používam STL vector ako náhradu poľa, ale raz som počul, že nie je dobrý nápad dávať veľkosť poľa nekonštantnú (teda int arr[nejaka_premenna];). Mohli by ste mi to vy lepší potvrdiť alebo vyvrátiť a vysvetliť prečo ?
#5 lukas.balaz
Problem je ze take pole sa vytvara na stacku a stack je relativne maly (vecsinou radovo v jednotkach MB), ak bude pole dost velke tak nastane stackoverflow...
nebylo by jednodussi zjistit rovnou velikost souboru, pak to nacist do jednoho pole naraz a pak si pocitat radky, pak ukazatele na jejich zacatky a misto \r ci \n (podle toho co je driv) hodit proste \0?
mistr rejpal "predcasna optimalizace" nabidne volani subshelu a pocitani radku pres wc ... vzdyt si to vlastne muze nechat naprogramovat nekym jinym ne? hodne se tim toho nauci a tak... nebo v necem jinem.. to se asi zase nauci pracovat s pameti...
Tak pokud to čte přes getchar(), tak to čte z stdinu, takže bude trochu problém zjistit velikost vstupu. Nicméně bude opravdu potřeba to nejprve načíst do nějakého bufferu, v C++ to může být vector nebo string, v čistém C bych použil realloc s nějakým exponenciálním zvětšováním.

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
Getchar a while — založil Thom
Getchar vs Gets — založil Slimer
C getchar EOF — založil Poki
Getchar() a EOF — založil dreamer
Moderátoři diskuze