In: Computer Science
C program!
Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50.
Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character).
After reading things in, your C program continues to accept input from the user, it ignores whatever the user enters but the return key, and for each return key it randomly chooses one out of the 5 things to print to the stdout.
Your C program iterates the process for ever until the user enters an EOF (which means by input redirection your program terminates at the end of file).
By randomly choosing a thing, it means you should try to make the probability distribution as evenly as possible to any one of the 5 things-to-do.
Any help is appreciated!!
Thank you!!
The C program to handle a file with 5 lines and randomly print one line from the file till user enter EOF is:
#include<stdio.h>
//Including stdlib to use exit() function
#include<stdlib.h>
//Including time.h library to seed srand() function
#include<time.h>
//random() function returns a random number in specified range
int random(){
//srand() sets seed for rand() function to generate random function
srand(time(NULL));
//rand()%max generates a random number in range (0,max)
int number= rand()%5;
return number;
}
//Program execution starts here
int main(){
/*For communication between file and program we need to
create a pointer of type FILE */
FILE *fptr;
//Declaring a string array of size 5 with each string size 50
char text_lines[5][50];
/*fopen("textfile","r") is a function to open a textfile.
Second argument denotes mode. "r" is used for reading*/
//If file is empty then error message is printed
if((fptr=fopen("C:\\hobbies.txt","r"))==NULL){
printf("Error reading file");
exit(1);
}
int i=0;
/*fgets() function gets a line from the opened text file
and stores the line in first argument*/
//Storing each new line in already declared char array text_lines
while(fgets(text_lines[i],sizeof text_lines[i],fptr)!=NULL){
fscanf(fptr,"%[^\n]",text_lines[i]);
i++;
}
//We are done with the file. So closing file opening fclose()
fclose(fptr);
char lines[25];
int rand_num;
printf("Entering infinite loop. Press EOF(-1) to exit.\n");
/*Entering a infinte loop to repeatedly take a input from user
and randomly print a line from the textfile*/
//The loop exits when user enter EOF
//Remember value of EOF in c is -1.
//So when user enter -1 this loop terminates
while(i!=-1)
{
printf("Enter ctrl+z to exit, else enter input: ");
scanf("%s",&lines);
if (atoi(lines)==EOF)
{
break;
}
/*Calling random() funciton to randomly get a index
to access text_lines[] array which contain the read file*/
rand_num = random();
printf("%s\n",text_lines[rand_num]);
}
printf("\nGood bye!!!");
return 0;
}
To access a file in a program we first need to create a pointer of FILE to allow communication between the file and program. To read a file from a directory we need to use fopen() function and mention the file directory in the fopen() function. This opens the file in program.
Now to read a line from the file I used fgets() function reads a line from the opened file. Remember that gets() function is similar to scanf() function but unlike scanf() function gets() function reads a entire line. Similarly fgets() reads only one line from a file. '
I have stored each line in a String array to randomly print a line when user enter a input and enter a return key. Then I randomly printed a line of the file stored in string array by randomly generating a index value.
I have tested the code and validated output.The code is working good. I am sharing the test file I created and the output of the program.
Remember the file directory and the file name I mentioned in fopen() function is the file in my computer. You should create a text file in your computer and paste the file directory and file name in the fopen() function in code for executing the code.
**If you don't change the directory in fopen() function to your file directory the code will throw error.
Hope this answer helps you.
Thank you :)