Matt23

Write & read

Nov 23rd, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <memory.h>
  3.  
  4. int main(int argc, char *argv[]) {
  5.     char text[100];
  6.     printf("Podaj tekst: ");
  7.     gets(text);
  8.  
  9.     int length = 0;
  10.     while (text[length] != '\0') length++;
  11.     printf("Długość: %d\n", length);
  12.     printf("Długość funkcją strlen: %zu\n\n", strlen(text));
  13.  
  14.  
  15.     FILE *file = fopen("/tmp/tekstowy_plik", "w+");
  16.     if (file == NULL) {
  17.         printf("Sth went wrong\n");
  18.         return 0;
  19.     }
  20.  
  21.     for (int i = 0; text[i] != '\0'; i++)
  22.         fputc(text[i], file);
  23.  
  24.     printf("Zapisano do pliku\n");
  25.     rewind(file);
  26.  
  27.     char content[100];
  28.     for (int i = 0; ; i++) {
  29.         int c = fgetc(file);
  30.  
  31.         if (feof(file)) break;
  32.         content[i] = (char) c;
  33.     }
  34.  
  35.     printf("Odczytano następującą treść:\n\n");
  36.     for (int i = 0; content[i] != '\0'; i++) {
  37.         printf("%c", content[i]);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment