-2

I am working on a program right now that will read customer info over to an invoice as an output file. The data I in my text file is:


George Washington

703-780-2000

100 50 3

Black


Dan Goldberg

800-600-6014

2 2 1

Red


How can I read these specific lines of string directly to an output file? Thanks!

Code I have started with:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int BLACK=35, RED=30, BROWN=25    //Unit price of mulch
int main()
{
    string name;
    string phone_number;
    string dimensions;
    string color;
    ifstream infile;

    return 0;
}
EthanT24
  • 9
  • 2
  • 1
    Can you share what you already tried, please? – Alex Mar 20 '18 at 02:34
  • Sure. Bare with me, for I am new to reading and writing data from input and output files. This is how I have started my code. I am more so struggling with how to read over the lines of data to the source code. – EthanT24 Mar 20 '18 at 02:43
  • Please edit the question to include this as properly formatted code instead of putting it in the comments. – eesiraed Mar 20 '18 at 02:52
  • whatis the format of the output file ? – WARhead Mar 20 '18 at 02:56
  • Make a `struct` or `class` that contains all of the pieces of information. Overload `operator >>` to read into this `struct` or `class`. Read into the structure (`infile >> mystruct`) until you can read no more. Store what you read according to taste. I like a trusty ol' `std::vector`. You may also find `std::getline` helpful. [Help on writing `operator>>`](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading). – user4581301 Mar 20 '18 at 03:13
  • In short: you can't "read" directly to a file. You load the data into variables - a struct makes sense in this case - then write that data to the output file. You can't just pipe it over without doing some work. – 3Dave Mar 20 '18 at 04:16

1 Answers1

0

Write

#include <iostream>
#include <fstream>
int main()
{
    std::string name = "George Washington";
    std::string phone = "703-780-2000";
    std::string dimensions = "100 50 3";
    std::string color = "Black";
    std::ofstream file("data.txt");
    file << name + "\n";
    file << phone + "\n";
    file << dimensions + "\n";
    file << color;
    file.close();
}

Read

#include <iostream>
#include <fstream>
int main()
{
    std::string name;
    std::string phone;
    std::string dimensions;
    std::string color;
    std::ifstream file("data.txt");
    std::getline(file, name);
    std::getline(file, phone);
    std::getline(file, dimensions);
    std::getline(file, color);
    std::cout << "name: " << name << "\nphone: " << phone
    << "\ndimensions: " << dimensions << "\ncolor: " << color << "\n";
    file.close();
}

Output

name: George Washington
phone: 703-780-2000
dimensions: 100 50 3
color: Black

Update

Write Structures

#include <iostream>
#include <fstream>

typedef struct{
    std::string name;
    std::string phone;
    std::string dimensions;
    std::string color;
}Customer;
int main()
{
    Customer customer[2]; // number of customers
    puts("Max number of customers is 2");
    for(int id = 0; id < 2; ++id)
    {
        std::cout << id <<"| name: ";
        std::cin >> customer[id].name;
        std::cout << id <<"| phone: ";
        std::cin >> customer[id].phone;
        std::cout << id <<"| dimensions: ";
        std::cin >> customer[id].dimensions;
        std::cout << id <<"| color: ";
        std::cin >> customer[id].color;
    }
    std::ofstream data("invoices.bin", std::ios::binary);
    data.write((char*)customer, sizeof(customer));
    data.close();
    return 0;
}

Read Structures

#include <iostream>
#include <fstream>

typedef struct{
    std::string name;
    std::string phone;
    std::string dimensions;
    std::string color;
}Customer;
int main()
{
    Customer customer[2]; // number of customers
    std::ifstream data("invoices.bin", std::ios::binary);
    data.read((char*)&customer, sizeof(customer));
    puts("Reading...");
    for(int id = 0; id < 2; ++id)
    {
        std::cout << id << "| name: " << customer[id].name << "\n";
        std::cout << id << "| phone: " << customer[id].phone << "\n";
        std::cout << id << "| dimensions: " << customer[id].dimensions << "\n";
        std::cout << id << "| color: " << customer[id].color << "\n";
    }
    data.close();
    return 0;
}
Beyondo
  • 2,952
  • 1
  • 17
  • 42
  • Thanks for the help! Now my question is, if I want to loop and read the same structures of data for however many customer information that I enter, will my string declarations change? – EthanT24 Mar 20 '18 at 03:24
  • @EthanT24 I edited my answer, and no, you won't change how you declare strings, but how you're processing them. – Beyondo Mar 20 '18 at 04:02
  • Down-voting this since it's generally not our policy to do homework for others. Offer suggestions in the comments, but don't do it for them. – 3Dave Mar 20 '18 at 04:14
  • 1
    I just answered his question from comments with examples cause it was really simple, If my answer is someway *too complicated* for him, he could search for what a specific line of code means and have other good questions to ask whether in SO or somewhere else. – Beyondo Mar 20 '18 at 09:40
  • @KiraSama That’s not the problem. The issue is that you did the whole thing for him, and this level of problem is *clearly* homework. He didn’t have to learn anything or research at all - just copy & paste your answer. Remember, you might wind up working with those that you help. Make them work for it. – 3Dave Mar 21 '18 at 05:43