Code after modifying according to Tater's original post:
Code:
//This one fails to quit when 'q' is entered
#include <stdio.h>
int main (void)
{
char linebuf[200], inputName[64];
FILE *in;
char command = 'a';
int i;
printf("Enter name of file to be copied: ");
scanf("%63s", inputName);
printf("\n");
if ( (in = fopen(inputName, "r")) == NULL) {
printf("Can't open %s for reading.\n", inputName);
return 1;
}
do {
for (i = 0; i < 20; ++i) {
fgets(linebuf, 20, in);
printf("%s", linebuf);
}
printf("Enter a character (q to quit): ");
scanf("%c", &command);
getchar();
} while (command != 'q');
return 0;
}
The code after following both scanf calls with getchar();
Code:
//This one fails to quit when 'q' is entered after a few cycles
#include <stdio.h>
int main (void)
{
char linebuf[200], inputName[64];
FILE *in;
char command = 'a';
int i;
printf("Enter name of file to be copied: ");
scanf("%63s", inputName);
getchar();
printf("\n");
if ( (in = fopen(inputName, "r")) == NULL) {
printf("Can't open %s for reading.\n", inputName);
return 1;
}
do {
for (i = 0; i < 20; ++i) {
fgets(linebuf, 20, in);
printf("%s", linebuf);
}
printf("Enter a character (q to quit): ");
scanf("%c", &command);
getchar();
} while (command != 'q');
return 0;
}
Code:
/*This one seems to loop through two or three times, prompt the user for
* input and simply prompt the user again, but it does quit when 'q' is entered. */
#include <stdio.h>
int main (void)
{
char linebuf[200], inputName[64];
FILE *in;
char command = 'a';
int i = 0;
printf("Enter name of file to be copied: ");
scanf("%63s", inputName);
getchar();
printf("\n");
if ( (in = fopen(inputName, "r")) == NULL) {
printf("Can't open %s for reading.\n", inputName);
return 1;
}
do {
while (i < 20 && fgets(linebuf, 20, in) != NULL)
printf("%s", linebuf);
printf("Enter a character (q to quit): ");
scanf("%c", &command);
getchar();
} while (command != 'q');
return 0;
}
I know this is really elementary stuff, so I'm sorry to trouble people with it. I appreciate your help immensely!