-2

我有一个程序可以从输入文件中为您提供送货地址。然而,在其中一个字符串 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);
            }
4

1 回答 1

2

这是一个重大错误:

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

...

fscanf(fpt, "%s", i);

此调用fscanf覆盖其他内存,即使它只读取一个字符也会fscanf 添加第二个字符,即特殊字符串终止字符'\0'

如果您将其他数组的大小与输入文件中字段的大小完全相同,那么您的所有输入都会发生这种情况。

于 2012-10-16T05:34:43.050 回答