user2506637 user2506637 - 3 months ago 19
C Question

Write/Read Int as Char in C++?

I have 256 frequency values that I write to a file as ints..

Example:
0
0
0
10
.
.
0

I need to store them in a more efficient way. So each int needs to be less than 4 bytes. I was thinking I can store them as a char, but not sure if that is possible.

In the end I need to write the frequencies to a file and then be able to read them back.

Answer

enter image description here

Yes it is possible to store them as char.

The 256 frequency values is only 1 kilobyte of size on the disk.

However storing them as binary would indeed require less disk space.

#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>

using namespace std;

int main() {

    // demo only to create a random frequency value
    srand (time(NULL));

    //Create a dynamic array to hold the values
    vector<unsigned char> frequencyvalues;

    int sample;  
    int linebreak=0;

  //**** w r i t e    t o    f i l e  *******************************************************

    //Create an output file stream for writing to a file
    ofstream writetofile("abcd.txt",ios::out);
    cout << "Write Number to file abcd.txt:\n\n";

    // loop through to 256
    for (int j=0; j<256; j++) {

        // create a random frequency value between 0 and 256 and store it 
        sample=rand() % 256;
        sample = j;

        // write char number to file seperated by blank space
        writetofile<< sample<<" ";
    }

    writetofile.close();


    //**** r e a d   f r o m   f i l e **************************************************

    //Create an input file stream  for  readinf from a file
    ifstream readfromfile("abcd.txt",ios::in);

    //Read number using the extraction (>>) operator
    int index=0;
    while (readfromfile >> sample) {

        //Add the number to the end of the char vector array
        frequencyvalues.push_back(sample);  
    }

    //Close the file stream
    readfromfile.close();


   //**** D i s p l a y   t h e    a r r a y   *****************************************


    //Display the numbers
    cout << "Read Number from file abcd.txt:\n\n";
    for (int i=0; i< frequencyvalues.size();  i++) {

       // explicit type convert char to int while displaying with cout<<
       // without the (int)frequencyvalues[i] conversion all that would be seen is binary characters
        cout << (int)frequencyvalues[i] << ' ';

        linebreak++;if(linebreak>18) {

            linebreak=0;
            cout <<"\n";
        }
    }
    cout <<"\n\n";
    cin.get();  
    return 0;
}

Code listing 2. Writing and reading frequency values as binary.

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main (){

  vector<int> frequencyvalues;
  int sample;  

  // store frequency values in an array ready for writing to disk   
  for (int j=0; j<256; j++) {
        sample = j;
        frequencyvalues.push_back(sample);
    }


  //**** w r i t e    t o    f i l e  *******************************************************

    //create a binary file for writing array  to disk
    ofstream outfile ("abc.bin", ios::binary);
    outfile.write(reinterpret_cast<char*>(&frequencyvalues), sizeof(frequencyvalues));


    //**** r e a d   f r o m   f i l e **************************************************

    //create a binary file for reading from file on  disk to array
    ifstream infile ("abc.bin", ios::binary);
    infile.read(reinterpret_cast<char*>(&frequencyvalues), sizeof(frequencyvalues));

     cout<<"Loaded array from disk\n\n";


    //**** D i s p l a y   t h e    a r r a y   *****************************************

    //loop for displaying array loaded from file on disk
    int linebreaker=0;
    for (int i=0; i< frequencyvalues.size();  i++) {
        cout <<frequencyvalues[i] << ' ';
        linebreaker++; if (linebreaker > 18 ){
            linebreaker = 0;
            cout<<"\n";
        }
    }

  cout<<"\n\n";
  cout<<" \nPress any key to continue\n";
  cin.ignore();
  cin.get();


 return 0;
}