Skip to content

Instantly share code, notes, and snippets.

@BillMoriarty
Last active October 20, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BillMoriarty/97b485ca41a5e272d25c62b8487f73be to your computer and use it in GitHub Desktop.
Save BillMoriarty/97b485ca41a5e272d25c62b8487f73be to your computer and use it in GitHub Desktop.
C command line: get string for file input name, get string for file name output
/* Hi all - I am currently a student. The code below was developed during student projects. It may not be the most efficient or robust.
Use it if it's helpful for you.
-Bill Moriarty
*/
#include <stdio.h>
#define BUF_SIZE 64
/* get the user's input and output filename from the keyboard */
char inKey[BUF_SIZE];
char outKey[BUF_SIZE];
FILE *inFile;
FILE *outFile;
printf("input filename >");
fgets(inKey, BUF_SIZE, stdin);
printf("output filename >");
fgets(outKey, BUF_SIZE, stdin);
/* call method to remove the new line character */
remove_trailing_space(inKey);
remove_trailing_space(outKey);
printf("reading '%s' for input\n", inKey);
printf("writing '%s' for output\n", outKey);
/* check that the files opened correcty */
if ((inFile=fopen(inKey, "r"))==NULL) {
printf("error reading %s for input\n", inKey);
return 1;
} /* end if */
if ((outFile=fopen(outKey, "w"))==NULL) {
printf("error reading %s for output\n", outKey);
return 1;
} /* end if */
/* a function to remove trailing end of line character*/
void remove_trailing_space(char s[]) {
int len=strlen(s);
int i;
i=len-1;
while (i>=0 && isspace(s[i])) {
s[i]='\0';
i--;
}
} /* end void remove_trailing_space(char s[]) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment