screw you
screw you

Reputation: 141

Having problems with my file read, using a struct

I got my struct the right way, and it seems like a lot of this is working. However I got a small problem with my strings reading this garbage in my file read. I am trying to just pull a name, date, and state. And it keeps producing this odd form of "]][]]]][[[[[[" before the actual date, name, or state. How do I get my struct to read the file without getting this garbage feedback.

#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");

}

My text file is this:

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

Upvotes: 0

Views: 89

Answers (2)

AravindMS
AravindMS

Reputation: 49

The below 3 statements doesn't make sense as you have not copied the string, but trying to terminate the string. Please copy the string then terminate it

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';

Upvotes: 1

nikhil mehta
nikhil mehta

Reputation: 1032

probably you are missing to use strcpy().you have to copy buf to info.name[i] and to info.state[i].

Upvotes: 1

Related Questions