Hi
I have the following function which read ASCII characters(separated by space) from a file and I want to print it out to the screen
contents of the data.txt file are like this
70 97 104 97 100

void decode()
{
	string str;
	string instr;
	
	ifstream inFile;
	cout<<endl;
	
	inFile.open("data.txt");
	inFile>>str;
	for (int i=0;i<=str.length()-1;i++)
	{
		if(str[i] == ' ')
		{
			
		}
		
	}
	
	inFile.close();
	selection=0;
}

could anyone here help me to solve this problem
thanks in advance

The char type is an integer type, but when you use the << operator to print it, it's assumed that you want to print the character representation. To get the numeric value you can cast to an integer:

#include <iostream>

int main()
{
  const char *s = "test";

  for ( int i = 0; s[i] != '\0'; i++ )
    std::cout<< (int)s[i] <<' ';

  std::cout<<'\n';
}

Just get the ASCII codes from the file (as a number), then put it in a char-variable and print it to the screen or append it at the end of the string variable where you want to put the converted string in :)

This example doesn't put the whole stuff in a string, but it gives you a good start:

int ascii;                     // for holding the ASCII value
inFile >> ascii;                // get the next ASCII code from the file
cout << (char)ascii << ' ';  // print out the character equivalent of the ASCII code

contents of the data.txt file are like this
70 97 104 97 100

I'm thinking of extracting each ascii number and convert it to char and print it to the screen
but how I do the extracting process in code

contents of the data.txt file are like this
70 97 104 97 100

I'm thinking of extracting each ascii number and convert it to char and print it to the screen
but how I do the extracting process in code

Start with my previous post (read the comments, they might be useful :P) , remember, we aren't writing code for you!

BTW, the following is useless IMO:

for (int i=0;i<=str.length()-1;i++)
{
	if(str[i] == ' ')
	{
		
	}
		
}

If you get the integers in the same way as I demonstrated in my previous post, then you even don't have to bother with spaces, you're just making it yourself difficult if you would do :)

>but how I do the extracting process in code
Take a look at my previous post, and put the code mentioned there in a loop (I hope this isn't too difficult for you).

thanks man i did it with this code

for(int i=0;i<=4;i++)
	{
		inFile >> ascii;
                cout << (char)ascii;
		
	}

But How I can get the number of characters an int variables has to put in the for condition

tux4life, don't torment the unhappy C++ programmer, tell him/her about while(f>>i) construct ;)

Comments
Srry, forgot to tell it :D

finally I come up with this

void decode()
{
		
	ifstream inFile;
	cout<<endl;
	int ascii;
    
	inFile.open("data.txt");
	while(!inFile.eof())
	{
		inFile >> ascii;	
		cout << (char)ascii;
	  
		
	}
	cout<<endl;
	
	inFile.close();
	selection=0;
}

which works fine except that it prints the last character twice

Comments
That's already a lot better, now take a look at the next posts :)
while(!inFile.eof())

Don't use eof() inside a loop like that, simply do what ArkM suggested and do this:

while ( inFile >> ascii ) {
  cout << (char)ascii;
}

Hope this helps.

Comments
thank you
Yes!

>which works fine except that it prints the last character twice
That's because of while(!inFile.eof()) , take a look at William's post :)

When you implement .eof(), things doesn't often work they way you think it does.
The problem is not in the .eof but in your usage of eof().
Read this thread on Stack Overflow, wherein the OP faces exactly similar problem. The first answer(in green) has brilliant explanation regarding this.

This question has already been answered. Start a new discussion instead.