在C中向字符串添加不需要的字符

在C中向字符串添加不需要的字符,c,arrays,string,strcat,C,Arrays,String,Strcat,我有一个程序,可以从输入文件中为您提供装运地址。但是,在其中一个字符串的开头,order.add_one,一个数字被添加到字符串的开头,该数字每次都相当于变量“choice”。为什么要这样做 #include <stdio.h> #include <math.h> #include <string.h> //structure typedef struct {char cust_name[25]; char cust_id[3]; char add_on

我有一个程序,可以从输入文件中为您提供装运地址。但是,在其中一个字符串的开头,order.add_one,一个数字被添加到字符串的开头,该数字每次都相当于变量“choice”。为什么要这样做

#include <stdio.h>
#include <math.h>
#include <string.h>

//structure
typedef struct
{char cust_name[25];
 char cust_id[3];
 char add_one[30];
 char add_two[30];
 char bike;
 char risky;
 int number_ordered;
 char cust_information[500];
 }ORDER;

 ORDER order;

int main(void){
fflush(stdin);
system ( "clear" );



        FILE *fpt;
        fpt = fopen("input.txt", "r");

            if (fpt==NULL){
                printf("Text file did not open\n");
                return 1;
                }

                printf("Enter Customer ID: ");
                scanf("%s", &order.cust_id);

                char choice;
                choice = order.cust_id[0];

                char x[3];
                int w, u, y, z;  
                char a[10], b[10], c[10], d[10], e[20], f[10], g[10], i[1], j[1];
                int h;
                printf("%s value of c", c);

                if (choice >='1'){
            while  ((w = fgetc(fpt)) != '\n' ){
            }
            }
                if (choice >='2'){
                while  ((u = fgetc(fpt)) != '\n' ){
                }
                }
                if (choice >='3'){
                while  ((y = fgetc(fpt)) != '\n' ){
                }
                }
                if (choice >= '4'){
                while  ((z = fgetc(fpt)) != '\n' ){
                }
                }
                printf("\n");
                fscanf(fpt, "%s", x);

                    fscanf(fpt, "%s", a);
                    printf("%s", a);
                    strcat(order.cust_name, a);

                    fscanf(fpt, " %s", b);
                    printf(" %s", b);
                    strcat(order.cust_name, " ");
                   strcat(order.cust_name, b);

                    fscanf(fpt, "%s", c);
                    printf(" %s", c);
                    strcat(order.add_one, "\0");
                    strcat(order.add_one, c);

                    fscanf(fpt, "%s", d);
                    printf(" %s", d);
                    strcat(order.add_one, " ");
                    strcat(order.add_one, d);

                    fscanf(fpt, "%s", e);
                    printf(" %s", e);
                    strcat(order.add_two, e);

                    fscanf(fpt, "%s", f);
                    printf(" %s", f);
                    strcat(order.add_two, " ");
                    strcat(order.add_two, f);

                    fscanf(fpt, "%s", g);
                    printf(" %s", g);
                    strcat(order.add_two, " ");
                    strcat(order.add_two, g);
                    strcat(order.add_two, "\0");

                    fscanf(fpt, "%d", &h);
                    printf(" %d", h);
                    order.number_ordered = h;

                    fscanf(fpt, "%s", i);
                    printf(" %s", i);
                    order.bike = i[0];

                    fscanf(fpt, "%s", j);
                    printf(" %s", j);   
                   order.risky = j[0];
                  fclose(fpt);

                  printf("%s %s %s %d %c %c", order.cust_name, order.add_one, order.add_two, order.number_ordered, order.bike, order.risky);
            }
#包括
#包括
#包括
//结构
类型定义结构
{char cust_name[25];
char cust_id[3];
char add_one[30];
char add_two[30];
炭自行车;
高风险;
订购的整数;
字符客户信息[500];
}秩序;
订单;
内部主(空){
fflush(stdin);
系统(“清除”);
文件*fpt;
fpt=fopen(“input.txt”,“r”);
如果(fpt==NULL){
printf(“文本文件未打开\n”);
返回1;
}
printf(“输入客户ID:”);
scanf(“%s”和order.cust\u id);
字符选择;
choice=order.cust_id[0];
charx[3];
int w,u,y,z;
字符a[10],b[10],c[10],d[10],e[20],f[10],g[10],i[1],j[1];
int-h;
printf(“c的%s值”,c);
如果(选项>='1'){
而((w=fgetc(fpt))!='\n'){
}
}
如果(选项>='2'){
而((u=fgetc(fpt))!='\n'){
}
}
如果(选项>='3'){
而((y=fgetc(fpt))!='\n'){
}
}
如果(选项>='4'){
而((z=fgetc(fpt))!='\n'){
}
}
printf(“\n”);
fscanf(fpt,“%s”,x);
fscanf(fpt,“%s”,a);
printf(“%s”,a);
strcat(订单.客户名称,a);
fscanf(fpt,“%s”,b);
printf(“%s”,b);
strcat(order.cust_name,”);
strcat(订单、客户名称、b);
fscanf(fpt,“%s”,c);
printf(“%s”,c);
strcat(order.add_one,“\0”);
strcat(订单添加一个,c);
fscanf(fpt,“%s”,d);
printf(“%s”,d);
strcat(order.add_one,“”);
strcat(订单添加一个,d);
fscanf(fpt,“%s”,e);
printf(“%s”,e);
strcat(订单号:2,e);
fscanf(fpt,“%s”,f);
printf(“%s”,f);
strcat(order.add_two,“”);
strcat(订单添加两个,f);
fscanf(fpt,“%s”,g);
printf(“%s”,g);
strcat(order.add_two,“”);
strcat(订单加两个,g);
strcat(order.add_two,“\0”);
fscanf(fpt、%d、&h);
printf(“%d”,h);
order.number_ordered=h;
fscanf(fpt,“%s”,i);
printf(“%s”,i);
order.bike=i[0];
fscanf(fpt,“%s”,j);
printf(“%s”,j);
order.risk=j[0];
fclose(fpt);
printf(“%s%s%s%d%c%c”,order.cust\u name,order.add\u one,order.add\u two,order.number\u order,order.bike,order.risk);
}
这里有一个主要错误:

char ..., i[1], ...

...

fscanf(fpt, "%s", i);
调用
fscanf
将覆盖其他内存,即使它只读取一个字符
fscanf
添加第二个字符,特殊字符串终止字符
'\0'


如果其他数组的大小与输入文件中字段的大小完全相同,然后你的所有输入都会出现这种情况。

你需要缩小你的错误范围-发布整个程序并让我们进行研究以找出错误是不可接受的。在你的100条
printf
语句中,哪一条显示了错误?同样
1==1
看起来是这样的。你真的应该试着理解语句在做什么,而不是照搬以前的做法。。这将使您的生活更加轻松。当您收到错误时,您将在程序中输入哪些数据?在
printf(“%s”,c)
语句中打印数组时,数组
c
不会初始化。