iostream

fatal error C1075: end of file found before the left brace and read and write files not working

安稳与你 提交于 2019-12-24 09:05:09
问题 could someone also tell me if the actions i am attempting to do which are stated in the comments are correct or not. i am new at c++ and i think its correct but i have doubts #include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads itemlist.txt ofstream out_stream1; // writes in items.txt ifstream in_stream2; // reads pricelist.txt ofstream out_stream3;// writes in plist.txt ifstream in_stream4;// read recipt.txt

How to read a fixed number of bytes from a c++ std::istream

三世轮回 提交于 2019-12-24 08:10:55
问题 How to read a fixed number of bytes from a std::istream without doing any extraction? For example, I have a variable sz of type size_t and I would like to read sizeof(size_t) bytes from the istream . void foo(std::istream& is) { if(is.rdbuf()->in_avail() < sizeof(size_t)) return; // how to read to sz from istream is without extraction (advancing pointers) size_t sz; } 回答1: You can only peek the next character without extracting. As such, you should change your strategy: instead of trying to

convert std::ostream to some array?

廉价感情. 提交于 2019-12-24 03:44:08
问题 I have a legacy library that takes data from hardware and writes it to ostream . The method looks like following : int sensors(ostream*) const; I am not skilled enough in Ancient Ways. How to convert this data to QByteArray ? Or, at least, to char array of known size? I would have solved it myself, but there is an additional problem: the data in ostream seem to be arbitrary length and have several arbitrary '\0' symbols, so you can't count on it being null-terminated. 回答1: I think this is

C++ currency output

时间秒杀一切 提交于 2019-12-24 02:43:29
问题 I am taking a C++ course right now and have completed my final assignment. However there is one thing that is bugging me: Though I have the correct outputs for the testing on a particular output, the basepay should be 133.20 and it is displaying as 133.2 . Is there a way to have this display the extra 0 rather than leaving it off? Anyone know if it's possible and how to do it? Thank you in advance My code is below: cout<< "Base Pay .................. = " << basepay << endl; cout<< "Hours in

Convert InputStream into FileInputStream

夙愿已清 提交于 2019-12-23 20:23:36
问题 I have read this post How to convert InputStream to FileInputStream on converting a InputStream into a FileInputStream. However, the answer does not work if you are using a resource that is in a jar file. Is there another way to do it. I need to do this to get the FileChannel from a call to Object.class.getResourceAsStream(resourceName); 回答1: You can't, without basically writing to a file. Unless there's a file, there can't be a FileInputStream or a FileChannel . If at all possible, make sure

cin >> val sometimes reads 0 depending on Ctrl-Z

强颜欢笑 提交于 2019-12-23 20:22:54
问题 I was trying to write a code in C++ in Windows using MinGW compiler, my code counts and prints the number of consecutive times a number occurs in a given set of input. The code is as follows: #include <iostream> int main() { int c_val = 0,val = 0,cnt = 1; std::cin>>c_val; while(std::cin>>val){ if(val==c_val) cnt++; else{ std::cout<<c_val<<" occurs "<<cnt<< " times"<<std::endl; c_val = val; cnt = 1; } } std::cout<<val<<" occurs "<<cnt<<" times"; } INPUT: 42 42 42 12 13 13 ^Z (press Enter)

c++ linker , how to link the iostream file?

一世执手 提交于 2019-12-23 20:14:52
问题 I have a file named main.cpp which includes iostream . I compiled main.cpp and it worked without errors, so my question is: I compiled main.cpp and I did not link iostream with main.cpp , so how could this be possible? Or did the compiler linked the iostream automatically? 回答1: The functions in iostream are part of the C++ standard library, which you usually don't need to link explicitly. If you use a compiler that's not strictly a C++ compiler, you sometimes need to add something like -lstdc

Strange std::cout behaviour with const char*

大憨熊 提交于 2019-12-23 18:55:45
问题 I have a method which returns a string to display as an error message. Depending on where this error occurs in the program, I might add a bit more of an explanation to the error message before displaying it. string errorMessage() { return "this is an error"; } // somewhere in the program... const char* message = ("Some extra info \n" + errorMessage()).c_str(); cout << message << endl; (I am storing the message as a const char* since I will actually provide this error to another method which

How to format my own objects when using STL streams?

风流意气都作罢 提交于 2019-12-23 17:32:05
问题 I want to output my own object to a STL stream but with customized formatting. I came up with something like this but since I never used locale and imbue before I have no idea if this makes sense and how to implement MyFacet and operator<<. So my questions are: does this make sense and how to implement MyFacet and operator<< ? The following is a simplified example which shows you what I want to do. struct MyObject { int i; std::string s; }; std::ostream &operator<<(std::ostream &os, const

Python : communication with c++ command line program not working when using <cstdio>

元气小坏坏 提交于 2019-12-23 16:17:24
问题 I have the following python code, which is supposed to provide the intial input to a C++ program, then take its output and feed it back into it, until the program finishes execution: comm.py p = subprocess.Popen('test__1.exe', bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=False) p.stdin.flush() p.stdout.flush() x = b'1\n' while True: p.stdin.write(x) p.stdin.flush() p.stdout.flush() x = p.stdout.readline() print(x) if p.poll() != None: