Originally Posted by
cherryduck
Still getting nowhere I'm afraid
I'll post all my code as it is. There are several other issues that need fixing as well if you're feeling particularly helpful
If you point out specific issues I or someone may have time to look (eg, I did not read your todo.txt...I have one of my own )
Anyway, my solution with chucking a string at the end of the scanf did not work so well in this circumstance. So I wrote this based on what you have around line 166. It should be the real foolproof method:
Code:
#include <stdio.h>
#include <string.h>
int getfilename(char *name) {
int r;
char buffer[128];
printf("\nSave file to : ");
fgets(buffer,128,stdin);
r=sscanf(buffer,"%15[a-zA-Z0-9_%-.=+]",name);
return r;
}
int main(void) {
int r;
char saveName[20];
char accept;
while (!r) r = getfilename(saveName);
strcat(saveName, ".txt");
printf("Filename is : \"%s\"\n",saveName);
printf("Accept? Type Y for yes or N for no : ");
scanf("%c",&accept); /* notice %c not %s! */
printf("\nThe answer was ->%c<-\n",accept);
return 0;
}
That compiles and runs -ansi, try it to see how it works. The comment in red is VERY IMPORTANT as otherwise you will be GUARANTEED to create a buffer overflow, which could account for other subsequent problems (including potential seg faults). Currently, you are making that mistake! Also, this still leaves a newline in the input buffer, which does not matter in the context of this code but it may in yours...
Notice I used quotes around the variables in printf, which helps indicate when there is a newline on the end -- you will get:
The filename is: "myfile
"
Altho with the above code that will not happen. Writing short, specific, potentially recyclable routines like "getfilename" is a good way to work, BTW. It also helps keep your code organized and comprehensible.