繁体   English   中英

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

[英]How to accept space in string in C in the given code

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

记录存储在名为record.dat的文件中

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

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

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

![输出显示记录详细信息,但由于缺少“ Finch”而并非完全正确]->>图像->> 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();
        }

}

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

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

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

#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

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

由于我从未使用过scanf,所以我无法分辨出错误的出在哪里,但也许如果您将代码简化为获取用户输入(-> scanf部分),然后尝试显示它(->字符串在遇到第一个空格时会被截断),我们可以轻松地帮助您更快地解决该问题,并且读起来更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM