Untitled

 avatar
unknown
c_cpp
2 years ago
1.6 kB
2
Indexable
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// constants for maximum number of lines in the file and max length of a line
#define MAX_LINES 100
#define MAX_LEN 2000
int random_num();
int main(void)
{
 
  char data[MAX_LINES][MAX_LEN];
  char data2[MAX_LINES][MAX_LEN];
  // Create a file pointer variable to allow us to access the file
  FILE *file;
  FILE *file2;
  
  // Open the file in reading mode, fopen() will return NULL if it fails to 
  // open the file...
  file = fopen("truth.txt", "r");
  file2 = fopen("dare.txt", "r");
  if (file == NULL)
  {
    printf("Error opening file.\n");
    return 1;
  }
  if (file2 == NULL)
  {
    printf("Error opening file.\n");
    return 1;
  }
  // line will keep track of the number of lines read so far from the file
  int line = 0;
  int line2=0;
  int p=0;
  while (!feof(file) && !ferror(file))
    if (fgets(data[line], MAX_LEN, file) != NULL)
      line++;
  
  // Close the file when we are done working with it.
  fclose(file);
  
  
  while (!feof(file2) && !ferror(file2))
    if (fgets(data2[line2], MAX_LEN, file2) != NULL)
      line2++;
  
  // Close the file when we are done working with it.
  fclose(file2);
  while(p==0){
    printf("\nchoose\n1.truth\n2.dare\n3.exit\n:");
    int dat;
    scanf("%d",&dat);
    if (dat==1){
        printf("\n%s", data[random_num()]);
    }else if(dat==2){
        printf("\n%s", data2[random_num()]);
    }else{
        p=1;
        break;
    }
  }
 

  return 0;
}

int random_num(){
    int number;
    srand(time(NULL));
    number=rand() % 100;
    return number;
}