Advertisement
Tobiahao

09_PLIKI_02

Jan 13th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. /*
  2. Napisz program, który odczyta swój kod źródłowy i wypisze na ekranie wyłącznie umieszczone w nim komentarze.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #define BUFFER_SIZE 1024
  8.  
  9. void read_buffer(char *buffer, unsigned long size)
  10. {
  11.     for(int i = 0; i < size; i++){
  12.         printf("%c", buffer[i]);
  13.     }
  14. }
  15.  
  16. void odczytaj_kod_zrodlowy(FILE *plik)
  17. {
  18.     char tmp;
  19.     char tmp_next = getc(plik);
  20.  
  21.     while(!feof(plik)){
  22.         char buffer[BUFFER_SIZE];
  23.         tmp = tmp_next;
  24.         tmp_next = getc(plik);
  25.         //KOMENTARZ1
  26.         //KOMENTARZ2
  27.  
  28.         if(tmp_next != EOF){
  29.             if((tmp == '/') && (tmp_next == '/')) {
  30.                 fgets(buffer, BUFFER_SIZE, plik);
  31.                 printf("%s", buffer);
  32.             }
  33.  
  34.             if((tmp == '/' && tmp_next == '*')) {
  35.                 int i = 0;
  36.                 while(!((tmp == '*') && (tmp_next == '/'))) {
  37.                     tmp = tmp_next;
  38.                     tmp_next = getc(plik);
  39.                     buffer[i] = tmp_next;
  40.                     i++;
  41.                 }
  42.                 //KOMENTARZ3
  43.                 read_buffer(buffer, strlen(buffer)-2);
  44.                 memset(buffer, 0, BUFFER_SIZE);
  45.             }
  46.  
  47.         }
  48.     }
  49.     printf("\n");
  50. }
  51.  
  52. int main(int argc, char *argv[])
  53. {
  54.     FILE *kod_zrodlowy;
  55.     kod_zrodlowy = fopen("main.c", "r");
  56.  
  57.     if(kod_zrodlowy == NULL){
  58.         printf("Blad otwarcia pliku!\n");
  59.         return -1;
  60.     }
  61.     //KOMENTARZ4
  62.     odczytaj_kod_zrodlowy(kod_zrodlowy);
  63.  
  64.     if(fclose(kod_zrodlowy)){
  65.         printf("Blad zamkniecia pliku!\n");
  66.         return -1;
  67.     }
  68.     /*KOMENTARZ5*/
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement