【发布时间】:2020-06-28 17:49:04
【问题描述】:
大家好,我有一个问题。我有一个程序将二进制数转换为十进制数。但我想从文件转换二进制数。例如,在我的 txt 文件中是这样的二进制数: 10000111001 10001000100 100010110 100001000 00010010011 我想在我的程序中打开这个 txt 文件并将这个数字转换为十进制 我已经创建了类似的东西,但它没有保存我的输出
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
int bin2dec(string binary)
{
int decimal = strtol(binary.c_str(), NULL, 2);
return decimal;
}
int main()
{
string number;
ifstream one("data.txt") //here are mine binary numbers
ofstream two("second.txt") // i would like to save my converted numbers in this file
while (!one.eof())
{
one >> number;
number = bin2dec(number);
two << number;
}
system("pause >nul");
return 0;
}
【问题讨论】: