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.

using namespace std;


#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


inFile >> meal.idNo;

inFile.getline (, 22, ",");

inFile >> meal.unitCost;

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

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

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

} //endwhile


return 0;
} //end of main program


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

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'
There is no context in which this conversion is possible

Any help with this would be most appreciated.


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

    should be

    inFile.getline (, 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.

    while (inFile >> meal.idNo)
    inFile.getline(, 25, ',');

    inFile >> meal.unitCost;

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

    cout << setw(3) << meal.idNo << setw(25) << << setw(10) << meal.unitCost << "
    cout << fixed << setprecision(2); } //end while
    } //end if
  • 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!