C++ bir dosyanın içine yazılan şeyleri alıp başka bir dosyaya yazmak

Hsn69

Decapat
Katılım
4 Ağustos 2021
Mesajlar
30
Daha fazla  
Cinsiyet
Erkek
Arkadaşlar bir dosyanın içine yazdığım şeyleri çekip başka bir dosyaya yazmaya çalışıyorum fakat olmuyor. Rica etsem yardımcı olur musunuz? Çalıştırdıktan sonra 1'i seçmeniz gerekiyor.

C++:
#include "iostream"
#include "fstream"
#include "string"
#include "iomanip"

using namespace std;

ofstream write;
ifstream read;
string line;
string str_calorie;
;



class product
{
public:
   
   
    float stock;
    float calorie;

    string prod_name;
    string prod_date;
    string exp_date;
    string str_stock;
    string str_calorie;

    int unit_type= 1;
   
public:



   
   
    void set_prod()
    {
        cout << "Enter product name" << endl;
        getline(cin, prod_name);
        cout << "Enter production date of product like this dd/mm/yyyy" << endl;
        getline(cin, prod_date);
        cout << "Enter production date of product like this dd/mm/yyyy" << endl;
        getline(cin, exp_date);
        cout << "To enter stock of product select stock unit" << endl << "For kg press 1" << endl << "For piece press 2" << endl;
        cin >> unit_type;
        switch (unit_type)
        {
            case 1:
                cout << "Enter stock" << endl;
                cin >> stock;
                str_stock = to_string(stock);
                str_stock.erase(5);
                str_stock.append(" kg");
                break;
               
            case 2:
                cout << "Enter stock" << endl;
                cin >> stock;
                str_stock = to_string(stock);
                str_stock.erase(5);
                str_stock.append(" piece");
                break;
               
            default:
                break;
        };
       
        cout << "Enter calorie of product" << endl;
        cin >> calorie;
        };
   
    void write_prod()
    {
        write.open("repo.txt");
        write << prod_name << endl;
        write << prod_date << endl;
        write << exp_date << endl;
        write << str_stock << endl;
        write << calorie << endl;
        write.close();
    };
   
    void read_prod()
    {
        read.open("repo.txt");
        read >> prod_name;
        read >> prod_date;
        read >> exp_date;
        read >> str_stock;
        read >> calorie;
        read.close();
    };
   
    void get_prod()
    {
        cout << prod_name << endl;
        cout << prod_date << endl;
        cout << exp_date << endl;
        cout << stof(str_stock) << endl;
        cout << calorie << endl;
        };
   
    ~product()
    {};
};
 

int main()
{
    product prod[100];
    int i = 0;
    int file_method;
    string deleted_item;
   
    write.open("repo.txt");
    write << "abc" << endl;
    write << "efg" << endl;
    write << "aaa" << endl;
    write << "gggg" << endl;
    write << "mmm" << endl;
    write.close();
   
    cout << "To add a new item press " << "1" << endl
         << "To delete an item " << "2" << endl;

    cin >> file_method;
   
    switch (file_method)
       
    {
        case 1:
            read.open("repo.txt");
            write.open("repo2.txt");
            while (!read.eof())
            {
                getline(read, line);
                prod[i].prod_name = line;
                getline(read, line);
                prod[i].prod_date = line;
                getline(read, line);
                prod[i].exp_date = line;
                getline(read, line);
                prod[i].str_stock = line;
                getline(read, line);
                prod[i].str_calorie = line;
                i++;
            };
           
            while (!write.eof())
            {
                write << prod[i].prod_name << endl
                << prod[i].prod_date << endl
                << prod[i].exp_date << endl
                << prod[i].str_stock << endl
                << prod[i].str_calorie << endl;
                i++;
            };
           
            read.close();
            write.close();
           

            break;
           
        case 2:
            cout << "Write an item wanted delete" << endl;
//            getline(cin, deleted_item);
            cin >>deleted_item;
            for(int i = 0 ; i < 2 ; i++)
            {
                if(prod[i].prod_name != deleted_item)
                {
                    read.open("repo.txt");
                    write.open("repo2.txt");
                    while (!read.eof())
                    {
                        getline(read, line);
                        prod[i].prod_name = line;
                        getline(read, line);
                        prod[i].prod_date = line;
                        getline(read, line);
                        prod[i].exp_date = line;
                        getline(read, line);
                        prod[i].str_stock = line;
                        getline(read, line);
                        prod[i].str_calorie = line;
                        i++;
                    };
                   
                   

                    while (!write.eof())
                    {
                        write << prod[i].prod_name << endl
                        << prod[i].prod_date << endl
                        << prod[i].exp_date << endl
                        << prod[i].str_stock << endl
                        << prod[i].str_calorie << endl;
                        i++;
                    };
                }
                write.close();
                read.close();
            };
            break;
           
        default:
            break;
    }
   
   


   
   
   
   
   





   

   

    return 0;
}
 

Technopat Haberler

Geri
Yukarı