C ints正在变成大数

时间:2015-10-10 23:03:17

标签: c int

我有一个程序,它从文本文件中读取数据,并将数据放入一个结构中,以便成为一个人。#34;。除了读取的整数之外的所有工作都不是正确的值。它们的数量非常大。 谢谢你的帮助。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "structs.h"

int main(void) 
{
    FILE *fp = NULL;
    char userInput = ' ';
    struct strPerson people[1];
    int i;
    int aPeople = 2;
    int menuSelect;


    while (fp == NULL) 
    {
        fp = fopen("test", "r"); // Open File To Read
        if (fp == NULL) 
        {
            perror("Error While Loading File\n");
        }
        else
        {

            for(i = 0; i < aPeople; i++)
            {
                fscanf(fp, "%s%s%s%s%s%s%s%s%s%s%s%s%s%d%d%d%d%d%d%d%d%d%d", 
                        people[i].perSurname,
                        people[i].perForname1,
                        people[i].perForname2,
                        people[i].perGender,
                        people[i].perUni.perAwardTitle,
                        people[i].perHomeAddress.perHAddress1,
                        people[i].perHomeAddress.perHAddress2,
                        people[i].perHomeAddress.perHAddress3,
                        people[i].perHomeAddress.perHAddress4,
                        people[i].perLocalAddress.perHAddress1,
                        people[i].perLocalAddress.perHAddress2,
                        people[i].perLocalAddress.perHAddress3,
                        people[i].perLocalAddress.perHAddress4,
                        people[i].perUni.perDOE.nDay,
                        people[i].perUni.perDOE.nMonth,
                        people[i].perUni.perDOE.nYear,
                        people[i].perUni.perDOG.nDay,
                        people[i].perUni.perDOG.nMonth,
                        people[i].perUni.perDOG.nYear,
                        people[i].perUni.perRegNumber,
                        people[i].strDOB.nDay,
                        people[i].strDOB.nMonth,
                        people[i].strDOB.nYear);
            }


            system("clear");
        }
    }

    while (userInput != 'g') 
    {   
        system("clear");
        printf("          |User System|\n"
                "|------------------------------|\n"
                "|------------------------------|\n"
                "|A) Save Current Data To A File|\n"
                "|B) Enter Details              |\n"
                "|C) View Details               |\n"
                "|D) Amend Details              |\n"
                "|E) Search by Award Title      |\n"
                "|F) Search by Surname          |\n"
                "|G) Shut Down                  |\n"
                "|------------------------------|\n");
        printf("Enter Function: ");

        userInput = getchar();
        getchar();

        if (userInput == 'c')
        {
            for(i = 0; i < aPeople; i++)
            {
                printf("%d) %s %s\n", i + 1, people[i].perForname1, people[i].perSurname);
            }
            printf("Select Person To View Details: ");
            scanf("%d", &menuSelect);
            getchar();



                printf("Name: %s %s %s\n",people[menuSelect-1].perForname1,
                                          people[menuSelect-1].perForname2,
                                          people[menuSelect-1].perSurname);
                printf("DOB: %d %d %d\n", people[menuSelect-1].strDOB.nDay,
                                          people[menuSelect-1].strDOB.nMonth,
                                          people[menuSelect-1].strDOB.nYear);

            getchar();
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

您未提供struct strPerson的定义。

但很明显,您没有传递尝试使用fscanf解析的整数的地址。这会调用未定义的行为,并且您的程序实际上完全运行是非常令人惊讶的。

您应该在启用警告的情况下编译以检测此类常见错误:gcc -Wall -W -Werror是您的朋友。

此外,您应该测试fscanf的返回值以验证已解析的字段数。任何未解析的字段都具有不确定的值。

此外,您正在尝试解析2个结构,但结构数组只有1个条目。