-1
我正在寫一個使用ncurses的文本編輯器,並且我想讓程序保存我寫入的文本文件。我現在所節省的東西是我寫的,但是我在字之間得到了一堆空字符......有時候保存的字符與我輸入的字符不同。我假設它與矢量存儲字符串的方式有關?或者也許推回功能留下空字符?向量的字符串返回空字符
main.cpp中:
#include<iostream> //g++ *.cpp -lncurses -o run
#include<fstream>
#include<string>
#include<ncurses.h>
#include<vector>
#include"draw.h"
using namespace std;
vector <string> file;
int nWords = 1;
int save(){
ofstream save;
save.open("/home/adam/editorText", ofstream::out);
for(int i=0; i<nWords; i++)
{
save.write(file[i].c_str(), sizeof(file[i]));
}
}
int colourCheck(){
if(has_colors() == false){
endwin();
cout<< "ERROR: your terminal does not support colours\n";
cout<< "exiting \n";
return 1;
}
else{
return 0;
}
}
int init()
{
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
start_color();
}
bool on = true;
int main()
{
string word = "";
const char * color = "blue";
init();
colourCheck();
drawGraphics draw;
draw.setDrawBox(0, 3, 0, 20);
draw.fillDrawBox(1, COLOR_BLACK, COLOR_BLUE);
while(on){
int chr= getch();
switch(chr){
case(32): //ascii value of 'space'
file.push_back(word);
printw("%c", chr);//
word = "";
nWords++; //every time space bar is pressed: nwords++
break;
case(27): //ascii value of 'ESC'
file.push_back(word);
save();
on = false;
break;
case(15):
default:
printw("%c", chr);
word.push_back(chr);
break;
}
}
endwin();
return 0;
}
您是否已經調試代碼,以縮小問題的根源? –