小编new*_*bie的帖子

C++困惑.从文本文件中读取整数.转换为ASCII

我是第一次学习C++.我以前没有编程背景.

我在书中看到了这个例子.

#include <iostream>

using::cout;
using::endl;

int main()
{
    int x = 5;
    char y = char(x);

    cout << x << endl;
    cout << y << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个例子很有意义:打印一个整数及其ASCII表示.

现在,我创建了一个包含这些值的文本文件.

48
49
50
51
55
56
75
Run Code Online (Sandbox Code Playgroud)

我正在编写一个程序来读取这个文本文件 - "theFile.txt" - 并希望将这些数字转换为ASCII值.

这是我写的代码.

#include <iostream>
#include <fstream>

using std::cout;
using std::endl;
using std::ifstream;

int main()
{
    ifstream thestream;
    thestream.open("theFile.txt");

    char thecharacter;  

    while (thestream.get(thecharacter))
    {
        int theinteger = int(thecharacter);
        char thechar = char(theinteger);
        cout << theinteger << "\t" << …
Run Code Online (Sandbox Code Playgroud)

c++ int ascii char new-operator

11
推荐指数
2
解决办法
5778
查看次数

标签 统计

ascii ×1

c++ ×1

char ×1

int ×1

new-operator ×1