C++中如何按位或按字节读文件?

来源:百度知道 编辑:UC知道 时间:2024/09/18 08:05:53
如果我想读文件.用较为底层的方式读取.如何实现?
用ifstream或者 FILE*的bin模式吗?

我要读入这个文件的所有字节,包括换行符等等等等..比如说中文字符,我可能就要读它的第一个字节,这如何实现??

可以追加的~

/*
假如我要读取文件chengji.txt中的数据。
文件中数据如下:
学生编号 数学 英语
1 80 90
2 66 67
怎样求各学生的平均成绩和总的平均成绩
*/

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
string line;
int head=0,count=0,num;
float math,english,sum_math=0,sum_english=0,average;

ifstream ifs("chengji.txt");
if(!ifs) return -1;

ofstream ofs("chengji_result.txt");
if(!ofs) return -2;

while(getline(ifs,line))
{
istringstream is(line);
if(head==0)
{
//跳过第一行的表头
head=1;
continue;
}
is>>num>>math>>english;
if(count==0)
{
ofs<<"学生编号\t平均成绩"<<endl;
}
ofs<<num<<"\t"<<(math+english)/2<<endl;
sum_math+=