C++ C++;从文件读取并执行位操作的程序
我不是在要求密码,我是在寻求帮助。是的,这是一个课堂项目 程序读取一个包含如下内容的.txt文件C++ C++;从文件读取并执行位操作的程序,c++,C++,我不是在要求密码,我是在寻求帮助。是的,这是一个课堂项目 程序读取一个包含如下内容的.txt文件 不是10100110 和00111101 程序需要读取运算符并根据该运算符执行函数以更改字节。然后输出修改后的字节 我知道怎么做: 打开文件 从文件中读取 我可以将字节存储在数组中 我需要帮助的是: 读取操作员(AND、OR、NOT) 将每个位存储在数组中(我可以存储字节,但不能存储位) 我的代码: #include <iostream> #include <fstre
- 不是10100110
- 和00111101
- 打开文件
- 从文件中读取
- 我可以将字节存储在数组中
- 读取操作员(AND、OR、NOT)
- 将每个位存储在数组中(我可以存储字节,但不能存储位)
#include <iostream>
#include <fstream>
#include <istream>
#include <cctype>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
const int SIZE = 8;
int numbers[SIZE]; // C array? to hold our words we read in
int bit;
std::cout << "Read from a file!" << std::endl;
std::ifstream fin("small.txt");
for (int i = 0; (fin >> bit) && (i < SIZE); ++i)
{
cout << "The number is: " << bit << endl;
numbers[i] = bit;
}
fin.close();
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
常数int SIZE=8;
int numbers[SIZE];/C array?保存我们读入的单词
整数位;
std::cout bit)&(i cout首先:将整数[SIZE];
更改为std::vector numbers(SIZE);
(#include
)
第二:我只见过这样的ifstream:
std::ifstream ifs;
ifs.open("small.txt");
第三,我的回答是:
您忘记阅读操作员,请尝试:
#include <string>
#include <vector>
int main()
{
using namespace std; // better inside than outside not to cause name clash.
const int SIZE = 8;
vector<int> numbers(SIZE);
ifstream ifs;
ifs.open("small.txt");
if(!ifs.is_open())
{
cerr<< "Could not open file"<<endl;
abort();
}
string operator_name;
for (int i = 0; !ifs.eof() && (i < SIZE); ++i)
{
ifs >> operator >> bit;
cout << "The operator is" << operator_name <<endl;
cout << "The number is: " << bit << endl;
numbers[i] = bit;
}
ifs.close(); // although ifs should manage it by itself, that is what classes are for, aren't they?
return 0;
}
#包括
#包括
int main()
{
使用命名空间std;//内部比外部更好,不会导致名称冲突。
常数int SIZE=8;
矢量数(大小);
国际单项体育联合会;
ifs.open(“small.txt”);
如果(!ifs.is_open())
{
cerr>bit;
您是否正在执行fin>>位
操作,其中位
未初始化。如果我将文件更改为以“10110100”这样的字节开头它只打印这些内容。否则它什么也不打印。您可以将运算符作为字符串读入。您必须决定是将其余内容作为字符串读入,还是将其作为多个字符读入。如果您的文本文件包含一个数字序列,并且您尝试将其读入int
,您将获得例如1000万个字符十万一千一百。