Advertisement
green1ant

2_3 for file input

Oct 16th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.    char * filename = "D://bsuir/oaip/2_1/c/2_1/bin/Debug/input.txt";
  7.    FILE *file;
  8.    char name[20];
  9.    int nums[3][3];
  10.    if((file = fopen(filename, "r")) == NULL)
  11.    {
  12.       perror("Error occured while opening file");
  13.       return 1;
  14.    }
  15.  
  16.  
  17.    int i, j;
  18.    
  19.    for(i = 0; i < 3; i++){
  20.       for(j = 0; j < 3; j++){
  21.          fscanf(file, "%d", &nums[i][j]);
  22.          printf("%d ", nums[i][j]);
  23.       }
  24.       printf("\n");
  25.    }
  26.       //printf("%s %d\n", name, age);
  27.    //}
  28.    fclose(file);
  29.  
  30.    /*
  31.    char * message = "Hello \n world!\n An apple a day keeps the doctor away";
  32.     char * filename = "D://bsuir/oaip/2_1/c/2_1/bin/Debug/output.txt";
  33.     char cc[256];
  34.     FILE *fp;
  35.  
  36.  
  37.     // запись в файл
  38.     if((fp= fopen(filename, "w"))==NULL)
  39.     {
  40.         perror("Error occured while opening file");
  41.         return 1;
  42.     }
  43.     // записываем строку
  44.     fputs(message, fp);
  45.  
  46.     fclose(fp);
  47.  
  48.     // чтение из файла
  49.     if((fp= fopen(filename, "r"))==NULL)
  50.     {
  51.         perror("Error occured while opening file");
  52.         return 1;
  53.     }
  54.     // пока не дойдем до конца, считываем по 256 байт
  55.     while((fgets(cc, 256, fp))!=NULL)
  56.     {
  57.         printf("%s", cc);
  58.     }
  59.  
  60.     fclose(fp);
  61.     */
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement