Thanks Rouss. I almost completed option 3. I converted the text file to lowercase. However, I now need to write the lowercase text to a new file and allow the user to name the output file. I used fputs, but it's not copying the file. Do I need to use the fopen statement again with the "w"?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define FLUSH while(getchar() != '\n')
FILE *create(FILE*); /*function prototype*/
void character(FILE*);
void lower(FILE*);
void main()
{
int ch;
FILE *editor;
FILE*option2;
FILE*option3;
editor=NULL;
option2=NULL;
option3=NULL;
int calls[7] = {0};
ch = 0;
while(ch != -1)
{
printf("\n\nPlease choose an option below by typing a number between 1 and 8.\n\n");
printf("1\tCreate text file\n\n");
printf("2\tCount Characters,words and sentences\n\n");
printf("3\tConvert to lowercase\n\n");
printf("4\tConvert to uppercase\n\n");
printf("5\tEncrypt text\n\n");
printf("6\tDecrypt text\n\n");
printf("7\tDisplay text file\n\n");
printf("8\tExit program\n\n");
scanf(" %d", &ch);
FLUSH;
if(ch > 0 && ch < 8) calls[ch-1]++;
switch(ch) {
case 1:
editor=create(editor);
break;
case 2:
character(option2);
break;
case 3:
lower(option3);
break;
}
}
fclose(editor);
return;
}
FILE *create(FILE *fname)
{
char fileName[13];
char text[82];
printf("Enter the name of file to create: ");
fgets(fileName, sizeof(fileName), stdin);
fileName[strlen(fileName)-1] = '\0';
fname = fopen(fileName, "w");
if(fname == NULL)
{
printf("\nThe file cannot be opened.");
printf("\nPlease check that the file exists.");
exit(1);
}
printf("Enter the text to write to the file: ");
fgets(text, sizeof(text), stdin);
text[strlen(text)-1] = '\0';
fprintf(fname, "%s", text);
fclose(fname);
return fname;
}
void character(FILE *fname)
{
char text[81];
char fileName[13];
int count,count1,i,j,k;
count=0,count1=0, i=0, j=0,k=0;
printf("\nPlease enter the name of the file you wish to open: ");
fgets(fileName, sizeof(fileName), stdin);
fileName[strlen(fileName)-1]='\0';
fname=fopen(fileName, "r");
if (fname==NULL)
{
printf("\nThe file cannot be opened.");
printf("\nPlease check that the file exists.");
return;
}
while (fgets(text,81,fname) !=NULL)
printf("\n%s", text);
for (i=0; text[i]!='\0';++i)
{
count++;
}
printf("\n\nThe file you just opened: %s, has: %d characters, ", fileName,count);
int charNum = 0;
int numWords = 0;
int inWord = 0;
while (text[j] != '\0')
{
if ((text[j] == ' ') || (text[j] == '\t') ||
(text[j] == '\n'))
{
inWord = 0;
}
else if (inWord == 0)
{
++numWords;
inWord = 1;
}
++j;
}
printf("%d words, and ", numWords);
for (k=0; text[k] !='\0';++k)
{
if (text[k]=='.')
{
count1++;
}
}
printf("%d sentences.\n", count1);
}
void lower(FILE *fname)
{
char text[81];
char fileName[13];
int c;
printf("\nPlease enter the name of the file you wish to open: ");
fgets(fileName, sizeof(fileName), stdin);
fileName[strlen(fileName)-1]='\0';
fname=fopen(fileName, "r");
if (fname==NULL)
{
printf("\nThe file cannot be opened.");
printf("\nPlease check that the file exists.");
return;
}
while ((c=getc(fname)) != EOF)
putchar(tolower(c));
while (fgets(text,81,fname) !=NULL)
printf("\n%s",text);
printf("\nWhat name would you like to call the new lowercase file: ");
fgets(fileName, sizeof(fileName), stdin);
fileName[strlen(fileName)-1]='\0';
fputs(fileName,fname);
}
****************************boss1.txt************* ***********************
Lets try this one more time.I want three sentences.This is the third one.
************************************************** ***********************
/*
Please choose an option below by typing a number between 1 and 8.
1 Create text file
2 Count Characters,words and sentences
3 Convert to lowercase
4 Convert to uppercase
5 Encrypt text
6 Decrypt text
7 Display text file
8 Exit program
3
Please enter the name of the file you wish to open: boss1.txt
lets try this one more time.i want three sentences.this is the third one.
Please choose an option below by typing a number between 1 and 8.
1 Create text file
2 Count Characters,words and sentences
3 Convert to lowercase
4 Convert to uppercase
5 Encrypt text
6 Decrypt text
7 Display text file
8 Exit program
*/