我的文件读取有问题,使用结构

时间:2014-03-21 05:02:47

标签: c arrays file-io struct

我以正确的方式得到了我的结构,看起来很多这样的工作。但是我的字符串在我的文件中读取这个垃圾时遇到了一个小问题。我想提取一个名字,日期和状态。并且它在实际日期,名称或状态之前不断产生这种奇怪形式的“]] []]]] [[[[[[”)。如何在不获得垃圾反馈的情况下让我的struct读取文件。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstring>
#include <exception>


//functions called
void get_info(int id[], char name[][20], char state[][5], char dis_code[], float balance[], char due_date[][40]);
void print_list(int id[], char name[][20], char state[][5], char dis_code[], float balance[], char due_date[][40]);

//why is it void?
int main(void)
{   

    FILE *pFile;
    int choice = 0;
    char buf[40];


    /*
    int id[sizeof(buf)];
    char name[sizeof(buf)][20];
    char state[sizeof(buf)][5];
    char dis_code[sizeof(buf)];
    float balance[sizeof(buf)];
    char due_date[sizeof(buf)][40];
    */

    struct fileinfo
    {
        int id[40];
        char name[40][20];
        char state[40][5];
        char dis_code[40];
        float balance[40];
        char due_date[40][40];
    } info;

    printf("WELCOME. \n");

    while (choice != 5)
    {

        printf("press number for action\n");
        printf("OPEN FILE - 1 \n");
        printf("LIST THE TABLE - 2 \n");
        printf("SEARCH THE TABLE -3 \n");
        printf("ADD A NEW ROW - 4 \n");
        printf("EXIT THE PROGRAM - 5 \n");
        gets_s(buf);
        choice = atoi(buf);

        if (choice == 1)
        {
            pFile = fopen("ASSIGNV1.dat", "r");
            if (pFile != NULL)
            {
                int i = 0;
                for (i = 0; i < 8; i++)
                {
                    //get id
                    fgets(buf, sizeof(buf), pFile);
                    info.id[i] = atoi(buf);

                    //get name
                    fgets(buf, sizeof(buf), pFile);
                    info.name[i][strlen(info.name[i]) - 1] = '\0';

                    //get state
                    fgets(buf, sizeof(buf), pFile);
                    info.state[i][strlen(info.state[i]) - 1] = '\0';

                    //get discount code
                    fgets(buf, sizeof(buf), pFile);
                    info.dis_code[i] = buf[0];

                    //get balance
                    fgets(buf, sizeof(buf), pFile);
                    info.balance[i] = atof(buf);

                    //get due date
                    fgets(buf, sizeof(buf), pFile);
                    info.due_date[i][strlen(info.due_date[i]) - 1] = '\0';


                    printf("ID \t\t %i \n", info.id[i]);
                    //problem with name
                    printf("NAME \t\t %s \n", info.name[i]);
                    //problem with state
                    printf("STATE \t\t %s \n", info.state[i]);
                    printf("DISCOUNT CODE \t %c \n", info.dis_code[i]);
                    printf("BALANCE \t %.2f \n", info.balance[i]);
                    //problem with due_date
                    printf("DUE DATE \t %s \n\n", info.due_date[i]);
                }

            }
            printf("\n\nfile was opened and loaded\n\n");
        }



    }
    printf("\n\n PROGRAM WILL END NOW");
    system("pause");

}

我的文字文件是:

125
LIPSO FACTO
SC
A
118.03
07/12/1998
193
GRADE BIT
OR
A
522.83
03/31/2003
237
MORE MATZOES
TN

846.29
01/11/2011
305
AIR BANGLEDESH
VT
A
3064
01/06/2005
485
FRED'S TATOOS
VT
C
2000.04
09/01/2007
520
WORLD WIDE WICKETS
WI

6280.43
04/29/1999
693
TAMMALIZATION
SC
B
3728
10/06/2009
746
REPLACEMENT PARTS
GA
C
5601.31
06/08/2003

2 个答案:

答案 0 :(得分:1)

可能你缺少使用strcpy()。你必须将buf复制到info.name [i]和info.state [i]。

答案 1 :(得分:1)

以下3个语句没有意义,因为您没有复制字符串,而是尝试终止字符串。请复制字符串然后终止它

info.name[i][strlen(info.name[i]) - 1] = '\0';

info.state[i][strlen(info.state[i]) - 1] = '\0';

info.due_date[i][strlen(info.due_date[i]) - 1] = '\0';