Reading data from a text file into a random access file

Hey. I'm trying to create part of a program that will read data from a text file, store the data into variables and then write the data into a random access file. So far this is what I have.

[code]
#include
using namespace std;

#include

#include //file stream
using std::fstream;


struct mealItem
{
int idNo; //meal ID number
char name[22]; //name of meal
double unitCost; //unit cost of meal
char descrip[55]; //description of meal
};

typedef struct mealItem item;

int main()
{
item meal; //meal of structure type item

fstream inFile("Meals.dat", ios::in); //text file
fstream outFile("Inventory.dat", ios::out, ios::binary); //RAF file

while(!inFile.eof())
{

inFile >> meal.idNo;

inFile.getline (meal.name, 22, ",");
inFile.ignore();

inFile >> meal.unitCost;

inFile.getline (meal.descrip, 55, ".");
inFile.ignore();

cout << meal.idNo << " " << meal.unitCost << "
";

outFile.seekp((meal.idNo - 100) * sizeof(item));
outFile.write(reinterpret_cast<char*> (&meal), sizeof(item));


} //endwhile

inFile.close();
outFile.close();

getch();
return 0;
} //end of main program

[/code]

But when I go to compile the program, I'm getting this error..

[quote]
error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,_Elem)' : cannot convert parameter 3 from 'const char [2]' to 'char'
with
[
_Elem=char,
_Traits=std::char_traits
]
There is no context in which this conversion is possible
[/quote]

Any help with this would be most appreciated.

Comments

  • inFile.getline (meal.name, 22, ",");

    should be

    inFile.getline (meal.name, 22, ',');

    and so on. You can tell this by reading the warnings carefully.
  • Thanks Lundin, I understand now that the characters should've been in single quotes rather than double quotes as the compiler was trying to read a string... or something to that effect.

    Now that I've got that part sorted, when I print the data from the file to the screen I'm only seeing half the text i.e if I have 20 lines of text I'm only seeing 10 lines being printed to the screen. I'm wondering if it's my loop. I tried just using the while loop - while (!inFile.eof()) - that my teacher suggested, but that doesn't terminate. So this is what I came up with, but as I said it's only printing half the data.

    [code]
    if(!inFile.eof())
    {
    while (inFile >> meal.idNo)
    {
    inFile.getline(meal.name, 25, ',');
    inFile.ignore();

    inFile >> meal.unitCost;

    inFile.getline(meal.descrip, 55, '.');
    inFile.ignore();

    cout << setw(3) << meal.idNo << setw(25) << meal.name << setw(10) << meal.unitCost << "
    ";
    cout << fixed << setprecision(2); } //end while
    } //end if
    [/code]
  • Strange enough I just increased the size of the descrip and name arrays and the whole file is now printing to the screen. So that takes care of that.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories