FAQ Database Discussion Community


What happens if an function fails?

c++,iostream
Transforming an old C function of mine to C++. I stumbled on the problem that i do not find the documentation for <iostream> behavior in case of an error. Just as one example - this old C function: #include <stdio.h> int OldFixedInterfaceWithErrorReturn(void) { int e = 0; int ret =...

Strange cout behavior in a while loop in C++

c++,iostream
I am trying to print the input and then all the strings in the tk vector, as in the following program: int main() { while (true) { string input; cout << "prompt: "; cin >> input; vector<string> tk = {"this", "is", "an", "example"}; cout << input << endl; for (int...

How to clear exception mask

c++,exception,exception-handling,iostream
When creating an exception mask for a file, should an exception throw during a file operation, I can reset the state bits of the actual file using ios::clear(). Though after doing so, will the exception mask still throw an exception, thinking that the specified error state flags are still set...

Writing in a txt file

c++,iostream
In trying to read and write datas in a .txt file. Reading is working fine, but each time i try to write, program overwrite on what it wrote before. Only the last thing written remains. void trace(Board& tab) { ofstream file("trace.txt", ios::in | ios::trunc); Position pos(0,0); Position& p = pos;...

Streaming binary files in C++

c++,iostream
Coming from C, I'm experimenting with C++ a bit and have stumbled on something as simple as reading binary data from a file into a buffer using an ifstream. It seems to me that I have three options to read data from the file: get(), which gets a single character...

Trouble reading row of doubles from file C++

c++,file,matrix,iostream
for some reason the code below stores a bunch of 0s into my doubles array and it doesn't write the 0s into the file im trying to create. This is my first time programming in c++ so I'm still getting used to some of the basic things. Any help is...

Java how to read folder and list files in that folder in jar enviroment instead of IDE

java,jar,iostream
my problem is that I create a folder(name is IconResoureces) under src , in IconResources there are many pictures. Directory is like this: ProjectName src package 1 package 2 IconResources(this is the target folder) I want to list all picture files name and do something with those pictures. And I...

Tying a stream to itself

c++,c++11,iostream
Why does this code run successfully on g++ and clang (libstdc++) but give a segmentation fault with libc++: #include <iostream> int main() { std::cout.tie(&std::cout); std::cout << 123; } What I think should happen is that std::cout.flush() should be called before the output actually occurs. Why would this give a segmentation...

streambuf::xsgetn and state flags

c++,iostream
The protected virtual member function streambuf::xsgetn enables I/O stream implementors to define a function which extracts n characters from an input stream and store them in a buffer. Normally, for most standard streams like fstream, this function simply call sbumpc in a loop until eof is returned. But a custom...

Is there a difference between ifstream::binary and ios::binary?

c++,file-io,iostream
I've seen code written like so: ifstream fin; fin.open("largefile.dat", ifstream::binary | ifstream::in); Now this makes me confused, is there any difference at all between the above code and this code below using ios::binary and ios::in as replacement? ifstream fin; fin.open("largefile.dat", ios::binary | ios::in); ...

trying to implement simple ostream singleton class

c++,singleton,iostream
I want to implement a singleton class that receives file path as a parameter. I tried to write the following code . I know it doesn't work and not good but I can't find why.. class OutputData { std::fstream ofile; std::ostream iout; static OutputData *odata; OutputData(const char* path):iout(std::cout), ofile(path) {...

Input using getline(cin,n); is not printing first input and i am not using cin>> to take input anywhere

c++,iostream,cin,getline
I am trying to print inputs until user gives a blank input.So,I used getline(cin,input).But,When i use getline(cin,input).It is skipping first input while giving output. #include <iostream> using namespace std; int main() { while(1) { string n; getline(cin, n); while(getline(cin,n) && !n.empty()) { cout<<n<<endl;; } if(n.empty()) break; } return 0; }...

Why two EOF needed as input? [duplicate]

c++,iostream,eof
This question already has an answer here: Canonical vs. non-canonical terminal input 1 answer When I run the code below, I use three inputs (in Ubuntu terminal): abc(Ctrl+D)(Ctrl+D) abc(Ctrl+D)(Enter)(Ctrl+D) abc(Enter)(Ctrl+D) The code reacts well in all cases. My question is: why in 1) and 2) I need two EOF?...

Ifstream.getline() - Only reading first line?

c++,iostream
I'm just trying to run a simple c++ program that will format a .txt file with data entries. I have run it with many different text files of the exact same format, and now it just won't work. I'm sure the solution is simple. Here is a simplified version of...

Problems with iostream library and gcc

c++,gcc,compiler-errors,iostream
I have tried to compile many times a small C++ program but it fails everytime I used gcc to compile. I'm using Cygwin64 and this is the code: #include <iostream> #include <string.h> using namespace std; #define MAX 100 using namespace std; #define MAX 100 int main(){ int n; cout<<"Caantidad de...

C++ Reading from txt file into object binary tree

c++,operator-overloading,iostream
***Edited code with my changes--not getting access violation anymore and it's assigning all my values perfectly except for the Customer ID--it's just leaving that blank. Is that because the ID is numeric and not technically a string?? This is homework--We are supposed to read data from a .txt file, then...

Unable to read two strings with cin.get()

c++,iostream
Why does trying to input two strings using cin.get() fails? I can successfully read the first string but the input fails for second string and subsequent operations.. See the code: #include <iostream> #include <stdlib.h> int main(){ long int n,k; char a[11],b[11]; cin.get(a,11); n = atoi(a); cin.get(b,11); cout<<b; k = atoi(b);...

Infinity loop while reading data from file

c,iostream
I'm trying to read data from file. There are three rows. What I've done is this below. Problem is that (file exists) it is infinity loop while reading a file. I've observed that program is not moving line by line until it reaches end of file. What's incorrect in my...

Connecting two streaming functions c++

c++,stream,iostream,ostream,istream
I have two functions: Get (std::ostream* os) and Put (std::istream& is) The way get functions is, it has multiple streams underneath from which it gathers the data and sends it out via the output stream os. Put on the other hand, writes the data from input stream into multiple streams...

How to print “\a” in c++ using codelite?

c++,iostream,cout
I was trying to print \a but it shows nothing so I searched for this and found out it should have made a sound but it didn't either. I am using code lite on Windows 8. How to print \a? Where is that sound? This is my code: #include <iostream>...

Why cout is producing no output on Code Blocks?

c++,codeblocks,iostream,cout
#include <iostream> using namespace std; int main(int argc, char** argv) { cout << "Whatever"; return 0; } Cout does not work, nor printf, nor puts, nor anything. Also, I've checked the project properties and both the debug and release are set to "Console Application" and to "Pause in the end"....

C++ ignore specific character while cin

c++,iostream
My input would be following: 4 5i 6 7i Instead of finding and replacing with string, I would like to store those individual values in double but with peeking and ignoring 'i' so double a = 4 , b = 5, c = 6 , d = 7 Following ugly...

Why I have to give a negative two offset for seekg for end of file

c++,iostream
So I was reading about file handling and wanted to read a text file from the end. So I decided to seek the get pointer to the last character using, seekg(-2,ios::end); My complete code is: fin.open("source.txt"); fin.seekg(-2,ios::end); fin>>ch; if(fin.fail()) cout<<"uh oh!"; else cout<<ch; My question is that why I have...

While loop uses the same value each time

c++,validation,input,while-loop,iostream
I have the problem with my code. There is while loop and when a enter not a number and for example character data, so then this loop uses the same value of choice each time. It's a list where user should choose one of variants. Choice it's a user's choice....

STL Map - Displaying what is pointed to by find() function

c++,dictionary,stl,iterator,iostream
For testing purposes, I'm running the following code through a for loop. Only the first three keys actually exist, and "Record found" is displayed as expected, along with the key, retrieved from findVertex->first. My question is, how would I be able to access the second value being pointed to? findVertex->secondseems...

Long double is printed incorrectly with iostreams on MinGW

c++,mingw,iostream
Consider the code #include <iostream> int main() { std::cout << 4.2L; } Compiling it on MinGW and running results in the following output: > g++ test.cc > a.exe -7.89773e-278 Is it a bug in MinGW and is there a fix or workaround? Update: There is a similar issue with printf...

Resetting output flags in C++

c++,iostream,flags,iomanip
I'm intending to reset all output flags to default on the lines where I end using the resetiosflags function. It provides erroneous output when I attempt to do it in this manner, contrary to my expectations. #include <iostream> #include <iomanip> using namespace std; int main() { bool first; int second;...

GCC, std::ctype specialisation & streams

c++,unicode,locale,iostream,facet
I've written my own specialisation of each virtual member function of std::ctype<char16_t>, so that this now works: #include <string> #include <locale> #include "char16_facets.h" // Header containing my ctype specialisation #include <sstream> #include <iostream> // Implemented elsewhere using iconv std::string Convert(std::basic_string<char16_t>); int main() { std::basic_string<char16_t> s("Hello, world."); std::basic_stringstream<char16_t> ss(s); ss.imbue(std::locale(ss.getloc(), new...

Read a very long console input in C++

c++,io,iostream,cin,getline
I'm trying to read a list of numbers (space delimited) from the console using std::cin. When the input line is longer than 1023 characters, the first "cin >> list[i]" in the following small working example never returns: using namespace std; int main() { vector<int> list(200,0); for(int i=0;i<200;i++){ cin >> list[i];...

C++ Calling - search function

c++,function,call,iostream
I was wondering how I could finish up this program. It's to perform a linear search on a list "ll" (which length is 31) for the user inputted item it, returning the user inputted numbers and their locations if they're found. Problem: I'm not sure how to call the functions...

How do I point to an input stream?

c++,pointers,iostream,ifstream,cin
Im trying to use a pointer to point to my desired input stream depending on user decision. This is what ive got so far. string fileName = "test.txt"; ifsteam = myFile; myFile.open(fileName.c_str(), ifstream::in); istream * myStream; if (file_mode) { myStream = &myFile; } else { myStream = &cin; } string...

GCC 4.8 and char16_t streams - bug?

c++,gcc,unicode,iostream,libstdc++
Is this a libstdc++ bug? #include <string> #include <sstream> using namespace std; int main() { basic_string<char16_t> str(u"0.0"); basic_stringstream<char16_t> sstr(str); double x = 9; sstr >> x; } Output, under GCC 4.8 Linux x86_64: $ ./main terminate called after throwing an instance of 'std::bad_cast' what(): std::bad_cast Aborted (core dumped) Edit Can...

Using struct as a return value of function crash program

c++,struct,iostream
I wnat to use struct as a return value of fucntion but it doesn't work and i dont know why. Program crash when i run this program. I got RTE. What is wrong with this code : #include <iostream> using namespace std; struct Tablica{ int T[201][201]; }; Tablica test(Tablica A,...

Unable to print CSV file

c++,csv,iostream
The following code compiles fine so there's no syntax errors. I am having trouble printing out the CSV file I read into this bit of code. I am not sure what is going on as this is a standard procedure, but would appreciate some input as to how to resolve...

Why is “Init” in std::ios_base::Init uppercase?

c++,design,naming-conventions,iostream,c++-standard-library
All the names in the standard C++ library are lowercase except std::ios_base::Init. Why is this?

How to clear the contents read by cin

c++,io,iostream,cin,flush
After typing an integer and pressing ENTER,the newline is getting stored. So when i do a char c = cin.get() , the previously entered newline is being assigned to variable c.The following loop gets skipped because of this..How to clear the contents of cin? Or how to avoid reading the...