#include <iostream>
using namespace std;
//數據 輸入 輸出 octal hex dec
//int main()
//{
// char buffer[] = "2020 - hello";
// char ch[10];
// int n;
//
// cout.write(buffer, 10);
// cout.put('A');
// //cout << "cout\r\n" << endl;
// //cin.get( ch , 80, '.');
// //cout << "cout" << ch << endl;
//
// cout << "enter a octal number:";
// cin >> dec >> n ;
// cout << "octal:" << oct << n;
// cout << "hex:" << hex << n;
// cout << "dec:" << dec << n;
//
// getchar();
// getchar();
// return 0;
//}
#include <fstream>
int main()
{
/*判斷文件結束
1.>>讀:可以通過判斷輸入流對象值是否爲 0
2.get讀:判斷讀入的字符是否是EOF
3.其他方式讀:通過成員函數eof, eof函數不需要參數,返回一個整形值。當讀操作遇到文件結束時,
該函數返回 1 ,否則返回 0 。
*/
/*
//1.打開輸入文件
//第一種方式:
//ifstream infile;
//infile.open("file1");
//infile.open("file1",ifstream::in);
//第二種方式:利用構造函數直接打開
ifstream infile("file1");
ifstream infile("file1", ofstream::out);
//2.打開輸出文件
//第一種方式:
//ofstream outfile;
//outfile.open("file2");
//outfile.open("file2",ofstream::out);
//第二種方式:
ofstream outfile("file2");
ofstream outfile("file2", ofstream::out);
//3.打開輸入輸出文件
fstream iofile("file3");
fstream iofile("file3", fstream::in | fstream::out);
*/
cout << "fstream \n"<<endl;
ofstream out("file_out.txt");
ifstream in;
int i;
if (!out)
{
cerr << "create file error\n"; getchar(); return 1;
}
for (int i = 0; i < 10; i++)
{
out << i << ' ';
}
out.close();
in.open("file_out.txt");
if (!in)
{
cerr << "open file error \n"; getchar(); return 1;
}
while (in >> i)
cout << i << ' ';
in.close();
getchar();
return 0;
}
#include<iostream>
#include<fstream>
#include <iomanip>
using namespace std;
int main()
{
char data_arry[1024] = { 0 };
char d;
int byte_count = 0;
int _BYTE = 0;
char _BYTE_index = 0;
char file_name_in;
char file_name_out;
ifstream read("gw1n4b.fs");
ofstream write("gw1n4b_hex.txt");
if (!read)
{
cerr << "error cannot open gw1n4b.fs \r\n" << endl; getchar(); return 0;
}
if (!write)
{
cerr << "error cannot open hex.txt\n" << endl; getchar(); return 0;
}
//read.getline(data_arry,1024);
//for (int i = 0; i < 1024; i++)
//{
// cout << data_arry[i] << " ";
// if (data_arry[i] == '\0')
// cout << "read end";
//}
write << "bitstream[]= \n {";
for (int i = 0; i < 20; i++)
{
write << "0x" << hex << 255 << ",";
}
write << "\n";
while (read>>d)
{
if ((_BYTE_index % 8 == 0) && (_BYTE_index > 0))
{
write << "0x" << setw(2) << setfill('0') << _BYTE << ",";
//cout <<hex <<_BYTE << " ";
//printf("[%x] ", _BYTE);
byte_count++;
_BYTE = 0;
_BYTE_index = 0;
if (byte_count % 16 == 0)
write << "\n";
}
if ( d == '1' || d == '0')
{
_BYTE <<= 1;
_BYTE_index++;
}
if (d == '1')
{
_BYTE += 1;
}
}
write << "};" ;
read.close();
write.close();
cout << "byte_count:" << byte_count<<endl;
//getchar();
return 0;
}