我不是要求代码我正在寻求帮助。是的,它适用于课堂上的项目。
程序读取包含类似内容的.txt文件,
程序需要读取运算符并根据该运算符执行函数来更改字节。然后输出改变的字节。
我知道该怎么做:
我需要帮助:
我的代码:
#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;
}
答案 0 :(得分:0)
首先:将int numbers[SIZE];
更改为std::vector<int> numbers(SIZE);
。 (#include <vector>
)
第二:我只看过这样的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;
}