C-programm oma lähtekoodi kuvamiseks väljundina

Lang L: none (table-of-contents)

Selles näites saate õppida programmi allikat kuvama makro __FILE__ abil.

Selle näite mõistmiseks peaksid teil olema teadmised järgmistest C-programmeerimise teemadest:

  • C Eeltöötleja ja makrod
  • C Failide käitlemine

Kuigi see probleem näib keeruline, on selle programmi kontseptsioon selge; kuvada sisu samast failist, kuhu kirjutate lähtekoodi.

C-programmeerimisel on eelnevalt määratletud makro, __FILE__mis annab praeguse sisendfaili nime.

 #include int main () (// asendab praeguse sisendfaili. printf ("% s", __ FILE__);) 

C-programm oma lähtekoodi kuvamiseks

 #include int main() ( FILE *fp; int c; // open the current input file fp = fopen(__FILE__,"r"); do ( c = getc(fp); // read character putchar(c); // display character ) while(c != EOF); // loop until the end of file is reached fclose(fp); return 0; ) 

Huvitavad Artiklid...