Re: reading from a temp file
On Jun 28, 10:37 am, Ian Collins <ian-n...@hotmail.com> wrote:
On 06/28/11 08:34 PM, Ruben Safir wrote:
any clue why this isn't retrieving the contents of the temp file
Question Deck::create_question(WINDOW* menu_win, std::ostream&os ,std::=
istream&in ){
char default_editor[20000] = "vi ";
char * editor;
char tmp[4096];
char path[] = "/tmp/fileXXXXXX";
Why don't you use strings?
because mkstemp expect a char* that is modified. A solution is to
using std::vector<char> instead.
int fd;
fd = mkstemp(path);
editor = getenv("EDITOR");
if (editor == NULL){
editor = default_editor;
}
strcat(editor, path);
char quest_prompt[] = "Edit Your Question";
.....
std::string input_q, input_a;
while( ( c=wgetch(quest_win) ) ){
if ( (c == 10) ){
def_prog_mode();
endwin();
What are these?
system(editor);
reset_prog_mode();
and this?
A gnu extension for building a filebuf from a file descriptor. IMO
closing the fildescriptor returned and using a simple ifstream with
path, would be less trouble.
In particular, the openmode are not good: binary will not translate
carriage return and app is irrelevant in input mode (and input mode is
missing). Use plain std::ios::in instead.
I don't know how it works in conccurent access, stdio_sync_filebuf<>
may be required here (with minimal buffer size in parameter for good
measure).
__gnu_cxx::stdio_filebuf< char> tmpfile_buf(fd, =
std::ios::binary|std::ios::app);
What's one of those?
std::istream is(&tmpfile_buf);
is.seekg(0, std::ios::beg);
seekg is not guaranteed to work
while(is){
is.read( tmp, 4096);
std::cerr<< "What did we read"<< tmp<< =
std::endl;
input_q = tmp;
}
tmpfile_buf.close();
Note that you can dump the content of a file with:
std::err<<is.rdbuf();
--
Michael