Thread: reading data from disk into array problem

  1. #1
    Unregistered
    Guest

    reading data from disk into array problem

    Hi,

    Basically i have made a small booking program which gets vairious details from the user stores then using a struct and an array, then the user is asked if they would like to save the details to disk, then the program will save the values in a .txt file. All this works fine, the problem is when i want to load the files back into the struct arrays, if the user entered "sometxt sometxt2" only he txt before the space will be loaded back into the program.

    here is snipets of the code that reads the values from the txt file into the array/struct.


    // read data call
    iCount = ReadData(filename, Bookings, iCount);



    int ReadData(char fname[], BookingType Arr[], int iNo)
    {
    ifstream iFile; // internal name of the INPUT file
    iFile.open(fname); // open the input file
    BookingType temp; // temporary struct
    if ( iFile.fail() ) // check for error opening file
    { cout << "Error opening file." << endl;
    cout << "Press any key to continue...";
    getch();
    return iNo; // return the same counter
    } // end if open error

    // read records
    iNo = 0; // start at the begining of the array
    while ( !(iFile.eof() ))
    {
    temp = ReadRecord(iFile);
    if ( !(iFile.eof() ))
    {
    Arr[iNo] = temp;
    iNo = iNo + 1;
    }
    } // all records read
    iFile.close(); // finished with file
    cout << iNo << " Records read from file " << fname << endl;
    return iNo; // return the new counter
    }



    BookingType ReadRecord(ifstream& file)
    {

    BookingType temp; // temporary struct



    file >> temp.iAdvanceDays;
    file >> temp.sDepAirport;
    file >> temp.sArrivalAirport;
    file >> temp.sName;
    file >> temp.sAddress;



    // return the temporary struct
    return temp ;
    } // end ReadRecord()

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    file >> temp.iAdvanceDays;
    file >> temp.sDepAirport;
    file >> temp.sArrivalAirport;
    file >> temp.sName;
    file >> temp.sAddress;
    These should be:
    file.getline(temp.iAdvanceDays, SOME_NUMBER);
    file.geline(temp.sDepAirport, SOME_NUMBER);
    file.getline(temp.sArrivalAirport, SOME_NUMBER);
    file.getline(temp.sName, SOME_NUMBER);
    file.getline(temp.sAddress, SOME_NUMBER);

  3. #3
    Unregistered
    Guest
    just tried that mate and i get an error:
    Error PROJECT.CPP 278: Could not find a match for 'istream::getline(int,int)' in function ReadRecord(ifstream &)
    Error PROJECT.CPP 279: Could not find a match for 'istream::getline(string,int)' in function ReadRecord(ifstream &)
    Error PROJECT.CPP 280: Could not find a match for 'istream::getline(string,int)' in function ReadRecord(ifstream &)
    Error PROJECT.CPP 281: Could not find a match for 'istream::getline(string,int)' in function ReadRecord(ifstream &)
    Error PROJECT.CPP 282: Could not find a match for 'istream::getline(string,int)' in function ReadRecord(ifstream &)



    line 278 file.getline (temp.iAdvanceDays,100);
    file.getline (temp.sDepAirport,100);
    file.getline (temp.sArrivalAirport,100);
    file.getline (temp.sName,100);
    line 282 file.getline (temp.sAddress,100);

    any idea what the problem is?

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    What compiler?

  5. #5
    Unregistered
    Guest
    Borland 4.5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  2. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. problem: reading user input into an array
    By alpha561 in forum C Programming
    Replies: 13
    Last Post: 05-24-2002, 07:23 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM