Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!news-xxxfer.readnews.com!panix!panix.com!mrbrklyn From: Ruben Safir Newsgroups: comp.lang.c++ Subject: Re: reading from a temp file Date: Sat, 2 Jul 2011 00:47:27 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 112 Message-ID: References: <96ti78FuacU2@mid.individual.net> NNTP-Posting-Host: panix2.panix.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: reader1.panix.com 1309567647 23575 166.84.1.2 (2 Jul 2011 00:47:27 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Sat, 2 Jul 2011 00:47:27 +0000 (UTC) User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (NetBSD/5.1 (i386)) Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:7340 Michael DOUBEZ wrote: > On Jun 28, 10:37=A0am, Ian Collins 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 ,st= d::istream&in ){ >> >> > =A0 =A0 =A0 =A0char default_editor[20000] =3D "vi "; >> > =A0 =A0 =A0 =A0char * editor; >> > =A0 =A0 =A0 =A0char tmp[4096]; >> > =A0 =A0 =A0 =A0char path[] =3D "/tmp/fileXXXXXX"; >> >> Why don't you use strings? >=20 > because mkstemp expect a char* that is modified. A solution is to > using std::vector instead. >=20 std::vector or std::vector ? >> >> > =A0 =A0 =A0 =A0int fd; >> > =A0 =A0 =A0 =A0fd =3D mkstemp(path); >> > =A0 =A0 =A0 =A0editor =3D getenv("EDITOR"); >> > =A0 =A0 =A0 =A0if (editor =3D=3D NULL){ >> > =A0 =A0 editor =A0=3D default_editor; >> > =A0 =A0 =A0 =A0} >> >> > =A0 =A0 =A0 =A0strcat(editor, path); >> > =A0 =A0 =A0 =A0char quest_prompt[] =3D "Edit Your Question"; >> > ..... >> > =A0 =A0 =A0 =A0std::string input_q, input_a; >> > =A0 =A0 =A0 =A0while( ( c=3Dwgetch(quest_win) ) ){ >> > =A0 =A0 if ( (c =3D=3D 10) ){ >> > =A0 =A0 =A0 =A0def_prog_mode(); >> > =A0 =A0 =A0 =A0endwin(); >> >> What are these? >> >> > =A0 =A0 =A0 =A0system(editor); >> > =A0 =A0 =A0 =A0reset_prog_mode(); >> >> and this? >=20 > 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. Those above are part of ncures.h, but those below as was you describe. >=20 > 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. >=20 The editor is creating a text file so you might be correct, but I don't think I want carriage returns altered or interpreted, just eventually dumped into a data file at 4096 bit clips. And I don't want to deal with deliminators or terminators on input, or output, which is a problem with getline and getchar. I want them included in the input stream, whole. > 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). >=20 It didn't have trouble with two seperate streams from the same fd. I did have trouble using one stream for both in and out. >> > =A0 =A0 =A0 =A0 __gnu_cxx::stdio_filebuf< =A0char> =A0 tmpfile_buf(f= d, std::ios::binary|std::ios::app); >> >> What's one of those? >> >> > =A0 =A0 =A0 =A0 std::istream is(&tmpfile_buf); >> > =A0 =A0 =A0 =A0 is.seekg(0, std::ios::beg); >=20 > seekg is not guaranteed to work > Upon input? It didn't work away. I thought maybe I as set to the end of the stream but it didn't like binary mode at all, and the eof bits behaved as I expected so I can only guess that it was behaving OK. =20 >> > =A0 =A0 =A0 =A0 while(is){ >> > =A0 =A0 =A0 =A0 =A0 =A0is.read( tmp, 4096); >> > =A0 =A0 =A0 =A0 =A0 =A0std::cerr<< =A0"What did we read"<< =A0tmp<< = =A0std::endl; >> > =A0 =A0 =A0 =A0 =A0 =A0input_q =3D tmp; >> > =A0 =A0 =A0 =A0 } >> > =A0 =A0 =A0 =A0 tmpfile_buf.close(); >=20 > Note that you can dump the content of a file with: > std::err< I want a guaranteed chunk of 4096, no greater. =20 >> > =A0 =A0 =A0 =A0 break; >> > =A0 =A0 } >> > =A0 =A0 =A0 =A0} >> > =A0 =A0 =A0 =A0remove(path); >> >=20 > -- > Michael