Extracting decimal values from file

I would like to read from a file and produce decimal values that are between 0 and 255 to produce an eight bit binary number. I have discovered a way to do this, but is there a better way? I am getting a char value and converting it to an int value + 128. Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream>
#include <fstream>
using namespace std;

int value;
int lowest = 127;
int highest = 127;
char letter;

int main(int argc, const char *argv[])
{
   if (argc < 2) {
      cerr << "Usage: " << argv[0] << "<filename>\n";
      return 1;
   }

   ifstream in(argv[1], ios::binary);
   while (in) {
       in.get(letter);
       value = int(letter) + 128;
       if(lowest > value)
          {
             lowest = value;
          }
       if(highest < value)
          {
             highest = value;
          }
   }
   cout << "lowest = " << lowest << "     highest = " << highest << endl;
   in.close();
   return 0;
}  
There is always another way.
Your code looks pretty simple. (a good thing)

The only thing I see right now is that if argc < 2 it looks like it will still try and run the rest of the code. (not good)

I haven't ran it but you set the value of lowest and highest to 127.
To me that seems wrong. One or the other will (in my thinking) always be true. (incorrectly)

// comments in your code would help me know what your trying to achieve in each step. (missing)

Last edited on
I acted on your suggestion and changed argc < 2 to argc != 2. It seems to work. I tried to place comments before the #include statements at the start of the file. My compiler generated multiple pages of errors. I have included lots of block quotes in this version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
using namespace std;

/* compiled using g++ (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4
   This program checks that there are two arguments in the command line
   The second argument is the name of the file that is to be read.
   The file comes in as a stream of char variables that are converted
   to int variables. These seem to be signed integers (-128 to 127), so
   128 is added to obtain a range of 0 to 255. Eventually the decimal
   value will be converted to an eight bit binary number and loaded
   into an 8x8 array. This array will be transposed (column to row)
   onto a second 8x8 array. The horizontal binary numbers in the
   second array will be converted to decimal, and then to a char
   variable and written to a file
*/
/* The variable int value holds the decimal value (hopefully it is
   between 0 and 255)
   The variable int lowest holds the value of the lower end of the
   range of the decimal value of the stream (it should be 0). Why did
   I set it to 127? I figured it was halfway between 0 and 255
   The variable int highest holds the value of the higher end of the
   range of the decimal value of the stream
   The variable char letter holds the value of the character being
   read from the file
*/


int value;
int lowest = 127;
int highest =127;
char letter;

/* When the main method is run the number of arguments is compared to
   2 if the result is false then an error message is displayed
   otherwise the program continues. 
*/

int main(int argc, const char *argv[])
{
   if (argc != 2) {
      cerr << "Usage: " << argv[0] << "<filename>\n";
      return 1;
   }

/* The input stream is declared and the while loop continues until
   the end of file is reached. The char variable letter assumes the
   value of the incoming character, and the char variable is converted
   to an int variable value (signed integer -128 to 127). 128 is added
   to the int variable value to hopefully get a range of 0 to 255.
*/
   ifstream in(argv[1], ios::binary);
   while (in) {
       in.get(letter);
       value = int(letter) + 128;

/* These two if statements are used to determine the range of the
   decimal values. The results are then displayed
*/
 
     if(lowest > value)
          {
             lowest = value;
          }
       if(highest < value)
          {
             highest = value;
          }
   }
   cout << "lowest = " << lowest << "     highest = " << highest << endl;
   in.close();
   return 0;
}  


Last edited on
Topic archived. No new replies allowed.