-1
void reportQnty(vector <Item> & items)
{
    bool MoreData = true;
    string line;
    fstream Report;
    Report.open("Report.txt");
    if (Report.is_open())
    {
        for (int i = 0; i< 11; i++)
        {
        Report << items[i].getName() << " ";
        Report << items[i].getonHand() << endl;
        }
    }
    while (MoreData == true)
    {
        if (Report.eof())
        {
            MoreData = false;
        }
        else
        {
            getline(Report, line);
            cout << line << endl;
        }
    }
    Report.close();
}

Though the report file is able to take in data when I send it, it refuses to print out each line when I try with the cout << line << endl. It has no errors--simply just does not print.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • 2
    Have you tried stepping through the code with a debugger to see what is going on? If you write to the end of a file, where do you think the file cursor ends up? – Quimby Nov 10 '22 at 07:10
  • A first step would be to verify that `getline` is successful. Also, read about the problems of `eof` [here](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). (Your loop is equivalent to `while (!Report.eof()) { getline(Report, line); ...` – molbdnilo Nov 10 '22 at 09:09

1 Answers1

0

I've solved your problem

There are two ways to write to a file

method 1: This is an ASCII file, its advantage is the ability to view it, you can open it in notepad and look at it, but it has drawbacks - it takes longer to write, longer to read and weighs more.

method 2: This is a BINARY file, its advantages are that it weighs little, is written quickly, and is read quickly , but there are also disadvantages - it cannot be read and changed in text editors

It all depends on the type of file: if you need a person to view it, then you should use an ASCII file if you need the file to weigh little and work quickly, then you should use a BINARY file

this program creates a file in its own directory (at least that's how it works in VisualStudio) it can be easily found by searching in explorer they are called:

Report.txt

ReportBIN.txt

And here is the code:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

struct Item 
{
    string s;
    int a;
    Item()
    {
    }
    Item(string inS, int inA) 
    {
        s = inS;
        a = inA;
    }
    string getName() 
    {
        return s;
    }
    int getonHand()
    {
        return a;
    }
};

//IT IS BINARY FILE WORKER

void reportWriteBINARY(vector <Item> & items) 
{
    ofstream file;
    file.open("ReportBIN.txt", ios::binary);

    unsigned int sizeArr = items.size();
    file.write((char*) &sizeArr, sizeof(unsigned int));

    file.write((char*)items.data(), (sizeof(Item) * sizeArr));


    file.close();
}

void reportReadBINARY(vector <Item> & items)
{
    items.clear();
    ifstream file("ReportBIN.txt", ios_base::binary);

    unsigned int sizeArr;
    file.read((char*)&sizeArr, sizeof(unsigned int));

    Item *temp = new Item[sizeArr];
    file.read((char*)temp, (sizeof(Item) * sizeArr));

    for (int i = 0; i < sizeArr; i++) 
    {
        items.push_back(temp[i]);
    }
}

//IT IS ASCII FILE WORKER
int split(string s, char c, vector<string>& r)
{
    string temp;
    int num = 0;
    for (int i = 0; i <= s.length(); i++)
    {
        if (s[i] != c and i != s.length())
        {
            temp += s[i];
        }
        else
        {
            r.push_back(temp);
            temp.clear();
            num++;
        }
    }
    return num;
}
void reportWriteASCII(vector <Item> & items)
{
    bool MoreData = true;
    string line;
    ofstream Report;
    Report.open("Report.txt");
    if (!Report.is_open()) { cout << "\nopen error!"; return; }
    
    for (auto & item : items)
    {
        Report << item.getName() << ' ';
        Report << item.getonHand() << '\n';
    }

    Report.close();
}

void reportReadASCII(vector <Item> & items)
{
    ifstream file;
    file.open("Report.txt");
    if (!file)  {cout << "\nOpen error!";  return;}

    while (!file.eof())
    {
        vector<string> split_strings;
        string s;
        getline(file, s);
        
        split(s, ' ', split_strings);

        if (split_strings.size() == 2) 
        {
            items.push_back(Item(split_strings[0], std::stoi(split_strings[1])));
        }
    }
    file.close();
}

void main() 
{
    vector<Item> WriteASCII =  { Item("test1", 10), Item("test2", 11), Item("test3", 12), Item("test4", 13)};
    vector<Item> ReadASCII;
    
    vector<Item> WriteBINARY = { Item("testBIN1", 10), Item("testBIN2", 11), Item("testBIN3", 12), Item("testBIN4", 13) };
    vector<Item> ReadBINARY;
    
    reportWriteASCII(WriteASCII);
    reportReadASCII(ReadASCII);

    reportWriteBINARY(WriteBINARY);
    reportReadBINARY(ReadBINARY);

    cout << "ASCII FILE:\n";
    for (auto & item : ReadASCII)
    {
        cout << '\n' << item.getName() << ' ' << item.getonHand();
    }
    cout << "\n\n\n" << "BINARY FILE:\n";
    for (auto & item : ReadBINARY)
    {
        cout <<'\n'<< item.getName() << ' ' << item.getonHand();
    }
}