我正在尝试编写一个程序,该程序从用户读取文件名输入,将其存储在“名称”中,然后稍后将检查输入的文件名是否在当前目录中。我遇到的问题是,当用户输入目录中不存在的文件的文件名时,然后一个名称比以前长的文件名可以工作,但是如果用户首先输入一个长的不存在的文件名然后确实存在的较短的
stat(name, &check)<0
并没有很好。我想知道是否因为第二个实例中的 'name' 是一个具有正确文件名的数组,但是 '\0's 将其填充到之前输入的较大错误尝试的大小与比较文件名有关。有什么方法可以在不使用 fflush 的情况下清除每次尝试的“名称”?
char *filename(int *valid_input)
{
/* Need to malloc name, dat and prefix if doing it this way. */
int valid1, valid2, valid3, i, n;
char c, *name;
char dat[5], prefix[16];
struct stat check;
name = malloc(14*(sizeof(char)));
if(name==NULL)
{
printf("Memory could not be allocated.");
exit(EXIT_FAILURE);
}
printf("\nPlease enter a filename in the form 'yourfile.dat'.\nUse only lowercase letters a-z and numerals 0-9 in the prefix. \n The prefix should be 10 characters or less.\nIf you wish to quit enter 'q'.\n\nInput filename : ");
*valid_input = 0;
while(*valid_input == 0)
{
valid1=0; valid2=0; valid3=0;
__fpurge(stdin);
printf("hello1");
while(valid1==0) /* Checks input is at least 5 characters long, 14 maximum. */
{
printf("hello2");
n = 0;
valid1 = 1;
__fpurge(stdin);
while ((c=(char)getchar()) != '\n') /* Reads in input */
{
printf("hello3");
if(n<14)
{
name[n]=c;
/*printf("%c",name[n]); */
if((name[n]=='\n')||(name[n]==EOF)||(name[n]=='\0')) break;
}
++n;
}
/* printf("check"); */
if(((n>=14)&&(name[14]!='t'))||(n<5)){printf("hello1"); valid1 = 0;} /* Checks input has a prefix and is less than 14 characters total */
if((name[0]=='q')&&(n==1)){printf("hello"); break;}
if(valid1 > 0) break;
/* printf("Length of name = %d n = %d ",strlen(name),n);*/
printf("\nYour filename should contain a prefix of up to 10 characters. \nTo quit press 'q'.\n\nInput filename : ");
}
if((name[0]=='q')&&(n==1)) break;
for (i=0;i<(n-4);i++)
{
prefix[i]=name[i];
printf("\nprefixvalue = %c",prefix[i]);
if(((prefix[i]>='a')&&(prefix[i]<='z'))||((prefix[i]>='0')&&(prefix[i]<='9'))){ valid3+=1;}
}
if(valid3!=n-4) valid3=0;
else valid3=1;
for (i=0;i<4;i++)
{
dat[i]=name[n-4+i];
if((dat[0]='.')||(dat[1]='d')||(dat[2]='a')||(dat[3]='t')) valid2=1;
}
if((valid2==0)||(valid3==0)) printf("\nYour filename should be in the form 'yourfile.dat' with only lowercase letters or numbers in the prefix.\nTo quit press q.\n\nInput filename : ");
*valid_input = valid1 && valid2 && valid3;
if(*valid_input==1)
{
if(stat(name, &check)<0)
{
printf("\n File does not exist in the current directory.\n Check and re-enter filename.\n To quit press q.\n\nfilename : "); *valid_input=0;
}
}
}
return(name);