Лекция 22 - Упражнение

В клас

// ex.c

#include <stdio.h>

#include "file_fn.h"

int main() {
  if(read_file("file.txt")) {
    puts("SUCCESS");
  } else {
    puts("FAILED");
  }
  /*FILE* myFile = fopen("file.txt", "r");

  if(myFile == NULL) {
    puts("Failed to open the file!");
    return 1;
  }

  char buff[255];
  int lineCount = 0;

  while(fgets(buff, 255, myFile) != NULL) {
    buff[strlen(buff) - 1] = '\0';
    printf("[%d][%d] %s\n", ++lineCount, strlen(buff), buff);
  }*/
  /*while(!feof(myFile)) {
    fgets(buff, 255, myFile);
    if(!feof(myFile)) {
      printf("[%d] %s", ++lineCount, buff);
    }
  }*/

  /*int val = 0;
  while(fscanf(myFile, "%d", &val) != EOF) {
    printf("[%d] %d\n", ++lineCount, val);
  }*/

  //fclose(myFile);

  return 0;
}
// file_fn.h

int read_file(char* path);
// file_fn_fgets.c

#include <stdio.h>
#include <string.h>

#include "file_fn.h"

int read_file(char* path) {
  FILE* myFile = fopen(path, "r");

  if(myFile == NULL) {
    puts("Failed to open the file!");
    return 0;
  }

  char buff[255];
  int lineCount = 0;

  while(fgets(buff, 255, myFile) != NULL) {
    buff[strlen(buff) - 1] = '\0';
    printf("[%d][%ld] %s\n", ++lineCount, strlen(buff), buff);
  }


  fclose(myFile);

  return 1;
}
// file_fn_fscanf.c

#include <stdio.h>
#include <string.h>

#include "file_fn.h"

int read_file(char* path) {
  FILE* myFile = fopen(path, "r");

  if(myFile == NULL) {
    puts("Failed to open the file!");
    return 0;
  }

  int val = 0;
  int lineCount = 0;

  while(fscanf(myFile, "%d", &val) != EOF) {
    printf("[%d] %d\n", ++lineCount, val);
  }

  fclose(myFile);

  return 1;
}
// makefile

fgets:
	gcc ex.c file_fn_fgets.c

fscanf:
	gcc ex.c file_fn_fscanf.c
// file.txt

123
456
789
0
0
0
123
4
// bitwise.c

#include <stdio.h>

int main() {
  char n1 = 15; // 0000 1111
  char n2 = 35; // 0010 0011

                // 0000 0011 3 <- &
                // 0010 1111 47 <- |
                // 0010 1100 44 <- ^
                // 1111 0000 -16 <- ~

  //if(file_is_open && file_is_ready)
  //if(height > 10 || height < 6)
  //if((in_varna && !in_burgas) || (!in_varna && in_burgas))

  /*printf("%d \n", n1 & n2);
  printf("%d \n", n1 | n2);
  printf("%d \n", n1 ^ n2);
  printf("%u \n", ~n1);*/

  char mask = 0b00001000;

  char IS_VSYNC_ENABLED = 0b00000001;
  char IS_MXAA_ENABLED = 0b00000010;
  char IS_FULLSCREEN = 0b00000100;
  char IS_CUDA_ENABLED = 0b00001000;

  char nvidia_110 = IS_VSYNC_ENABLED | IS_MXAA_ENABLED;
  char nvidia_210 = IS_VSYNC_ENABLED | IS_MXAA_ENABLED | IS_FULLSCREEN;

//  printf("%d \n", n1 & mask);
  printf("%d \n", nvidia_110);
  printf("%d \n", nvidia_210);

  printf("%d \n", nvidia_210 & IS_FULLSCREEN);
  printf("%d \n", nvidia_110 & IS_FULLSCREEN);

  return 0;
}