cannot read in from file - C++

Discussion in 'Computer Science & Culture' started by wu_weidong, Jul 21, 2002.

Thread Status:
Not open for further replies.
  1. wu_weidong Registered Member

    Messages:
    2
    hi,
    I am trying to write a program that will read in the different fields in a
    file "NRIC.txt". They are separated by a space except the "name" and "race" field, which is separated by a "\t". After I entered the number of students, nothing happens, the program just hangs. I am using Borland C++ 5.0. Please tell me what is wrong with my code. My other question is that in the date field, I need to read in the month individually (excluding the date and year). How do I do that? Also, how do I arrange the names alphabetically? Will strcmp do it? Thank you very much.

    #include <fstream.h>
    #include <conio.h>
    #include <stdlib.h>
    void main()
    {
    int number, i, tel[50], race[50], country[50];
    char ID[50][9], name[50][21], dob[50][9], sex[50];
    ifstream inFile;
    cout << "Enter number of students: ";
    cin >> number;
    inFile.open("NRIC.txt");
    if (inFile.fail())
    {
    cerr << "Error! The file cannot be opened!";
    exit(1);
    }
    while (!inFile.eof())
    {
    for (i = 0 ; i < number ; i++)
    {
    inFile.get(ID, 9, '\n');
    inFile.get(name, 21, '\n');
    inFile >> race;
    inFile.get(dob, 9, '\n');
    inFile >> sex;
    inFile >> country;
    inFile >> tel;
    }
    }
    cout << ID[4];
    inFile.close();
    getch();
    }

    The contents of NRIC.txt are in this format arranged in the order of ID, name, race, date of birth, sex, country, and telephone number:

    0039295B RAJU S/O MURU 2 31.12.50 M 1 4689355
    0060524G GOPAL SINGH 2 29.02.60 M 2 7771234
    0239219D GERALD D' SOUZA 4 15.10.60 M 3 2854678
    1345800F ALIJAH BTE ABAS 3 08.08.68 F 2 2345876
    1605165I GOH WEI MIN 1 11.01.70 M 1 2345123
    1789182J ZEE MAY LING 1 13.05.71 F 2 4557865
    1824913H GAN CHEE WEE 1 25.12.73 M 1 5453310
    1833283C LEELA D/O MENON 2 09.08.65 F 1 2345689

    Regards,
    Wei Dong
     
Thread Status:
Not open for further replies.

Share This Page