For this lab, you can assume that no string entered by the user will be longer than 128 characters. You can also assume that the database file does not contain more than 128 lines. However, you can not assume that the database has the correct format.
There are three commands your program must handle. If any other command is entered, the program should reply
Unrecognized command.
and then present the prompt again. The three commands are:
If inputfile.txt can be opened for reading, your program must read from the database file, and determine the number of correctly formatted lines in it. It must then output a message such as
Read 54 facts from inputfile.txt
where 54 is the number of correctly formatted lines in inputfile.txt.
A line that does not conform to the correct format outlined in the Background section is considered malformed and is not counted in the number of facts reported above. If there are any such lines in the input file, your program must count them, and produce output as below
Read 54 facts from inputfile.txt 12 malformed lines were ignored.
The name of the input file may be different of course. In fact, it may be a full path name, such as /eecs/home/bil/input.txt
If the database file could not be opened for reading, your program must respond
Unable to read from inputfile.txt
If no input file is provided, your program must respond
Missing Argument.
If more than one input file is provided, your program must respond
Read 54 facts from inputfile.txt 12 malformed lines were ignored. Extra arguments ignored.
If no facts have been read through a successful setdb command, your program must respond
Database is empty.
Otherwise, your program must output the facts in the order that they were read from the database file. Any extraneous whitespace must be removed, i.e. the only whitespace must be two spaces between the three words.
The expectation is that your program will store the database facts internally. In other words, printdb should not re-read the input file.
If the user enters an empty command, i.e. just hits Enter, a new prompt must be presented in the next line.
To implement this lab, you need to be able to compare and copy strings in C. Take a look at the man page for strncmp and strncpy. It should be all you need to implement this part.
Following is a sample run that shows the expected behaviour of the program.
% gcc lab11.c % a.out Grock- setdb example.txt Read 4 facts from example.txt Grock- printdb inherit class1 class2 inherit class3 class4 implement class3 interface1 aggregate class3 class5 Grock- setdb malformed.txt extra Read 2 facts from malformed.txt 4 malformed lines were ignored. Extra arguments ignored. Grock- printdb rel ent1 ent2 rel ent3 ent6 Grock- quit %
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YES 1
#define NO 0
void trim(char *str){
int len = strlen(str)+1;
char *dest = (char*)malloc(len * sizeof(char));
char *src ;
src = str; // char pointer to original
string
int j = 0;
j = 0;
while(*src !='\n'){
if(isspace((unsigned
char)*src)){
src++; // move
one byte
continue;
}
//woow isdigit i was miissing took
me hours
while(isalpha((unsigned char)*src)
|| isdigit((unsigned char)*src)){
dest[j++] =
*src++;
}
dest[j++]= ' ';
}
strncpy(str, dest , strlen(dest) + 1);
}
void *readline(){
char args[128]; // buffer
char *temp = NULL;
int count = 1;
while(fgets(args , 128 , stdin)){
if(count == 1){
temp = (char *)malloc(sizeof(char)
* 128);
if(temp != NULL){
strcpy(temp ,
args);
}
else{
fprintf(stderr ,
"Not enought memory!! \n");//not enought memory
free(temp);
}
}
else{
char *more_mem = (char
*)realloc(temp , sizeof(char) * 128 * count);
temp = more_mem;
strcat(temp , args);
}
if(strchr (temp , '\n') != NULL)
return temp;
count++;
}
return temp;
}
char *file_memalloc(FILE *in){
char *str = (char *) malloc(128 * sizeof(char));
char *temp = NULL;
int counter = 1;
while(fgets(str , 128 , in)){
if(counter == 1){
temp = (char *)
malloc(128 * sizeof(char)); //temporary memory space
strcpy(temp ,
str);
counter++;
}
else{
char * temp_str
= (char *) realloc(temp , sizeof(char) * 128 * counter); // give me
more memory starting right for where we left off
if(temp_str ==
NULL)
fprintf(stderr , "Virtual memory
exhausted\n");
temp =
temp_str;
strcat(temp ,
str); //i'm thinkng the first run of ten chars the strcat does the
same thing strcpy
counter++;
}
if(strchr(temp , '\n') !=
NULL)
return
temp;
counter++;
}
return temp;
}
//global variable for printdb only
//int printdb = 0;
main(){
char *args;
char *first_args;
char *second_args;
char *extra;
int boolean = 0;
//---------------------------------------------------------------------------------------------------------------------------------------------------------/
char *exact_lines;
char *first;
char *second;
char *third;
char **outfile;
outfile = malloc( 10 * sizeof(char *)); // intial
intialization for index like but [ptr] so needs intialization
int increment_for_realloc = 0;
do{
printf("Grock- ");
args =
readline();
printf("The
command -> %s" , args);
if(strcmp(args,
"quit\n") == 0){
break;
}
//Intilalize the first second and
third args with strlen of args because its long enough
//int len = strlen(args);
//printf("%d \n" , len);
first_args = (char*)
malloc(strlen(args) * sizeof(char));
second_args =(char*)
malloc(strlen(args) * sizeof(char));
extra = (char*) malloc(strlen(args)
* sizeof(char));
int tokens = sscanf(args ,"%s %s
%[^\n]s" , first_args , second_args , extra);
if(tokens < 0 || strcmp(args ,"
\n") == 0)
continue;
if(tokens == 1){
int cmp =
strcmp(first_args , "setdb");
if(cmp == 0
&& tokens == 1){
printf("Missing argument.\n");
}
else{
int
cmp3 = strcmp(first_args , "printdb");
if(cmp3 == 0)
if(boolean){ // if you are
able to read file
//clean
outfile otherwise you will have repeating correct format
files
int i =
0;
for(i = 0
; i < increment_for_realloc ; i++){
//printf("Should be based on increment =>
%d\n" , i);
printf("%s" ,
outfile[i]);
}
//clean up
garbage collection
//increment_for_realloc = 0;
boolean =
YES; // set the switch if you came here
already
}
else{
printf("Database is empty.\n");
}
else{
printf("Unrecognized
command.\n");
}
}
if(strncmp(first_args , "putrel", strlen(first_args)) == 0){
if(boolean){
int i = 0;
printf("Implemented
functionanlity .....\n");
}
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if (tokens >= 2){
int cmp2 =
strcmp(first_args , "setdb");
if(cmp2 ==
0){
FILE *database;
int c;
//second args is my filename
database = fopen(second_args,"r");
if( database == NULL){ //case where file might
not exist.Seg fault if you try to close the file
printf("Unable to read from
%s\n" , second_args);
boolean = NO;
continue;
}
else{
int word_count = 0;
int line_counter = 0;
int lines = 0;
//increment_for_realloc++;
while(exact_lines =
file_memalloc(database)){
increment_for_realloc++;
int len =
strlen(exact_lines) + 1;
//allocate
memory according to exact_lines
first =
(char *) malloc(len * sizeof(char));
second =
(char *) malloc(len * sizeof(char));
third =
(char *) malloc(len * sizeof(char));
extra =
(char *) malloc(len * sizeof(char));
if(increment_for_realloc >= 5){
outfile = realloc(outfile ,
increment_for_realloc * 10 * sizeof(char *));
}
printf("more space counter -> %d\n" ,
increment_for_realloc);
outfile[word_count] = malloc(len * sizeof(char));
lines =
sscanf(exact_lines ,"%s %s %s %[^\n]s" , first ,second ,third
,extra );
//printf("lines -> %d\n" , lines);
if(lines
< 0) {
word_count++;
line_counter++;
continue; // a fix because empty lines were not
counted
}
if (lines
!= 3){
line_counter++; // number of malformed lines as
a check
boolean = YES;
}
//added
for putrel
if(lines
== 4){
trim(exact_lines);
//printf("rel-> %s ent-> %s ent-> %s\n"
, first ,second , third);
strncpy(outfile[word_count] , exact_lines ,
strlen(exact_lines) + 1);
printf("%s\n" , outfile[word_count]);
strcat(outfile[word_count] , "\n");
//printdb = word_count; // refrence to print
over outfile
boolean = YES;
}
else{
trim(exact_lines);
printf("rel-> %s ent-> %s ent-> %s\n" ,
first ,second , third);
strncpy(outfile[word_count] , exact_lines ,
strlen(exact_lines) + 1);
printf("%s\n" , outfile[word_count]);
strcat(outfile[word_count] , "\n");
//printdb = word_count; // refrence to print
over outfile
boolean = YES;
}
word_count++;
free(exact_lines);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//just for formatting with
sscanf return value
printf("Read %d facts from
%s\n" , (word_count - line_counter) , second_args);
if(line_counter >
0){
printf("%d
malformed lines were ignored.\n" , line_counter);
}
if(tokens == 3){
printf("Extra arguments ignored.\n");
}
fclose(database); //closes
the buffer stream
}
}
else{
printf("Unrecognized command.\n");
}
}
}while(1);
}
input.txt
rel ent
rel ent1 ent2 ent3
rel
rel ent1 ent2
rel ent3 ent6
For this lab, you can assume that no string entered by the user will be longer...
2. Write a function file_copy() that takes two string parameters: in file and out_file and copies the contents of in_file into out file. Assume that in_file exists before file_copy is called. For example, the following would be correct input and output. 1 >>> file_copy( created equal.txt', 'copy.txt) 2 >>> open fopen ( copy.txt') 3 >>> equal-f = open( 'created-equal . txt') 4 >>equal_f.read () 5 'We hold these truths to be self-evident, Inthat all men are created equalIn' 3. Write:...
. . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....
Lab 10: ArrayLists and Files in a GUI Application For this lab, you will work on a simple GUI application. The starting point for your work consists of four files (TextCollage, DrawTextItem, DrawTextPanel, and SimpleFileChooser) in the code directory. These files are supposed to be in package named "textcollage". Start an Eclipse project, create a package named textcollage in that project, and copy the four files into the package. To run the program, you should run the file TextCollage.java, which...
A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...
Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...
Round 1: sequence.c This program should read and execute a list of commands from stdin. Each command and its arguments (if any) will appear on a separate line. For example, if the file "cmdfile" contains the lines: whoami cal 4 2019 echo The time is: date then running 1 /sequence< cmdfile should output your username, a calendar of the month of April, the string "The time is:", and the current date/time, to standard output. Suggested approach: first, make sure you...
Using python 1. Write: A program that implements a logbook, recording the visitors to a place of interest and their visit's purpose. This log book could later be read by another program. Implement a function called log that takes one parameter, filename, the name of a file. You may assume the file is in the current working directory of the program. log should prompt the user for their name arid the nature of their visit. log should then append a...
For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...
Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...
in c prog only please!! Name this program one.c - This program takes two command line arguments: 1. an input filename 2. a threshold Your program will create two files: 1. even.txt - contains all integers from the input file that are even and greater than the threshold 2. odd. txt - contains all integers from the input file that are odd and greater than the threshold • The input file will exist and only contain a set of integers....