-1

I want to open a file in decimal mode.

Exemple:

ifstream file (ds->GetLocation(), ios::in|ios::binary); // how to open in decimal mode?

char ch;
if(file.is_open())
{
    while (!file.eof())
    {
        file.get(ch);
        double num = ch;
        cout << num << endl;
    }
    file.close();
}

When I open in binary mode the output is:

0
0
0
2
0
0
0
2
0
0
0
2
0
0
0
4
0
0
0
4
0
0
0
4
0
0
0
4
0
0
0
6
0
0
0
6
0
0
0
6
0
0
0
6
0
0
0
0

But I don't want the zero. I just want:

2
2
2
2
4
4
4
4
6
6
6
6

I can obtain these values in terminal with:

$ od -D filename

So, I want this result by reading the file in C++.

Azeem
  • 7,094
  • 4
  • 19
  • 32
  • 1
    Please post the contents of the input file. – R Sahu Mar 30 '20 at 04:40
  • output with command od filename: 0000000 000002 000000 000002 000000 000002 000000 000002 000000 0000020 000004 000000 000004 000000 000004 000000 000004 000000 0000040 000006 000000 000006 000000 000006 000000 000006 000000 0000060 – searcherddx Mar 30 '20 at 04:45
  • Relevant: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – Azeem Mar 30 '20 at 05:20
  • @searcherddx: While reading characters from the file, you can filter out using an `if` statement and print only non-zero values. – Azeem Mar 30 '20 at 05:21
  • You might find this useful: https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c – jignatius Mar 30 '20 at 06:11
  • 2
    There is no such thing as "decimal mode" for opening a file. There are only [binary and text modes](https://en.cppreference.com/w/cpp/io/c#Binary_and_text_modes). (You might also note that `2`, `4`, and `6` are not binary digits; your current code is already converting to a decimal representation.) Don't ask about the open mode; ask about how to read and interpret the data from the file. – JaMiT Mar 30 '20 at 06:35
  • I cannot filter the values different of zero, because i am reading the file to put the dataset on a data structure, so it's possible to have zero values on the file. What i'm trying to show is that i want to read the file like the command 'od -D filename' on terminal. In this way the output should be 222244446666 – searcherddx Mar 30 '20 at 14:23

1 Answers1

0

Here's a working example with writing and reading of file in binary mode (data: 0020):

#include <iostream>
#include <fstream>

int main()
{
    const auto filename = "test.bin";

    // write file in binary mode; data: `0020`

    std::ofstream out_file { filename, std::ios::binary };
    if ( !out_file.is_open() ) return -1;

    out_file.put( 0 );
    out_file.put( 0 );
    out_file.put( 2 ); // non-zero value
    out_file.put( 0 );
    out_file.close();

    // read file in binary mode; print only non-zero value

    std::ifstream in_file { filename, std::ios::binary };
    if ( !in_file.is_open() ) return -1;

    char c {'\0'};
    while ( in_file >> c )
    {
        const int n = c;
        if ( n ) std::cout << n << '\n';
    }

    in_file.close();

    return 0;
}

Output:

2

Relevant thread to read:

Azeem
  • 7,094
  • 4
  • 19
  • 32
  • I cannot filter the values different of zero, because i am reading the file to put the dataset on a data structure, so it's possible to have zero values on the file. What i'm trying to show is that i want to read the file like the command 'od -D filename' on terminal. In this way the output should be 222244446666 – searcherddx Mar 30 '20 at 14:23
  • @searcherddx: How long can this value be? Seems like a perfect candidate for string concatenation character-by-character and then `std::stoi` or other variant depending on the upper limit of the number. – Azeem Mar 30 '20 at 14:35
  • @searcherddx: If it's different than that then please update your question with proper formatting of the input file. A solution could be to invoke `od` on your binary file from your C++ program, pipe it into a different file and parse that from code. Also, you could look for the `od` source code and use that as a reference to write your own version. – Azeem Mar 30 '20 at 14:48
  • My question is: How to read a file in C++ in the same format of od -D filename. The link to edit question is not available for me. – searcherddx Mar 30 '20 at 16:39
  • 1
    I will make a new question later with a better explanation. – searcherddx Mar 30 '20 at 16:48