Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Reading data from a text file into a random access file Posted by bajan_elf on 30 Jun 2009 at 6:30 AM
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.

#include <iostream>
using namespace std;

#include <conio.h>

#include <fstream> //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 << "\t" << meal.unitCost << "\n";

        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



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<char>
]
There is no context in which this conversion is possible
[/quote]

Any help with this would be most appreciated.
Report
Re: Reading data from a text file into a random access file Posted by Lundin on 30 Jun 2009 at 7:29 AM
inFile.getline (meal.name, 22, ",");

should be

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

and so on. You can tell this by reading the warnings carefully.
Report
Re: Reading data from a text file into a random access file Posted by bajan_elf on 30 Jun 2009 at 8:34 AM
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.

	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 << "\n";
			cout << fixed << setprecision(2); 		} //end while
	} //end if
Report
Re: Reading data from a text file into a random access file Posted by bajan_elf on 30 Jun 2009 at 10:23 AM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.