2015-02-11 84 views
1

這是我在stackoverflow中的第一篇文章。 我寫了一個程序,該程序應該在輸入文件中輸入一個bmp文件,然後黑白文件,然後將其寫入out.bmp。 當我開始編寫代碼時,我在輸入名稱末尾刪除了bmp文件格式,然後用文本編輯器打開它,然後編寫代碼,輸出樣式就像輸入。 當我在終端輸入./a.out <in.bmp>out.bmp,我得到一箇中止錯誤(Aborted (core dumped)),當我給./a.out <in> out.bmp gimp對我說這不是一個bmp文件。 這裏是代碼:嘗試讀取和寫入bmp文件時中止C++

// In the Name of God 

#include <iostream> 
#include <vector> 
#include <string> 
#include <cstdlib> 
#include <algorithm> 
#include <math.h> 
#include <sstream> 

using namespace std; 

bool TypeIsTrue (string type){ 
    if (type == "424d") 
     return true; 
    return false; 
} 
int GrayIt (int b , int g , int r){ 


    return (b + g + r)/3 ; 
} 
int ConvertToDec(string hex){ 
    long int dec = 0; 
    reverse(hex.begin(), hex.end()); 
    for (int i = 0 ; i <hex.size();i++){ 
     if (hex[i] == 'a') 
      dec = dec + 10 *(pow(16,i)); 
     else if (hex[i] == 'b') 
      dec = dec + 11 *(pow(16,i)); 
     else if (hex[i] == 'c') 
      dec = dec + 12 *(pow(16,i)); 
     else if (hex[i] == 'd') 
      dec = dec + 13 *(pow(16,i)); 
     else if (hex[i] == 'e') 
      dec = dec + 14 *(pow(16,i)); 
     else if (hex[i] == 'f') 
      dec = dec + 15 *(pow(16,i)); 
     else 
      dec = dec + ((hex[i] - '0')*(pow(16,i))); 
    } 
    return dec; 
} 
string ConvertToHex(int dec){ 
    string hex; 
    int reminded,Divided; 
    reminded = dec % 16 ; 
    Divided = dec/16; 
    if (Divided == 10){ 
     hex = "a"; 
    } 
    else if (Divided == 11){ 
     hex = "b"; 
    } 
    else if (Divided == 12){ 
     hex = "c"; 
    } 
    else if (Divided == 13){ 
     hex = "d"; 
    } 
    else if (Divided == 14){ 
     hex = "e"; 
    } 
    else if (Divided == 15){ 
     hex = "f"; 
    } 
    else if (Divided == 0){ 
     hex = "0"; 
    } 
    else if (Divided == 1){ 
     hex = "1"; 
    } 
    else if (Divided == 2){ 
     hex = "2"; 
    } 
    else if (Divided == 3){ 
     hex = "3"; 
    } 
    else if (Divided == 4){ 
     hex = "4"; 
    } 
    else if (Divided == 5){ 
     hex = "5"; 
    } 
    else if (Divided == 6){ 
     hex = "6"; 
    } 
    else if (Divided == 7){ 
     hex = "7"; 
    } 
    else if (Divided == 8){ 
     hex = "8"; 
    } 
    else if (Divided == 9){ 
     hex = "9"; 
    } 
    if (reminded == 10){ 
     hex = hex+"a"; 
    } 
    else if (reminded == 11){ 
     hex = hex+"b"; 
    } 
    else if (reminded == 12){ 
     hex = hex+"c"; 
    } 
    else if (reminded == 13){ 
     hex = hex+"d"; 
    } 
    else if (reminded == 14){ 
     hex = hex+"e"; 
    } 
    else if (reminded == 15){ 
     hex = hex+"f"; 
    } 
    else if (reminded == 0){ 
     hex = hex+"0"; 
    } 
    else if (reminded == 1){ 
     hex = hex+"1"; 
    } 
    else if (reminded == 2){ 
     hex = hex+"2"; 
    } 
    else if (reminded == 3){ 
     hex = hex+"3"; 
    } 
    else if (reminded == 4){ 
     hex = hex+"4"; 
    } 
    else if (reminded == 5){ 
     hex = hex+"5"; 
    } 
    else if (reminded == 6){ 
     hex = hex+"6"; 
    } 
    else if (reminded == 7){ 
     hex = hex+"7"; 
    } 
    else if (reminded == 8){ 
     hex = hex+"8"; 
    } 
    else if (reminded == 9){ 
     hex = hex+"9"; 
    } 
    return hex; 
} 
int main(){ 
    vector <string> a; 
    vector <string> r; 
    vector <string> g; 
    vector <string> b; 
    vector <string> out; 
    string temp; 
    int red,green,blue; 
    while(cin >> temp){ 
     a.push_back (temp); 
    } 
    if(!TypeIsTrue(a[0])){ 
     cout<<"The file is not bmp\nRerun program"<<endl; 
     abort(); 
    } 
    int phase = 1; 
    for (int i = 27 ; i < a.size(); i++){ //int i = 27 

     string first; 
     string last; 
     first = a[i].substr(0,2); 
     last = a[i].substr(2,3); 
     if(phase == 4) 
      phase = 1; 
     if(phase == 1){ 
      b.push_back(first); 
      g.push_back(last); 
      phase ++; 
      // cout<<"push_backed"<<endl; 
     } 
     else if(phase == 2){ 
      r.push_back(first); 
      b.push_back(last); 
      phase ++; 
      // cout<<"push_backed"<<endl; 
     } 
     else if(phase == 3){ 
      g.push_back(first); 
      r.push_back(last); 
      phase ++; 
      // cout<<"push_backed"<<endl; 
     } 

    } 
    for (int i = 0 ; i <27 ; i++){ 
     out.push_back(a[i]); 
    } 
    for(int i = 27 ; i<b.size() ; i++){ 
     blue = ConvertToDec(b[i]); 
     green = ConvertToDec(g[i]); 
     red = ConvertToDec(r[i]); 
     out.push_back (ConvertToHex(GrayIt (blue , green , red))); 
     out.push_back (ConvertToHex(GrayIt (blue , green , red))); 
     out.push_back (ConvertToHex(GrayIt (blue , green , red))); 
    } 
    int j = 1 ; 
    for (int i = 0 ; i < 27 ; i++){ 
     cout<< out[i] << " "; 
     if (j == 8){ 
      cout<<endl; 
      j = 0; 
     } 
     j++; 
    } 
    j=1; 
    bool space = false; 
    for (int i = 27 ; i < out.size(); i++){ 
     if(i == 27 + 10){ 
      cout<<endl; 
      j = 1; 

     } 
     cout<<out[i]; 
     if (space) 
      cout<<" "; 
     j++; 
     if(j == 17){ 
      cout<<endl; 
      j = 1 ; 
     } 
     space=!space; 
    } 
    return 0; 
} 
+0

請花些時間來學習如何使用編譯器的調試器。這就是它的目的。 – OldProgrammer 2015-02-11 20:06:20

回答

1

你得到中止錯誤,因爲你問一個。

if(!TypeIsTrue(a[0])){ 
    cout<<"The file is not bmp\nRerun program"<<endl; 
    abort(); 
} 

如果你的程序被設計爲具有其輸出重定向,這是非常重要的錯誤信息發送到stderr(和std::cerrstd::clog),而不是stdout

順便說一句,類型測試失敗,因爲BMP文件不是文本,使用cin >> variable讀取它們是沒有意義的。

此外,不能保證a[0]甚至存在。您首先需要測試a.size()

if(a.size() < 1 || !TypeIsTrue(a[0])){ 
    cerr << "The file is not bmp\nRerun program\n"; 
    abort(); 
} 
+0

沒有cout消息,如果你評論它,中止會再次發生。 – 2015-02-11 20:46:01

+0

@Ali:您正在向'out.bmp'文件發送'cout'。另外,調用'abort()'時,緩衝區中的任何數據都不會出現。這就是爲什麼你需要使用'cerr'。 – 2015-02-11 21:36:16

+0

我想我應該使用fstream頭文件作爲bmp圖像從輸入中讀取數據。我應該把輸出作爲圖像文件,所以cerr本身並不能解決問題。 – 2015-02-12 07:26:07