WOULD YOU PLEASE EXPLAIN THE CODE : THANKS Write a program that works as: The pr
ID: 3528941 • Letter: W
Question
WOULD YOU PLEASE EXPLAIN THE CODE : THANKS
Write a program that works as: The program starts by printing your name with an end sign ">". For example." ... >"; Then, you can type in a string. If the string is not "vi xxx", you print "I don't know this command." You keep getting a string until you get a string starting with "vi". For example, "vi myp.c". You program will then open "myp.c" for reading. If the file does not exist, then there is nothing to read. Read the file into a fixed array of "mytext[1000][ 81]". Presumably, the "myp.c" text file is not too large with each line 80 characters or fewer. If there are more characters than 80 in a line, you can just ignore the characters beyond 80. Close your file. Change all your texts to upper case, and display the first 20 lines on your screen. Then, your program will wait for a command from the keyboard. If you type ":w". your program will open the same file for writing, and write your array of text into that file. If you type ":q!". your program will quite. If your type ":w myp1.c", your program will open "myp1.c" and write into that file. You should always close your file after finish. Remember to use function calls instead of a program of one function. Each function should be shorter than two pages.Explanation / Answer
please rate - thanks
hope this helps
#include <stdio.h>
#include<string.h>
//function prototypes
void getfilename(char[],char[]);
void decodefilename(char[],char[]);
int openfile(char[],FILE**);
void openfilewrite(char[],FILE**);
void inputdata(char[][81],FILE**,int*);
void convert(int,char[][81]);
void display(int,char[][81]);
void writedata(int,char[][81],FILE**);
int getcommand(char[]);
int main()
{// define pointers to input and output files
FILE *in;
FILE *out;
char input[21],mytext[1000][81];
char filename[21];
int lines,command;
printf("My Name>"); //prompt for name
getfilename(filename,input); //get name-use getline in case it has blanks
decodefilename(input,filename); //extract the filename from the line that was input
if(openfile(filename, &in)==1) //only continue if open is successful
{inputdata(mytext,&in,&lines); //input the data
fclose(in); //close input file
convert(lines,mytext); //convert to all upper case
display(lines,mytext); //display 20 lines to the screen
command=getcommand(filename); //get command
if(command==0) //check for command validity
printf("Invalid command ");
else if(command==1) // command was :w
{openfilewrite(filename,&out); //so open output file
writedata(lines,mytext,&out); //output the data
fclose(out); //and close the file
}
else //command was :q
return 0; //so exit
}
}
void writedata(int lines,char mytext[][81],FILE** out) //write the data to the output file
{int i,j;
for(i=0;i<lines;i++) //all lines of data
fprintf(*out,"%s ",mytext[i]);
}
int getcommand(char filename[]) //get command
{int command;
char input[21];
printf("enter command"); //prompt for the command
fgets(input,81,stdin); //input it
if(input[0]!=':') //if doesn't start with :
return 0; //return an error condition code
if(input[1]=='q'&&input[2]=='!') //if it's q!
return 2; //return code saying to quit
if(input[2]==' ') //it's w with no filename, so send code
return 1; //to write file, and filename remains the original file name
decodefilename(input,filename); //if new filename extract it and return
return 1; //with code to write file, and new file to write to
}
void display(int lines,char mytext[][81]) //display at most 20 lines to screen
{int last,i;
if(lines<=20) //display at most 20 lines //only display 20 line
last=lines; //so if less than 20 lines in file, only display as many as there are
else
last=20; //if more than 20 lines, only display first 20
for(i=0;i<last;i++) //display the lines
printf("%s ",mytext[i]);
}
void convert(int lines,char mytext[][81]) //convert to uppercase
{int i,j;
for(i=0;i<lines;i++) //for each line
for(j=0;j<strlen(mytext[i]);j++) //check each character
if(islower(mytext[i][j])) //if it's lower case
mytext[i][j]=toupper(mytext[i][j]); //change it to upper case
}
void inputdata(char mytext[][81],FILE** in,int *lines) //input data from file
{int j,i;
char c;
i=0;
j=0;
c=fgetc(*in); //get 1 character at a time
while(i<1000&&c!=EOF) //for each line, at most 1000
{
mytext[i][j]=c; //get a character
if(mytext[i][j]==' ') //done? //if its an enter (end of line)
{mytext[i][j]=''; //last character must be null, so make it null
j=0; //start next line
i++;
}
else
j++;
if(j==81) //line too long?--at most 80 characters per line
{while(fgetc(*in)!= ' '); //if anything left in input buffer get rid of it
mytext[i][80]=''; //put null at end of string;
i++; //go to next line
j=0; //start character count for the new line at 0
}
c=fgetc(*in);
}
*lines=i; //return the number of lines that were input
//printf("%d****%d ",i,*lines); //used for debugging
}
int openfile(char filename[],FILE** in)
{*in = fopen(filename,"r"); //open the file
if(*in == NULL) //if it doesn't open
{ printf("Unable to open input file: %s ",filename); //report an error
getch(); //wait for user to hit a key, so error message can be seen
return 0; //and return error code
}
return 1; //return code that the file is open
}
void openfilewrite(char filename[],FILE** out)
{*out = fopen(filename,"w"); //open file to write
}
void decodefilename(char input[],char filename[])
{int i,j;
i=2;
while(input[i]==' ') //skip blanks
i++; //get filename 1 character at a time
j=0;
while(input[i]!='') //do until end of filename
{filename[j]=input[i];
j++; //next character position in filename
i++; //next character in input buffer
}
filename[--j]=''; //add null to end of filename
}
void getfilename(char filename[],char input[])
{
fgets(input,81,stdin);
while(input[0]!='v'||input[1]!='i') //must start with vi
{ printf("I don't know that command "); //dodn't start with vi so error
printf("please reenter filename: ");
fgets(input,80,stdin); //keep getting until starts with vi
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.