I got this silly problem and honestly dunno why it isnt working .
The scenario is i got a text file with information and i got to print the ASCII equivalent of the text into the screen .
While attempting to do so , i am getting the binary equivalent (well apparently it doesnt seem so ).

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

int main () {
  int c;
  char str[10];
  ifstream is;

  cout << "Enter the name of an existing text file: ";
  cin.get (str,10);

  is.open (str);        // open file

  while (is.good())     // loop while extraction from file is possible
  {
    c = is.get();       // get character from file
    cout << c ;
  }

  is.close();           // close file

  return 0;
}

I have a test.txt file where in i have store only "d" character (without quotes)

the output got is :
1001010-1

but i want it to print it as 100

although , if i run this program i.e to find the ascii eq i get the right output .

#include<iostream.h>
using namespace std;
int main ()
{
  char a = 'd';
  int x = a;
  cout<<x;
  return 0;
}

the output got is :
1001010-1

Perhaps your file contains d \n \n and you also print the received EOF before breaking the loop. ;)

U used cin.get(). The function was told to "get" 10 characters. Since your file contained only 3 characters, the OS added 7 more of its own!

U used cin.get(). The function was told to "get" 10 characters. Since your file contained only 3 characters, the OS added 7 more of its own!

No, the 10 char string is for the filename. OP is reading in single char at a time, storing as an int. I believe Dave Sinkula hit the nail on the head.

change the loop to

while (is.good())     // loop while extraction from file is possible
  {
    c = is.get();       // get character from file
    cout << c ;
    cout << "    ";   //put some space between results
}

to see that in fact multiple characters are being processed.

Well i think you arent getting the point. I am aware about the about and how it is coming but the gist is
"I want the ascii of that character to be printed i.e 100 for d . So ,if the file contains a list of characters , i need the program to display the ascii of these letters not the binary equivalents , besides the operation should be reversible i.e to get back the characters from their ascii notations. "

Well i think you arent getting the point. I am aware about the output and how it is coming but the gist is
"I want the ascii of that character to be printed i.e 100 for d . So ,if the file contains a list of characters , i need the program to display the ascii of these letters not the binary equivalents , besides the operation should be reversible i.e to get back the characters from their ascii notations. "

Edited 6 Years Ago by rahul8590: grammer

Try to type this

c = is.get();       // get character from file
cout << (int)c;

Or

c = is.get();       // get character from file
int int_c = (int)c;
cout << int_c;

ostream operator << overloaded for all primitive types. So you have to define the type explicitly.
For Code2Char operation you need to type something like this

c = is.get();       // get character from file
int int_c = (int)c;
cout << int_c;   //ascii-code
char a = (char)int_c;
cout << a;        //character

Edited 6 Years Ago by Protuberance: n/a

Well i think you arent getting the point. I am aware about the output and how it is coming but the gist is
"I want the ascii of that character to be printed i.e 100 for d . So ,if the file contains a list of characters , i need the program to display the ascii of these letters not the binary equivalents , besides the operation should be reversible i.e to get back the characters from their ascii notations. "

Well i think you arent getting the point.
Your program does print what you want. In decimal. It is just a bad luck that the codes for all characters in your file have only ones and zeroes, which gives an impression of binary. Try some other letters.
Since you do not print any delimiter, the codes are printed back to back, and there is no way to reverse it.

This article has been dead for over six months. Start a new discussion instead.