如何在给定代码中接受C中字符串中的空格

时间:2015-02-25 06:36:12

标签: c string scanf fgets gets

对于以前的代码感到抱歉,我没有正确解释这种情况。 发生的事情是字符串是用空格存储的,但是当我试图在之后读取它时,输出会连续闪烁(就像它一遍又一遍地读取它并在其上无限地打印它一样。)

记录存储在名为record.dat

的文件中

我正在插入图像,显示我在文件中输入新记录后存储的文件内容(此处所有内容都正确存储)

![这些是我插入新记录后的record.dat的内容] - >图像 - > http://i.stack.imgur.com/YxGEd.png

现在,当我尝试在我的应用程序中查看此记录的详细信息时,我得到了这个。

![输出显示记录详细信息但不完全正确为"雀"缺少] - >>图像 - >> http://i.stack.imgur.com/zgmGY.png

之后,如果我关闭应用程序,然后再次启动它并尝试读取它显示的先前数据:

![此输出也像以前一样闪烁,但现在甚至数据都消失了] - >>图像 - >> http:// i.stack.imgur.com/ 4q0qb.png

(以防万一你想知道 - 数据仍然在record.dat文件中 - 我在我创建记录时输入的所有内容。)

我希望问题现在更清楚了.....

void see(void)
{
FILE *ptr;
int test=0,rate;
int choice;
float time;
float intrst;
char c;

ptr=fopen("record.dat","r");
printf("Do you want to check by\n1.Account no\n2.Name\nEnter your choice:");

//Selection Choice with Validation
int once = 0;
do
{   
    if(once!=0)
    {
        printf("\nThe choice is invalid.\nEnter 1 for account number and 2 for name");
    }
    once = once+1;
} while ((scanf("%d%c", &choice, &c)!=2 || c!='\n') && clean_stdin());


if (choice==1)
{   
    //Account Number with Validation
    once = 0;
    do
    {   
        if(once!=0)
        {
            printf("\nThe above account number is invalid.\nEnter an account number (numeric value only):");
        }
        else
        {
            printf("\nEnter an account number:");
        }

        once = once+1;
    } while ((scanf("%d%c", &check.acc_no, &c)!=2 || c!='\n') && clean_stdin());

    while (fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizenship,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
    {
        if(add.acc_no==check.acc_no)
        {   system("cls");
            test=1;

            printf("\nAccount NO.:%d\nName:%s \nDOB:%d/%d/%d \nAge:%d \nAddress:%s \nCitizenship No:%s \nPhone number:%.0lf \nType Of Account:%s \nAmount deposited:$ %.2f \nDate Of Deposit:%d/%d/%d\n\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.phone,
            add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
            if(strcmpi(add.acc_type,"fixed1")==0)
                {
                    time=1.0;
                    rate=9;
                    intrst=interest(time,add.amt,rate);
                    printf("\n\nYou will get $%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+1);
                }
            else if(strcmpi(add.acc_type,"fixed2")==0)
                {
                    time=2.0;
                    rate=11;
                    intrst=interest(time,add.amt,rate);
                    printf("\n\nYou will get $.%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+2);

                }
            else if(strcmpi(add.acc_type,"fixed3")==0)
                {
                    time=3.0;
                    rate=13;
                    intrst=interest(time,add.amt,rate);
                    printf("\n\nYou will get $.%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+3);

                }
             else if(strcmpi(add.acc_type,"saving")==0)
                {
                    time=(1.0/12.0);
                    rate=8;
                    intrst=interest(time,add.amt,rate);
                    printf("\n\nYou will get $.%.2f as interest on %d of every month",intrst,add.deposit.day);

                 }
             else if(strcmpi(add.acc_type,"current")==0)
                {

                    printf("\n\nYou will get no interest\a\a");

                 }

        }
    }
}
else if (choice==2)
{   
    printf("Enter the name:");
    scanf("%s",&check.name);

    while (fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizenship,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
    {
        if(strcmpi(add.name,check.name)==0)
        {   system("cls");
            test=1;
            printf("\nAccount No.:%d\nName:%s \nDOB:%d/%d/%d \nAge:%d \nAddress:%s \nCitizenship No:%s \nPhone number:%.0lf \nType Of Account:%s \nAmount deposited:$%.2f \nDate Of Deposit:%d/%d/%d\n\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.phone,
            add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
            if(strcmpi(add.acc_type,"fixed1")==0)
                {
                    time=1.0;
                    rate=9;
                    intrst=interest(time,add.amt,rate);
                    printf("\n\nYou will get $.%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+1);
                }
            else if(strcmpi(add.acc_type,"fixed2")==0)
                {
                    time=2.0;
                    rate=11;
                    intrst=interest(time,add.amt,rate);
                    printf("\n\nYou will get $.%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+2);

                }
            else if(strcmpi(add.acc_type,"fixed3")==0)
                {
                    time=3.0;
                    rate=13;
                    intrst=interest(time,add.amt,rate);
                    printf("\n\nYou will get $.%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+3);

                }
             else if(strcmpi(add.acc_type,"saving")==0)
                {
                    time=(1.0/12.0);
                    rate=8;
                    intrst=interest(time,add.amt,rate);
                    printf("\n\nYou will get $.%.2f as interest on %d of every month",intrst,add.deposit.day);

                 }
             else if(strcmpi(add.acc_type,"current")==0)
                {

                    printf("\n\nYou will get no interest\a\a");

                 }

        }
    }
}


fclose(ptr);
 if(test!=1)
    {   system("cls");
        printf("\nRecord not found!!\a\a\a");
        see_invalid:
          printf("\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
          scanf("%d",&main_exit);
          system("cls");
             if (main_exit==1)
                menu();
            else if (main_exit==2)
                close();
            else if(main_exit==0)
                see();
            else
                {
                    system("cls");
                    printf("\nInvalid!\a");
                    goto see_invalid;}
    }
else
    {printf("\nEnter 1 to go to the main menu and 0 to exit:");
    scanf("%d",&main_exit);}
    if (main_exit==1)
    {
        system("cls");
        menu();
    }

    else
       {

         system("cls");
        close();
        }

}

3 个答案:

答案 0 :(得分:1)

推荐的解决方案是不使用scanf,因为scanf也容易受到缓冲区溢出的影响。在任何情况下,您实际上并没有使用scanf调用进行任何格式化。您可以使用fgets将输入读入字符串并指定允许的最大字符数。使用以下内容:

printf("\nEnter the name: ");
fgets(add.name, MAX_NAME_SZ, stdin);

答案 1 :(得分:0)

您可以使用%[^...]格式说明符将空格读入字符串。这是一个示例程序,希望能为您提供一些工作。

#include <stdio.h>

int main()
{
   char const* line = "This is a string, 10";
   char text[50];
   int number;

   // Read up to 49 characters not including ',' to text.
   sscanf(line, "%49[^,], %d", text, &number);

   printf("%s, %d\n", text, number);
   return 0;
}

输出:

This is a string, 10

答案 2 :(得分:0)

我认为你应该提供一个失败情况的例子,或者至少清理代码,只给我们一些错误的部分。 我看到很多printf在没有\ n的情况下使用,我知道这可能是一个问题(这也可能完全脱离了主题)。

由于我从未使用过scanf,因此无法确定您的错误在哪里,但是如果您将代码缩减为获取用户的输入( - &gt; scanf部分),则尝试显示它( - &gt; ;你的字符串在遇到第一个空格时被截断的部分)我们可以很容易地帮助你更快地解决这个问题,并且可以更好地阅读。