c++文件尾部问题
该程序将一个文件以16进制显示并写入新文件,但还未到文件尾部就退出了
#include<fstream>
#include <stdlib.h>
#include<iostream>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
char ch;
int ch1 = 255;
int i;
unsigned int x, y;
int k = 0;
char a, b, c = ' ', d = '\n';
infile.open("1.hex", ios::in);
if (!infile)
exit(0);
outfile.open("1.ox", ios::out);
while (!infile.eof())
{
infile.get(ch);
i = ch&ch1;
x = i; //获取char前四位并转换为int再转换为符号
x = x >> 4;
if (x < 10)
x = x + 48;
else
x = x + 87;
a = (char)x;
y = i; //获取char后四位并转换为int再转换为符号
y = y << 28;
y = y >> 28;
if (y < 10)
y = y + 48;
else
y = y + 87;
b = (char)y;
outfile.put(a);
cout << a;
outfile.put(b);
cout << b;
if (k < 15)
{
k++;
outfile.put(c);
cout << c;
}
else
{
k = 0;
outfile.put(d);
cout << d;
}
}
infile.close();
outfile.close();
return 0;
}