Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ cerr而不是cout,它没有缓冲,因此保证在出现严重错误之前执行。 #include <iostream> #include <fstream> #include <string> #include <conio_C++_Binary - Fatal编程技术网

C++ cerr而不是cout,它没有缓冲,因此保证在出现严重错误之前执行。 #include <iostream> #include <fstream> #include <string> #include <conio

C++ cerr而不是cout,它没有缓冲,因此保证在出现严重错误之前执行。 #include <iostream> #include <fstream> #include <string> #include <conio,c++,binary,C++,Binary,cerr而不是cout,它没有缓冲,因此保证在出现严重错误之前执行。 #include <iostream> #include <fstream> #include <string> #include <conio.h> #include <stdlib.h> #include <string.h> using namespace std; char answer; struct PRODUCT { strin

cerr而不是cout,它没有缓冲,因此保证在出现严重错误之前执行。
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

char answer;

struct PRODUCT {
    string product_name;
    int quantity;
    PRODUCT() : product_name(""), quantity(0) {}
} product;

int main () {
    fstream delivery_file("files/Delivery", ios::app | ios::in | ios::out | ios::binary);
    do {

        if (delivery_file.is_open()) {
            cout << "\nPlease input the name of a product: ";

            getline(cin, product.product_name);

            cout << "\nPlease input the quantity of a product: ";
            string str;
            getline(cin, str);
            product.quantity = atoi(str.c_str());

            bool foundAndReplaced = false;

            while (!delivery_file.eof())
            {
                PRODUCT temp_prod;
                while (!delivery_file.eof())
                {
                    char ch = delivery_file.get();
                    temp_prod.product_name += ch;
                    if (ch == 0)
                    {
                        break;
                    }
                }
                if (delivery_file.eof() && delivery_file.tellg() == ios::beg)
                {
                    cout << "Error: Unexpected end of file.\n";
                    delivery_file.clear();
                    break;
                }
                if (temp_prod.product_name == product.product_name)
                {
                    delivery_file.seekp(delivery_file.tellg());
                    delivery_file.read((char*)(temp_prod.quantity), sizeof(PRODUCT::quantity));
                    product.quantity += temp_prod.quantity;
                    delivery_file.write((char*)(product.quantity), sizeof(PRODUCT::quantity));
                    foundAndReplaced = true;
                }
            }

            if (!foundAndReplaced)
            {
                delivery_file.write(product.product_name.c_str(), product.product_name.length() + 1);

                delivery_file.write((char*)(&product.quantity), sizeof(product.quantity));
            }

        }

        else {
            cout << "Unable to open file";
        }


        cout << "Do you want to add more products? Y/N \n";
        answer = 0;
        while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N')
        {
            answer = _getch();
        }
    }
    while(answer == 'Y' || answer == 'y');
    delivery_file.close();

    cout << "\nDeliveries registered.\n";

    return 0;
}
int foo = 0x0F00;
ofstream bar("test.txt", ios::out | ios::binary);
bar.write(reinterpret_cast<char*>(&foo), sizeof(int));
bar.close();