将数据从文本文件写入输出文件

时间:2018-03-20 02:29:25

标签: c++

我正在制作一个程序,将客户信息作为输出文件读取到发票。我的文本文件中的数据是:

  

乔治华盛顿

     

703-780-2000

     

100 50 3

     

黑色

  

Dan Goldberg

     

800-600-6014

     

2 2 1

     

红色

如何直接将这些特定的字符串行读取到输出文件?  谢谢!

代码我已经开始:

#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;
}

1 个答案:

答案 0 :(得分:0)

<强>写

#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();
}

<强>读取

#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();
}

<强>输出

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

更新

书写结构

#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;
}

阅读结构

#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;
}