Writing in C ++ files

0

The following code compiles and runs without errors, but when writing the data in the text file they appear in ASCII symbols.

Why is this happening?

#include <iostream>
#include <stdio.h>
#include <iomanip>
#define strlen 20
using namespace std;

typedef struct
{
    int number;
    int cost;
    int quantity;
}  PRICE, *LPPRICE;

int main()
{
    int size;
    cout << "Enter number of structures: ";
    cin >> size;
    cout << "-----------------------------------\n" << endl;

    LPPRICE pm = new PRICE[size];
    int index;
    FILE *file;

    if ((file = fopen("D:\8.txt", "wb")) == NULL) { printf("Error opening file.\n"); exit(1); }

    for (index = 0; index<size; index++)
    {
        cin.ignore();
        cout << "Number of orden: "; cin >> pm[index].number;
        cout << "Product cost: "; cin >> pm[index].cost;
        cout << "Quantity in stock: "; cin >> pm[index].quantity;
        cout << "-----------------------------------\n" << endl;
        fwrite(&pm[index], sizeof(PRICE), 1, file);
    }
    fclose(file);
    delete[]pm;
    system("PAUSE");
}
    
asked by Neon 14.11.2016 в 17:46
source

2 answers

2
fwrite(&pm[index], sizeof(PRICE), 1, file);

In that line you are dumping the content in binary into the file.

When you do:

int variable = 5;

In memory the number is stored in binary, that is: 0...0101 , which in 32 bits and hexadecimal would be 00 00 00 05 . If you then dump the value as is in a file what you are going to store is a sequence of four characters: 3 null and the non-printable character 0x05.

If you want to write to the file in text format you can choose to use fprintf to format the output or you can choose to use ofstream , own c ++ object to write files.

In the case of fprintf , more compatible with what you have, the code would look like this:

for (index = 0; index<size; index++)
{
    cin.ignore();
    cout << "Number of orden: "; cin >> pm[index].number;
    cout << "Product cost: "; cin >> pm[index].cost;
    cout << "Quantity in stock: "; cin >> pm[index].quantity;
    cout << "-----------------------------------\n" << endl;
    fprintf(file, "%d %d %d\n", pm[index].number, pm[index].cost, pm[index].quantity);
}

In the case of using ofstream, you could do something similar to:

ofstream file;
file.open ("D:\8.txt");

for (index = 0; index<size; index++)
{
    cin.ignore();
    cout << "Number of orden: "; cin >> pm[index].number;
    cout << "Product cost: "; cin >> pm[index].cost;
    cout << "Quantity in stock: "; cin >> pm[index].quantity;
    cout << "-----------------------------------\n" << endl;

    file << pm[index].number << ' ' << pm[index].cost << ' ' << pm[index].quantity << '\n';
}

file.close();

By the way, in C ++ the structures should be declared like this:

struct PRICE
{
    int number;
    int cost;
    int quantity;
};

And the pointer alias:

typedef PRICE* LPPRICE;

This last from the C ++ 11 standard (standard released in 2011) could also look like this:

using LPPRICE = PRICE*;

I recommend not using the proper syntax of C in C ++ projects.

    
answered by 14.11.2016 / 17:58
source
0

It happens that fopen when doing w means that you are going to write text string (plain text), however if you want to save the binary bytes (the binary representation of numbers, etc) you will have to place the letter b when opening .

if ((file = fopen("D:\8.txt", "wb")) == NULL) { printf("Error opening file.\n"); exit(1); }
    
answered by 14.11.2016 в 17:51