Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Group 2 Program 1
- Paul Macclean
- Mike Gorse
- Robert Breckenridge
- CSC 460
- Language Translations
- Program Description:
- */
- #include <stdio.h>
- typedef enum {false,true} logical;
- char INFILE_EXTENSION[] = ".in";
- void appendExtension(char *FileName,char *DesiredExtension) {
- //Get a pointer to extension (Last occurence of '.')
- char* filename_extension = strrchr(FileName, '.');
- //If no extension, OR if extension is **NOT** the expected infile extension:
- if (filename_extension == NULL) {
- //Append extension
- strcat(FileName, INFILE_EXTENSION);
- }
- }
- int getInfile(FILE **InFile, char *FileName, logical PromptFile) {
- logical fileexists = false;
- logical quit = false;
- do {
- if (PromptFile) {
- printf("Please enter an input file: ");
- scanf_s("%s", FileName, 100);
- }
- //if input is 'q'
- if (tolower(FileName[0]) == 'q')
- quit = true;
- else {
- appendExtension(FileName, INFILE_EXTENSION);
- //Open the file
- fopen_s(InFile, FileName, "r");
- if (*InFile == NULL) {
- printf("That file does not exist!");
- fileexists = false;
- PromptFile = true;
- }
- else {
- fileexists = true;
- }
- }
- } while (fileexists == false && quit == false);
- return quit; //indicate whether the user quit or not
- }
- int getOutfile(FILE** OutFile, char* FileName, logical PromptFile) {
- logical fileexists = false;
- logical quit = false;
- do {
- if (PromptFile) {
- printf("Please enter an output file: ");
- scanf_s("%s", FileName, 100);
- }
- //if input is 'q'
- if (tolower(FileName[0]) == 'q')
- quit = true;
- else {
- appendExtension(FileName, INFILE_EXTENSION);
- //Open the file
- fopen_s(OutFile, FileName, "r");
- if (*OutFile == NULL) {
- printf("That file does not exist!");
- fileexists = false;
- PromptFile = true;
- }
- else {
- fileexists = true;
- }
- }
- } while (fileexists == false && quit == false);
- return quit; //indicate whether the user quit or not
- }
- int main(int argc, char *argv[]) {
- logical promptinfile = true;
- logical promptoutfile = true;
- FILE *infile = NULL;
- char inputname[100] = { 0 };
- char outputname[100] = { 0 };
- if (argc > 1) {
- promptinfile = false;
- strcpy(inputname, argv[1]);
- }
- if (argc > 2) {
- promptoutfile = false;
- strcpy(outputname, argv[2]);
- }
- logical userquit = getInfile(&infile, inputname, promptinfile);
- if (userquit == true) {
- printf("Coward.\n");
- }
- else {
- //get output file thingo
- //int userquit = getOutfile(&infile, inputname, promptinfile, "Please enter an input file: ");
- }
- if (infile != NULL) {
- fclose(infile);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement