ASCII

Help Help!!!!!!!!!

Question:
Write a program that reads the integers in the file “flints.txt” (the integers stored in the file are the ASCII code for characters), converts each integer to a char, and outputs each character a text file. There is no need for an array. ASCII table can be found here.

Note that given an int value called number (which stores the ASCII code for a character), either of the following statements can convert number to the corresponding char.
char theChar = static_cast<char>(number);

char theChar = (char) number;


My Program:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main()
{

ifstream myfile ("code");
string line;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}


else cout << "Unable to open file";

return 0;
}


If you could explain to me what to do next and why, that would be great. :)
Last edited on
Ignoring the bad assumptions made by the assignment:

Just read character codes from the file, convert them to characters, and spit them out to the other file.
1
2
3
4
5
# include <iostream>
int main() {
  for (int ccode; std::cin >> ccode; ) 
    std::cout << static_cast<char>(ccode);  
}


I'll leave it to you to replace std::cin and std::cout with the appropriate file-streams.

Live demo:
http://coliru.stacked-crooked.com/a/1fe6b35f1fbbaade
Last edited on
Yeah thank you i got the code working but it is not outputting anything so i am just wondering if i saved the file that i am using wrong. So is there a special process of saving these file???
Hello srk100,

Which file(s)?

Also post your revised code so the changes can be seen not guessed at.

And,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main()
{

  ifstream myfile ("code");
  if (myfile.is_open())
  {
      cout << myfile << '\n';

       myfile.close();
  }

  else cout << "Unable to open file";

  return 0;
}
The fille i need to convert to Ascii contains integers. Also my mistake the code in not working.
Last edited on
1
2
3
4
5
6
# include <fstream>
int main() {
  std::ifstream input("code"); // open the file named code 
  for (int ccode; input >> ccode; ) // while we successfully read integers from the file
    std::cout << static_cast<char>(ccode);  // convert them to chars, send to standard output
}
great thank you, any advice on saving this file so it can be read by the program.
According to the instructions, the input file must be named flints.txt

You can create a txt file in notepad.
Topic archived. No new replies allowed.