Thread: Loading a file

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Loading a file

    Ok at the moment im just trying to load some data of a txt file into an array of structures and display it. But it isnt working proply. The data that im useing is:

    8/2/2002
    1 2 2 3

    3 0 4 2

    5 3 6 1

    i have 7 of these.

    Not sure if i have set up my structure proply to deal with. Do i need 1 integer for lines below the date or will i need 1 for each?

    #include<iostream.h>
    #include<fstream.h>

    struct Tfixtures
    {
    char date[8];
    int teams;
    int scores;
    int teams2;
    int scores2;
    };

    void readfromfile(Tfixtures teamfixtures[], int size);
    void showtoscreen(Tfixtures teamfixtures[], int size);

    main()
    {
    const int MAX_NUM=7;
    Tfixtures teamfixtures[MAX_NUM];
    readfromfile(teamfixtures, MAX_NUM);
    showtoscreen(teamfixtures, MAX_NUM);

    return 0;
    }



    void readfromfile(Tfixtures teamfixtures[], int size)
    {
    int i;
    ifstream fin;
    fin.open("a:fixtures.txt");
    for(i=0; i<size; i++)
    {
    fin>>teamfixtures[i].date>>ws;
    fin>>teamfixtures[i].teams>>ws;
    fin>>teamfixtures[i].scores>>ws;
    fin>>teamfixtures[i].teams2>>ws;
    fin>>teamfixtures[i].scores2>>ws;
    }
    fin.close();

    }

    void showtoscreen(Tfixtures teamfixtures[], int size)
    {
    int i;
    for(i=0; i<size; i++)
    {
    cout<<teamfixtures[i].date<<endl;
    cout<<teamfixtures[i].teams<<endl;
    cout<<teamfixtures[i].scores<<endl;
    cout<<teamfixtures[i].teams2<<endl;
    cout<<teamfixtures[i].scores2<<endl;
    }
    }


    Also is there some way i can output the file in the same format as it is in the txt, i cant seem to find anything on formatting.

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    what's your proper name? You don't go to uclan by any chance?

  3. #3
    Unregistered
    Guest
    you don't declare what ws is, but I suspect you intended it to be a char to handle whitespace.

    In the screen display it appears as though the numbers under the date are single digits separated by spaces (which I is the case I believe) as opposed to a single 4 digit number. I think you are trying to say that on 8/8/2002 team 1 had score of 2, team 2 had score of 3, team 3 had score of 0, team 4 had score of 2, team 5 had score of 3 and team 6 had score of 1. If that is true, then. If that is the case then your struct is off a bit. You could remove the date from the struct, in which the struct would basically represent one line of file other than the date. Or you could add more members to the struct to include teams3, team4, team5, team6, and their scores. That is each struct would have one string member for the date and 12 int members, 6 for the team number and 6 for the team score. Last, but not least you could create a different struct called team with two members called number and score. Then you could have an array of 6 teams in the Tfixtures struct. Lotsa choices to choose from, but the current code won't cut the muster with this file.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    Check out the best File I/O tutorial on the net:
    http://www.cpp-home.com/FileIO_tutorial.php
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  5. #5
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    maybe uclan , maybe not, but I know some1 who does? right subdene?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM