SEARCH YOUR SOLUTION HERE  

fstream getline

I'm trying to do line-at-a-time command interpreter input under eCos using
an fstream for a serial port (/dev/tty1). The fstream has a 256-byte
filebuf, and it's doing 256-byte fread() calls underneath. However, even
though fread() gets a short read from the underlying device when I hit
Enter, it goes back and reads more, until it gets the full 256 bytes--which
perversely seems to be what the C standard requires it to do.

Is getline() generally supposed to work as one would expect on interactive
devices Is one expected to do a setvbuf on it, either to disable
buffering,
or to supply a one-character buffer I've tried both of these under eCos,
and neither works, but I wouldn't be a bit surprised if eCos's
implementation of fstreams, or for that matter the C stdio library, is
buggy.

--

Ciao, Paul D. DeRocco
Paul mailtoderocco@ix.netcom.com



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Posted On: Monday 5th of November 2012 01:38:15 AM Total Views:  236
View Complete with Replies




Related Messages:

K&R reverse polish calculator (modified for getline use)   (118 Views)
all: Im working on an exercise in the k&r handbook, specifically the reverse polish calculator. Ive created my own version based on the one in the book. Im getting one compiler error saying "error: line 66 'index' redeclared as different type of symbol". Im not quite sure what's causing this error, nor really what the error message even means. Im wondering if someone can tell what this error is trying to tell me, and maybe give some suggetions as to solve my problem.
Does strcmp work with getline?   (221 Views)
I can't seem to get getline and strcmp to work together. I am reading in from a file that looks like this: Code: a b c ---- d and I want to detect the dashes. The location of the dashes is unknown. I have tried this but to no avail: Code: #include #include int main(int arc, char *argv[]) { FILE * fp; fp = fopen("file", "r"); char * line = NULL; size_t len = 0; ssize_t read; while ((read = getline(&line, &len, fp)) != -1) { if (!strcmp(line, "----\n")) { fprintf(stdout, "Dashes\n"); continue; } printf("%s", line); } return 0; }
Streaming a wstring to a wofstream   (276 Views)
Is there ever any case in which streaming a wstring to a wofstream should fail to write all the characters in the wstring to the file In code, built with VC9 in Windows, I have: std::wstring astring; // Code that fills the wstring with about 210,000 wide characters. // I can verify the size of this in the debugger. std::wofstream wofs("somefile",std::ios_base:ut | std::ios_base::binary); wofs
getline problems   (299 Views)
hi, i'm using code like this string s while(getline(cin,s)){ process(s); } // this is the last line process(s); process does some replacement and rescanning. The problem is i can't strip or add newlines. So i don't know whether the lastline contains a '\n' or it was just EOF. I can't read the whole buffer as it is too huge and some line doesn't need to be replaced. Using while(fgets(str,MAX,stdin){ s=str } works but again this conversion of str to s is an overhead. Can you suggest on how to overcome on this getline issue
Problem mixing read and write on io fstream   (200 Views)
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware of. f.open(filename,ios::in | ios:ut | ios::binary | ios::trunc) The program flow is 1) write some data 2) read the data 3) overwrite old data //this fails I am aware that mixing reading and writting is complicated due to buffering and as far as I know the solution is to do a seekg or seekp when changing between reading and writting, I am doing this between 1, 2 and 3. In 'my' program, writting in step 3 fails. I wrote a simple program that does just the above 3 steps with seeks and it works but in my program, it sets the failbit. After lots of debugging in internal fstream code I figured that an internal flag _IOREAD gets set somehow in step 2 which indicates that the stream is now readonly and causes subsequent writes to fail. It also seems that _IOREAD is set anytime any read operation is done on a read+write file, is this correct And now how do I 'unset' this flag I tried calling f.clear and f.seekp but that doesnt helps. What might be special in my program that causes this read-mode flag to remain set while this doesn't happens in a barebone/simple test program. I am not doing anything except f.seekg, f.get, f.read. Sachin Garg ps. If it matters, I am using visual studio 9.0
fstream opening new file   (326 Views)
I find this peculiar. I want to use a fstream object first writing a file and then reading it. I want to create the file if it does not exists, so I do fstream aFile("DiskFile1", ios:ut|ios::in|ios::binary); I thought. But that does not create the file if it does not exist! fstream aFile("DiskFile1", ios:ut|ios::binary); does. How does one create a fstream object with read/write and create if not exist Can't figure that one out.. -totte
fclosall() and std::ofstream?   (138 Views)
Is it ok to call fcloseall() when using both C-style FILE * and C++ std:fstream's to read files in a program
How do we use an ifstream vector?   (183 Views)
std::vector in[10]; // string open_input[10]={ "TEMPLATE_1", "TEMPLATE_2", "TEMPLATE_3", "TEMPLATE_4", "TEMPLATE_5", "TEMPLATE_6", "TEMPLATE_7", "TEMPLATE_8", "TEMPLATE_9", "TEMPLATE_10" }; How do we open the first 5 files above by using the vector in When I try for (i=0; i
ofstream   (140 Views)
How to assign a default arguement to ofstream For example: void My_Function( int a, int b = 0, ofstream outFile ) { //Boby of the code } The arguement b has a default value 0. How to assign a default arguement to the arguement outFile so that the caller of the function does not need to pass a value to it
Trying to Learn C++, Problems with iostream::getline() from stringheader   (199 Views)
The following is not a complete program. Its supposed to read 2 lines from a config file and place what it reads into the line array. I am having problems using getline(). From what I have read, it looks like including the header should allow getline() to put the data into my string. The errors I am getting are: |25|error: invalid conversion from void* to char** | |25|error: cannot convert std::string to size_t* for argument 2 to __ssize_t getline(char**, size_t*, FILE*) | It looks like the compiler is still trying to use getline() from the header rather than the header. Any suggestions #include #include #include #include using namespace std; int main() { // declare variables double avalue; double bvalue; double cvalue; string line[2][2]; // Open configfile stream ifstream configfile; configfile.open("configtest.conf"); assert(! configfile.fail()); // Write data from file to line array for(int i = 0;i
ofstream   (152 Views)
I get Stack Overflow error on the following statement: .... Log
wifstream & locale   (142 Views)
all, I needed to read contents of an UTF8 encoded file in C++ (gcc version 3.3.5 on Linux). I decided to do it the C++ way, using streams: ------------- std::locale utf8loc("en_US.utf8"); std::wifstream ifs("smthg"); ifs.imbue(utf8loc); std::wstring item; while (ifs >> item) { here } ------------ but the inner block ("here") never gets executed. I tried searching around the web and my head spins, surely doing such mundane, straightforward task cannot be that complicated in C++ What am I doing wrong please What should I test for, look out for Cheers. To narrow down my problem: * I don't want to use any non-standard functions, libraries etc. * the input file is in UTF8, I want to read (parts of) its contents as wstring --------------- $ locale -a C POSIX en_GB.utf8 en_US.utf8
fstream problem   (158 Views)
As you will be able to see I am fairly new at this. Here is the part I'm having problems figuring out: The file I'm inputing from is formated like this: firstName lastName postion name (one or two words) firstName lastName first base (for example) firstName lastName position name (one or two words) I'm trying to seperate the first name from the last name putting the last name in parallel arrays with the position in the other array. The garbage array I set up just to see what I was getting rid of. When the position has two words it always gets messed up as far as what it puts in the arrays the code is runable. Any suggestions would be most welcome. TIA (This is just a C++ win32console program created in an empty solution using MS Visual Studio 2005) Code: #include #include #include using namespace std; int main() { char sentry; string garbage[9]; string player[9]; string position[9]; fstream inFile; inFile.open("team1.txt"); for(int test = 0; test < 9; test++) { inFile >> garbage[test]; inFile >> player[test]; getline(inFile,position[test]); } for(int test = 0; test < 9; test++) { cout
Help needed for STL ifstream class   (228 Views)
I've posted this in another thread, but I suppose I should've started a new thread for it instead. I cannot get the following short program to compile under g++: #include #include #include #include using namespace std; int main(int argc, char **argv) { copy(istream_iterator(argc >= 2 ifstream(argv[1]) : cin), // this line won't compile! istream_iterator(), ostream_iterator(cout)); return 0; } The compiler error messages are as followed: /usr/include/c++/4.0.0/iosfwd: In copy constructor 'std::basic_ios::basic_ios(const std::basic_ios&)': /usr/include/c++/4.0.0/bits/ios_base.h:779: error: 'std::ios_base::ios_base(const std::ios_base&)' is private /usr/include/c++/4.0.0/iosfwd:55: error: within this context /usr/include/c++/4.0.0/iosfwd: In copy constructor 'std::basic_istream::basic_istream(const std::basic_istream&)': /usr/include/c++/4.0.0/iosfwd:61: warning: synthesized method 'std::basic_ios::basic_ios(const std::basic_ios&)' first required here a.cpp: In function 'int main(int, char**)': a.cpp:10: warning: synthesized method 'std::basic_istream::basic_istream(const std::basic_istream&)' first required here a.cpp:10: error: no matching function for call to 'std::istream_iterator::istream_iterator(std::basic_istream)' /usr/include/c++/4.0.0/bits/stream_iterator.h:70: note: candidates are: std::istream_iterator::istream_iterator(const std::istream_iterator&) [with _Tp = char, _CharT = char, _Traits = std::char_traits, _Dist = ptrdiff_t] /usr/include/c++/4.0.0/bits/stream_iterator.h:66: note: std::istream_iterator::istream_iterator(std::basic_istream&) [with _Tp = char, _CharT = char, _Traits = std::char_traits, _Dist = ptrdiff_t] /usr/include/c++/4.0.0/bits/stream_iterator.h:62: note: std::istream_iterator::istream_iterator() [with _Tp = char, _CharT = char, _Traits = std::char_traits, _Dist = ptrdiff_t] Now, I have discovered that if I change the program into the following, then it compiles fine: #include #include #include #include using namespace std; int main(int argc, char **argv) { istream *ifile = argc >= 2 new ifstream(argv[1]) : &cin; copy(istream_iterator(*ifile), istream_iterator(), ostream_iterator(cout)); return 0; } I know this works, but it would be nice to understand why the original version does not work anyway. Thank you for your help. -- -kira
About ifstream   (161 Views)
all, I dont know how to fix it. The program is to open a file "the_obtained_output", then open another file "verification1". The first file can be opened successfully but I got a error message "error: unable to open the input file: 000000". I checked the codes, I found if I removed "for(float i;infile>>i prediction.push_back(i); for(vector::size_type ix=0;ix!=10;ix++) cout
string as getline delimiter   (264 Views)
all, what shoudl I do if my delimiter has more than one character Also is it possible to have 2 different delimiters
I got a strange error message when using fstream!   (286 Views)
#include #include #include using namespace std; class MyObj { public: int data; ifstream in; }; typedef std::vector MyObjVector; int main(int arc, char* argv[]) { MyObjVector myVector; MyObj obj; obj.data = 10; obj.in.open("test.txt"); myVector.push_back(obj); } In the code above, I want to use a vector of class MyObj, which have a ifstream object, but I got a message says that I can't access the private memeber of ios_base, both in MinGW and Visual C++, but if a commented the line myVector.push_back(obj); all thing is OK, can't any one help me! -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
Reading/Writing from/to a fstream ? Broken ?   (172 Views)
I'm trying to read/write from a std::fstream and came across some very strange behavior. But may be it's a feature not a bug. I didn't find much about what the standard says about it and may be someone could explain it to me. This is under GNU libstdc++ 4.1.2 Rel. 33 int main() { std::fstream f; f.open("bla",std::ios::in | std::ios:ut); if(!f) { std::cerr
Of getline(cin,...), Ctrl+D and infinite loops   (249 Views)
Here is a small program. It's meant to keep reading lines from the stdin till someone types the (really cute) string "manchu". In that case it breaks out of the loop and the program terminates. /////////////////////////////////// #include #include using namespace std; int main() { string str; char ch; while ( cin ) { cin.clear(); cin.ignore( cin.rdbuf()->in_avail() ); getline( cin, str, '\n'); if ( str == "manchu" ) break; if ( cin.eof() || cin.fail() ) { cin.clear(); } } return 0; } I am compiling this with gcc 3.3.1 on Solaris 9 SPARC, and with gcc 3.2.3 on RHEL 3. Now here is the crux. I wanted that if the user just typed "Ctrl+D", it should clear the eof and fail bits and continue the loop looking for more input. It does this nicely on Linux but goes into an infinite loop on Solaris sparc, repeatedly encountering EOF. I am guessing that this is a wrong piece of code and somehow it is working in one case, and not in the other case. And hoping that someone actually points out where I am going wrong. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
Invalidated fstream footnotes in N2134   (118 Views)
The standard wording of N2134 has extended the 14882:2003(E) wording for the ifstream/ofstream/fstream open function to fix a long standing problem, see http://www2.open-std.org/jtc1/sc22/w...fects.html#409 Now it's properly written as "If that function does not return a null pointer calls clear(), otherwise calls setstate(failbit)[..]" instead of the previous "If that function returns a null pointer, calls setstate(failbit)[..] While the old footnotes saying "A successful open does not change the error state." where correct and important, they are invalid now for ifstream and ofstream (because clear *does* indeed modify the error state) and should be removed (Interestingly fstream itself never had these, although they where needed for that time). Proposed resolution: Remove the current N2134 footnotes 335 (page 902), 336 (page 903) Greetings from Bremen, Daniel Krgler --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]