tags:

views:

37

answers:

1

Hello all. I am a beginner with C++. I have a new project at work where I have to learn it, so I'm trying some things just to test my understanding. For this problem, I'm trying to read a file and then print it on screen. Super simple, just trying to get good at it and understand the functions that I'm using. I copied some text from a MS Word document into a notepad (*.txt) file, and I'm trying to read this *.txt file. All of the text in the word document is bolded, but other than that there are no 'unusual' characters. Everything prints out on the screen as it appears in the document except the bolded " - " symbol. This character is printed as the "u" with a hat character ("so called extended ASCII" code 150). I try to print out the integer value of this character in my array (which should be 150) but I get -106. I realize this signed integer has the same bits as the unsigned integer 150. My question is how to get the output to say 150? Here's my code:

        #include <iostream>
        #include <fstream>
        using namespace std;

       int main() {
       unsigned char* input1;
       int input1size = 57;
       ifstream file("hello_world2.txt",ios::binary | ios::ate);
       if (file.is_open()){
            int size;
            size = (int) file.tellg();
            cout <<"This file is " << size << " bytes." << endl;
            file.seekg(0,ios::beg);
            input1 = new unsigned char[input1size];
            file.read(input1, input1size);
            cout << "The first " << input1size <<" characters of this file are:" << endl<<endl;
            for (int i=0; i<input1size; i++) {
            cout << input1[i];
       }
       cout<<endl;
       }
       else {
       cout <<"Unable to open file" << endl;
       int paus;
       cin>>paus;
       return 0;
       }
       file.close();
       int charcheck = 25;
       int a=0;
       int a1=0;
       int a2=0;
       unsigned int a3=0;
       unsigned short int a4=0;
       short int a5=0;
       a = input1[charcheck];
       a1 = input1[charcheck-1];
       a2 = input1[charcheck+1];
       a3 = input1[charcheck];
       a4 = input1[charcheck];
       a5 = input1[charcheck];
       cout <<endl<<"ASCII code for char in input1[" << charcheck-1 <<"] is: " << a1 << endl;
       cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] is: " << a << endl;
       cout <<endl<<"ASCII code for char in input1[" << charcheck+1 <<"] is: " << a2 << endl;
       cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as unsigned int: " << a3 << endl;
       cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as unsigned short int: " << a4 << endl;
       cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as short int: " << a5 << endl;
       int paus;
       cin>>paus;
       return 0;
       }

Output for all this looks like:

    This file is 80 bytes.
    The first 57 characters of this file are:

    STATUS REPORT
    PERIOD 01 u 31 JUL 09

    TASK 310: APPLIC

    ASCII code for char in input1[24] is: 32
    ASCII code for char in input1[25] is: -106
    ASCII code for char in input1[26] is: 32
    ASCII code for char in input1[25] as unsigned int: 4294967190
    ASCII code for char in input1[25] as unsigned short int: 65430
    ASCII code for char in input1[25] as short int: -106

So it appears "int a" is always read as signed. When I try to make "a" unsigned, it turns all the bits left of the eight bits for the char to 1's. Why is this? Sorry for the length of the question, just trying to be detailed. Thanks!

A: 

What you're dealing with is the sign-extension that takes place when the char is promoted to int when you assign it to one of your a? variables.

All the higher order bits must be set to 1 to keep it the same negative value as was in the smaller storage of the char.

Amardeep
So is there a way to output the unsigned value 150? When I try to declare the pointer input1 as "unsigned char* input1;" (as is posted in the code in the original post), I get an error saying ...::read' cannot convert parameter 1 from 'unsigned char *' to 'char *'.
Jade
Yes. Use a mask: `a3 = input1[charcheck] ` to remove the sign extended one bits. Then your cout will display 150.
Amardeep
Ah ok, thank you! I don't know anything about masks at this point, but now I have a direction to search. I appreciate your help!
Jade