0

I'm a beginner C++ user and I have tried collaborating with my classmates and such but we haven't been able to find an answer to this question. Our instructor has provided us with a linker that runs the main function for us and provides a simple text file for us to read from, and for the time being the second const char* in the heading is unimportant, for now all I need is to read the data from the file const char* saifFile and display it on-screen. When I run my program I have found that it stops the reading early. And I understand that you may not be able to help because you do not have access to the linker, but any help would be much appreciated.

Here is all my code:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int DESC_SIZE = 37;

struct Item
{
int itemId;
char description[DESC_SIZE];
double cost, price;
};

int processFile(const char* saifFile, const char* raofFile)
{
fstream outFile, inFile;
Item Inventory;

inFile.open(saifFile, ios::in);
while (inFile)
    {
        inFile >> Inventory.itemId >> Inventory.cost >> Inventory.price;
        inFile.getline(Inventory.description, DESC_SIZE);
        cout << "      " << Inventory.itemId << "     " << setw(5) << Inventory.cost << "       " << setw(5) << Inventory.price <<" " << Inventory.description << endl;
    }

return 0;
}
lethal-guitar
  • 4,101
  • 1
  • 16
  • 38
Prog
  • 23
  • 1
  • 5
  • 3
    Can you post the contents of the file you're trying to read? – Paweł Stawarz Apr 23 '14 at 20:44
  • Prefer using `Inventory.description = std::getline(infile)`, so you don't need to worry about the buffer size. – lethal-guitar Apr 23 '14 at 20:48
  • is `DESC_SIZE` an arbitrary size? – 109 Apr 23 '14 at 20:50
  • My instructor hasn't provided us with the file, the file is contained inside the linker and we don't have access to it. And DESC_SIZE is 37 because my instructor has stated that the description will be no larger than 36 characters. – Prog Apr 23 '14 at 20:54
  • What I have noticed about the loop is that it tends to stop after 15 repetitions even though the file has, say 100, entries in it, it will stop reading after 15, like there is something about that number and I cannot seem to figure out what is going on here. – Prog Apr 23 '14 at 20:57
  • If you're running the program then you must have a file... Why do you say you don't have the file? – Mooing Duck Apr 23 '14 at 20:58
  • I'm probably not stating this right. My instructor has provided us with a linker that provides the file for us, but we don't have access to the file, as in I can't open it up and give you the contents of this file. All I am charged with doing is writing the processFile function which will read from the provided file and display the contents on screen. – Prog Apr 23 '14 at 21:03
  • Unrelated, but I made the code pretty: http://coliru.stacked-crooked.com/a/0b93b11db5b8606f (Why the variable spacing? Perhaps those should be replaced with tabs or something) – Mooing Duck Apr 23 '14 at 21:06
  • I can't imagine why it would stop after 15, unless the input is not as you're expecting. wait, I know! Try this: `std::cout << std::ifstream(saifFile).rdbuf();` (This displays the whole file exactly as-is on the screen) – Mooing Duck Apr 23 '14 at 21:32

2 Answers2

0

Make sure that the data type you have set to receive from inFile matches the type that inFile reads. If not, you will get a stream error and that will cause your program to stop reading.

After every read, try to inFile.clear() and see if your program hangs or stops early. Alternatively, after each read try

if(inFile.fail())
{
    cout << "Read error in file\n";
}

This may not be the answer, but I'd start debugging here.

myselfesteem
  • 673
  • 1
  • 6
  • 20
-1

Try changing the while statement to:

while(!inFile.eof())

And also make sure that you have stored the data in the file in proper order.

user3566211
  • 65
  • 1
  • 9
  • [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Mooing Duck Apr 23 '14 at 21:00
  • @MooingDuck, this thread answers your query: http://stackoverflow.com/questions/5837639/eof-bad-practice – user3566211 Apr 23 '14 at 21:12
  • 2
    Sorry, that was a link to a SO question that explains why I downvoted. It wasn't intended to be an actual question. Sorry for the confusion. – Mooing Duck Apr 23 '14 at 21:15
  • @MooingDuck The amazing thing is there's an up-vote. The `eof()` zombie lives on. – juanchopanza Apr 23 '14 at 21:28