iostream

Why am I having this error message from g++?

被刻印的时光 ゝ 提交于 2019-12-11 08:34:02
问题 Below is my code. It compiles fine in g++, but there is always this running time error: Segmentation fault (core dumped) Where am I wrong? #include<iostream> #include<string> using namespace std; void sort_string(string x){ for (int i=0;x.size();i++){ for(int j=i;x.size();j++){ char temp = x[i]; if (temp > x[j]){ x[j]=temp; x[i]=x[j]; } } } } int main(){ string words; cin >> words; while (words != " "){ cout << words << " "; sort_string(words); cout << words << endl; } } 回答1: You are looping

Output and Input at Same Line using IOSTREAM

微笑、不失礼 提交于 2019-12-11 08:04:52
问题 I want to use the iostream input and output operators in the same statement, not to be nicer to the user but the look I was trying not successfully obtained. Code fragment: int value = 0; std::cout << "Number 1: " << std::cin >> value << std::endl; Is there any way to do this using only cin cout? 回答1: struct IO { template <typename T> const IO & operator << (const T & t) const { std :: cout << t; return *this; } template <typename T> const IO & operator >> (T & t) const { std :: cin >> t;

seekp/g and tellp/g does not work properly

这一生的挚爱 提交于 2019-12-11 07:47:01
问题 Whenever I try to use tellp/g and seekp/g functions on fstream object, and use the fstream::getline(cstr, bufsize) function, it just don't work properly and don't store anything in 'cstr' variable. and it does not report any problem which invalidate the stream like failbit or badbit Sample code: what it does is read it line by line and on each read it jumps to the end of the file and print the next line's offset from the beginning of the file. int main() { // Prepare file fstream f("test.txt"

sh_none is not a member of 'std::basic_filebuf<_Elem,_Traits>'

你。 提交于 2019-12-11 07:17:15
问题 I am trying to use the Microsoft Specific filebuf::sh_none variable to open an fstream in an exclusive mode (another Microsoft specific function). I am getting the above error. I am importing <fstream> . How can I fix this error? UPDATE : I found this thread, where it was suggested that I try _SH_DENYNO instead. The code compiles, I am testing it now. This doesn't make any sense to me, as this isn't documented in the function. 回答1: What version of Visual C++ are you using? The Visual C++ 6

I/O Stream String Manipulation (Correct Cin/cout operators <</>>)

99封情书 提交于 2019-12-11 06:56:22
问题 I came across this question on here and had a further question about the answer give (I can't comment since I'm new to stackoverflow). The guy who answered it seems to be right about replacing << >> for cin and cout . But the problem I'm having is that all the semi-colons won't show up in the new output file. I know that while std::getline(input, command, ';') erases all semicolons, but theyre supposed to be put back with the else statement at the end, but it isn't being replaced when I run

C++ iostream >> operator behaves differently than get() unsigned char

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 06:45:24
问题 I was working on a piece of code to do some compression, and I wrote a bitstream class. My bitstream class kept track of the current bit we are reading and the current byte (unsigned char). I noticed that reading the next unsigned character from the file was done differently if I used the >> operator vs get() method in the istream class. I was just curious why I was getting different results? ex: this->m_inputFileStream.open(inputFile, std::ifstream::binary); unsigned char currentByte; this-

Why #define is bad? [duplicate]

安稳与你 提交于 2019-12-11 06:24:37
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When are C++ macros beneficial? Why is #define bad and what is the proper substitute? Someone has told me that #define is bad. Well, I honestly don't not understand why its bad. If its bad, then what other way can I do this then? #include <iostream> #define stop() cin.ignore(numeric_limits<streamsize>::max(), '\n'); 回答1: #define is not inherently bad . However, there are usually better ways of doing what you

transparent access to files on Samba shares on Linux et al. using C++ IO streams

狂风中的少年 提交于 2019-12-11 05:23:40
问题 Is it possible to open files on Samba shares using C++ IO streams in Linux as transparently as on Windows using just UNC path (or similar) or do I always need some kind of 3rd party library? If a 3rd party library is the only solution, what 3rd party libraries are there for Samba shares access? 回答1: It looks like you just want to mount the remote server using cifsfs (see "man mount.cifs"), and then access the files from the mount point you specified. There is no way that you can explicitly

Why cout is producing no output on Code Blocks?

﹥>﹥吖頭↗ 提交于 2019-12-11 04:01:47
问题 #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". It makes no sense. Edit: Also, I've tried to flush with endl before (No results). 回答1: I am writing my comment as a response: Your image shows option "-mwindows", that removes the console, as said at

Using buffered streams for sending objects?

℡╲_俬逩灬. 提交于 2019-12-11 03:59:44
问题 I'm currently using Java sockets in a client-server application with OutputStream and not BufferedOutputStream (and the same for input streams). The client and server exchanges serialized objects (writeObject() method). Does it make sense (more speed) to use BufferedOutputStream and BufferedInputStream in this case? And when I have to flush or should I not write a flush() statement? 回答1: Does it make sense (more speed) to use BufferedOutputStream and BufferedInputStream in this case? Actually