簡體   English   中英

C ++ / Qt:無效的STL文件-無法讀取前6個字節

[英]C++/Qt : Invalid STL file - could not read first 6 bytes

std::string fname = "cube.stl";
FILE *inf = std::fopen(fname.c_str(), "rb");
if (inf == NULL) {
    //throw std::runtime_error(std::string("Cannot open file ") + fname);
    qglColor(Qt::black);
    this->renderText(10, 10, "Cannot open file ");
}
char buffer[6];
size_t bytes_read = fread(buffer, 1, 6, inf);//copy file to buffer
QString bytes_read_diaplay = QString::number(bytes_read);
qglColor(Qt::black);
this->renderText(10, 300, bytes_read_diaplay);

if (bytes_read != 6) {
    qglColor(Qt::black);
    this->renderText(10, 10, "Invalid STL file - could not read first 6 bytes.");
}

std::rewind(inf);

if (0 == std::memcmp(buffer, "solid ", 6)) {
    //read_ascii_file(inf);
    qglColor(Qt::black);
    this->renderText(10, 30, "read_ascii_file");
} else {
    //read_binary_file(inf);
    qglColor(Qt::black);
    this->renderText(10, 30, "read_binary_file");
    char buffer[80];
    size_t num_read = fread(buffer, 1, 80, inf);
    if (num_read != 80) {
        qglColor(Qt::black);
        this->renderText(10, 50, "Invalid binary STL file - could not read 80 byte header.");
    } 
    std::memcpy(header, buffer, 80);

    unsigned int num_tris = 0;
    num_read = std::fread(&num_tris, sizeof(unsigned int), 1, inf);
    if (num_read != 1) {

        qglColor(Qt::black);
        this->renderText(10, 70, "Invalid binary STL file - could not read number of triangles from binary STL file.");
    }


}
std::fclose(inf);

我得到bytes_read是4,所以我得到了:

無效的STL文件-無法讀取前6個字節。

read_binary_file

無效的二進制STL文件-無法讀取80字節的標頭。

無效的二進制STL文件-無法從二進制STL文件讀取三角形數量。

“ cube.stl”是.stl文件,但是為什么會顯示這樣的文件? 我哪里錯了? 如何在cube.stl中獲得頂點坐標? 我的.stl文件是: http : //www.mediafire.com/download/hnnw444anih201n/cube.stl


解決了

經過檢查和檢查並檢查。 終於,我找到了方向。 D ***,錯誤很簡單。 我更改了std :: string fname =“ cube.stl”的路徑; (將cube.stl轉換為D:/.../ cube.stl)。 太奇妙了。 跑。

您似乎對Ascii和Binary文件之間的區別有些困惑。

fopen的“ rb”標志將作為二進制文件打開。 cube.stl是二進制文件嗎? .stl文件可以在Binary或Ascii中。

此外,您如何驗證文件大小? 您期望輸入6個char或80個字節的二進制。 這些數字從哪里來?

簡而言之,您的問題是:

  • 是cube.stl Ascii嗎?
  • 標頭的大小是多少?
  • .stl文件中的二進制數據格式是什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM