我從機器人獲得測試文件,我必須通過正在開發的C++程序進行編程。所以我想用這個文件來看看機器人如何保存點的座標。我的程序目前能夠計算座標,現在我必須生成機器人代碼。將未知文件格式打印到屏幕
因此我想看看這個文件。但似乎該文件是以二進制模式寫入的。所以我的第一個想法是:以二進制模式打開文件並將內容打印到屏幕上。所以這是我正在使用的代碼:
//#include "stdafx.h"
#include <iostream> // std::cout
#include <fstream> // std::ifstream
#include <Windows.h>
int main() {
std::ifstream is ("Test.PRG", std::ifstream::binary);
if (is) {
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... ";
// read data as a block:
is.read (buffer,length);
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
// ...buffer contains the entire file...
for(int i=0; i<length; i++)
{
std::cout << (double) buffer[i] << std::endl;
}
delete[] buffer;
}
Sleep(10000);
return 0;
}
但是,使用此代碼,我只是不能看到什麼是寫在文件中。我也嘗試了不同的對話,而不是(雙)。我用char,int和float。現在我只是不知道,我能做更多。有沒有可能的方法來讀取此文件並將其轉換爲ASCII?我也在這裏添加文件的鏈接,所以你可以看看它。 Download link for file
使用文本編輯器,可以查看二進制文件,如超編輯。 –
二進制文件包含*字節*或*八位字節*。值在0到255之間。可打印的ASCII字符集要小得多。所以有很多* unprintable *值(如果你選擇查看字符)。您需要將值作爲「uint8_t」讀取並以整數形式打印出來。通常是以十六進制完成的。 –
在* nix系統和Cygwin(對於Windows)中,有一個實用程序「od」(八進制轉儲),可以打印八進制和十進制文件的內容。研究它。你甚至可以獲得它的源代碼。 –