Here's for the 6. Not really sure bout this one.
void assignment_6()
{
FILE *pFile;
int i,nLength;
char[] sFilename,sMode,sInput;
char cData;
get_str("Enter File Name:",sFilename,MAX);//Gets filename
get_str("Enter Mode (R/W)",sMode,MAX);//Gets mode
if(strcmp(sMode,"R")==0||strcmp(sMode,"r")==0)//if sMode gets R or r
{
if((pFile=fopen(sFilename, "rt"))!=NULL)//opens file for Reading and checks if it exists
{
do{
cData=fgetc(pFile);//gets the character from the file
printf("%c",cData);//prints cData
}while(cData!=EOF);//while it's not yet the end of the file
fclose(pFile);
/*the reason why I chose fgetc over fgets is because of the bug that fgets has with the EOF. I'm not sure if they already fixed the bug though*/
}
else //if it doesn't exists or something is wrong witht he file
puts("Error Opening File");
}
else if(strcmp(sMode,"W")==0||strcmp(sMode,"w")==0)//if sMode gets W or w
{
if((pFile=fopen(sFilename, "wt"))!=NULL)//Opens the filename for writing
{
do{
nLength=get_str("",sInput,MAX);//gets input from keyboard
for(i=0;i<nLength;i++)//loops the whole string char per char
fputc(pFile,sInput);//enters the whole string char per char except the '\0'
fputc(pFile,'\n');//inserts a line break
}while(strcmp(sInput,EOF)!=0);//loop to detect if the input from keyboard is an EOF
fclose(pFile);//closes the file
}
else//if file is in use
puts("Error Opening File");
}
else//if keyboard input is wrong
puts("Wrong input. Must be R or W only");
}