711 questions
1
vote
1
answer
86
views
Read-and-validate istream equivalent to fscanf
I have a very simple scenario that looks like
std::ifstream file("myfile.txt");
std::string discard;
int num;
file >> discard >> num; // to consume e.g. HEADER 55
Despite all ...
2
votes
1
answer
62
views
std::istreambuf_iterator equality when using seekg
I am puzzled by the output of the code below:
std::string content = "abcdefghijklmnopqrstuvwxyz1234567890";
std::istringstream iss(content);
iss.seekg(10);
std::istreambuf_iterator<char&...
0
votes
0
answers
121
views
How I can "convert" IStorage to a byte array or to an IStream?
I have implemented a drag-and-drop receiver (C++, WINAPI). It allows me to get a storage with a dragged object (OLE IStorage object). I need to "convert" it to a byte array (something like a ...
1
vote
1
answer
112
views
Validating istream with std::ios_base::iostate
I have to write a stream extraction operator>> function which checks if the istream variable (stream) in the parameter is valid before returning it along with a delimiter (which is already ...
1
vote
0
answers
48
views
Why is EOF triggering on the last word instead of after trying to reget input?
So currently working on some exercises on I/O, my code basically takes a string from input and removes the vowels at input:
ExerciseClass.cpp:
NoVowelString& NoVowelString::operator>>(std::...
1
vote
2
answers
99
views
C++ - Why does the cin inside the if statement get skipped over when it is under a while loop reading an unknown number of inputs?
My code skipped over the cin under the while loop reading an unknown number of inputs.
I wanted to run the code
int curval = 0, val = 0;
while(std::cin >> curval){
val++;
}
...
0
votes
0
answers
77
views
Is it possible to pass std::cin to a function wanting an iostream?
I am using a library that has a function taking an iostream for a data stream, but I want to pass std::cin. It apparently is not possible to pass an istream to an iostream, but is there way around ...
-1
votes
2
answers
120
views
Can an std::istream be split without specifying the number of delimiters?
I have the separate_by_delimiter function which is used inside another function outside_func
std::vector<std::string>
separate_by_delimiter(std::istream &input, int num_separators, char ...
0
votes
1
answer
87
views
Is there a unget() method for putting back everything extracted by std::getline back into an istream?
I am extracting each full row of a csv file using std::getline:
for (std::string row; std::getline(is, row); ) // reads entire row in csv file
{
if (std::ranges::count(row, ',') != Temp_reading::...
1
vote
2
answers
127
views
Why does `std::istream::seekg()` affect the behavior of `std::istreambuf_iterator`?
Why does std::istream::seekg() affect the behavior of std::istreambuf_iterator?
I carefully read the introduction about std::istreambuf_iterator on cppreference, but there is no direct answer for this ...
0
votes
2
answers
91
views
No output from class method
The code:
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
class Point{
double m_x,m_y;
public:
Point(double x=0,double y=0):m_x(x),m_y(y){}
Point ...
1
vote
0
answers
42
views
istreambuf_iterator potential null pointer deref [duplicate]
In the following code:
std::string fn()
{
std::ifstream ifs {"bob", std::ios::in | std::ios::binary};
auto beg = std::istreambuf_iterator<char>(ifs);
auto end = std::...
0
votes
1
answer
54
views
How to read a file that is given from NodeJS to a c++ process using stdin?
I created a C++ program that uses a method of a library. This method needs an istream to work.
The C++ compiled program is executed using child_process module of NodeJS and the file is sent to the c++ ...
0
votes
0
answers
25
views
"no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'Diem2D')"
enter image description here
i tried to used operator istream and ostream but it doesn't work. I'm writing a HinhTron class from the existing Diem2D class to use point coordinates from the 2D class. ...
0
votes
1
answer
114
views
Template default argument missing with basic_istream class
I was checking the implementation of the basic_istream class. I found the implementation at https://gcc.gnu.org/onlinedocs/gcc-13.2.0/libstdc++/api/a00113_source.html#l00095.
Let me add a snippet.
I ...
3
votes
2
answers
213
views
why is std::views::istream not exhausted with take_while
I was trying to get all words (sequence of non whitespace characters) out of a file. In trying to do so, I accidently created an infinite loop, because at the end of the file, no more word is ...
0
votes
0
answers
202
views
Buffer-based stringstream without copying [duplicate]
Suppose I have two APIs that I need to interact with:
API 1 is a producer which provides me with a raw character buffer (const char*) and a length (say std::size_t).
API 2 is a consumer which expects ...
4
votes
1
answer
293
views
Why can't I use lazy_split_view with a istream view instead of a string_view?
Consider this code snippet (adapted from cppreference.com):
constexpr std::string_view text{"Hello-+-C++-+-23-+-!"};
constexpr std::string_view delim{"-+-"};
std::ranges::for_each(...
-2
votes
3
answers
125
views
The problem is writing operator overloading << and >> using templates
I have a class of shapes, it works on the principle that a shape consists of points (x and y coordinates). But overloading the input and output operators I encountered a problem that could serve as an ...
1
vote
1
answer
61
views
Making an std::istream produce one extra character pass its end
I'm writing an adapter between different libraries in C++20. I reached a part where I get a std::istream from one library and pass it to the second library, which also takes std::istream. The problem ...
3
votes
0
answers
221
views
Creating icon stream from resource file?
Win32 MFC
I can load an icon stream (to use with WebView2) from a physical ICO file on my hard drive. Eg:
wil::com_ptr<IStream> iconStreamPdfSettings;
CHECK_FAILURE(SHCreateStreamOnFileEx(
L&...
2
votes
0
answers
81
views
Reading a number when no input given in C++
Is the following code prone to undefined behavior when end of file or an empty line is provided as input?
unsigned long long i;
cin >> i;
From what I've observed using Compiler Explorer, it ...
1
vote
1
answer
343
views
Cannot make std::unique_ptr<std::istream> hold a pointer to std::ifstream
Consider the following piece of code (out of context for the sake of simplicity):
std::unique_ptr<std::istream> stream;
stream = std::make_unique<std::ifstream>(path, std::ios::in | std::...
0
votes
2
answers
258
views
Reading Floats from a txt file in C++
I am trying to read (x,y) floating point values from a txt file in C++. The numbers are separated by a space. The ith number and the i+1th number make the (x,y) coordinates. So index positions 0 and 1 ...
0
votes
1
answer
80
views
Std::istream& as function parameter
What is the best way to make a program that reads the elements from the input until the EOF and returns the container of read elements? This is what I have to do
p.h file:
#pragma once
#include <...
2
votes
0
answers
41
views
How to input a stream that will ignore numbers and treat all symbols as delimiters
I have a binary tree that takes in a stream of words and adds them each to a new node of the binary tree, skipping duplicates.
I tried implementing an input method like so:
void WordTree::input(std::...
0
votes
0
answers
29
views
I have a while loop for stringstream and it's giving me the last word twice. Why is this happening? [duplicate]
My program takes in a student's last name, then first name, then midterm score 1, then midterm score 2, and lastly their final score. Based on the average of their midterm scores and their final score ...
3
votes
3
answers
127
views
stringstream operator>> fails to assign a number in debug
I have this simple function that, given a string str, if it is a number then return 'true' and overwrite the reference input num.
template <typename T>
bool toNumber(string str, T& num)
{
...
0
votes
0
answers
410
views
std::vector string whitespace manipulation
Given a string of which the content could be "this is a test string jump fox string".
There are 2 things to note from this string - there is one space between every word, but also at ...
-1
votes
1
answer
84
views
Operator>> overloading: "cannot bind 'std::istream {aka std::basic_istream<char>}' lvalue to 'std::basic_istream<char>&&'" [closed]
Here's my fraction class:
class fraction { // type definition
int num;
int denom;
ostringstream sstr;
public:
fraction(int c=0, int d=1) :
num(c), denom(d)
{ sstr = ...
-1
votes
2
answers
545
views
How to read from a binary file and load the data into a 3d vector in C++?
I currently have some .bin files, each of which contains a matrix of size AxBxC. I would like to load it into a 3d vector vector<vector<vector<float>>> in C++, but encountered some ...
1
vote
1
answer
97
views
Correct syntax for operator >> overloading to members of nested class?
I have class Address nested in class Student, and I want to feed each input line into the class Student with operator>> overloading through istream.
class Address {
public:
....
private:
...
16
votes
2
answers
3k
views
Input from an istream to a char* pointer?
I am reading Bjarne Stroustrup's "Programming Principles and Practice Using C++" (second edition). On page 660-661, the writers define a function as follows:
istream& read_word(istream&...
1
vote
2
answers
221
views
Problem with std::getline() and std::cin.get() [duplicate]
can you help me
Why this code can't be swap
cout << "Enter a string: ";
getline(cin, str1);
cout << "Enter another string: ";
cin.get(str, 100, '\n');
...
1
vote
1
answer
257
views
How to print icmp packet (including all headers) in hex from istream in boost-asio example
I am trying to modify the ICMP boost_asio implementation to print ICMP response and request packets. In the example code I am trying to print the response/reply buffer using below code:
void ...
2
votes
2
answers
1k
views
Is there a way to make a smart pointer that points to either a std::ifstream or std::cin?
I want to take input from an istream from either a std::ifstream or std::cin depending on some condition.
As far as I can get it working, I had to use a raw std::istream* pointer:
int main(int argc, ...
0
votes
1
answer
640
views
Writing a custom input manipulator
I need to make a custom istream manipulator that reads 5 characters from input, then skip 5 characters from input, and does it to the end of the string. Example:
string line;
cin >> skipchar >...
1
vote
1
answer
399
views
istringstream skip next (n) word(s)
Is there a propper way in an isstrinstream to skip/ ignore the next, or even the next n words?
The possibility to read n times a variable seems to work, but is very clunky:
for (int i = 0; i < n; ++...
1
vote
1
answer
125
views
Why do C++ istreams only allow formatted-reading into an existing variable?
In C++ (and using the standard library facilities), if I want to read an integer from the standard input, I need to do this:
int x;
std::cin >> x;
And looking at the std::istream page on ...
2
votes
1
answer
748
views
C++ Operator overloading error: no match for ‘operator>>’ (operand types are ‘float’ and ‘Class’)
I'm working on some OOP code and was trying to setup a istream& with the operator >>. But with the code that I have, I keep getting an error, like the one stated above in the title. The code ...
0
votes
1
answer
59
views
How to do is >> std::skipws >> through multiple indices of an array?
Let's say you have std::array<int, SIZE> a, and you have saved each element of a into a file in one line separated by a space. Then you want to read them with a std:istream& is via:
is >&...
0
votes
0
answers
239
views
Passing istream and string as functions parameters
I have the following code. What I'm trying to do is implement a word count function similar to wc in unix. It takes a file as input and gives back the number of lines, words and characters in that ...
0
votes
1
answer
135
views
Whenever i use while(cin>>(var)) in my code, the istream gets stuck in error state
The code i am trying to run is given below. Here in line 9 I am trying to take multiple inputs using the while(cin>>n) method. The input I gave is like :
2 4 5 6 45 357 3 (ctrl+z)(ctrl+z)
to ...
1
vote
1
answer
454
views
How do i convert String to IStream And Vice Versa, in Delphi?
i want to use :
Rslt := WrapCompressedRTFStream(OleStream, 0, Uncompressed);
where : OleStream, Uncompressed : IStream;
and RTFStringIn RTFStringOut are String;
i have the RTFStringIn (As String).
...
0
votes
0
answers
78
views
Is this elegant way to open stdin or a file legal?
I've recently had to implement the common pattern of reading from a file or, given a filename of "-", from stdin. There is an abundance of solutions on SO, but I found mine to be shorter.
...
-1
votes
3
answers
3k
views
How to prevent the user from inputing a string in c++
Hello I'm relatively new to operator overloading, but I do believe its the answer to a problem i've been facing in almost every program I make. My goal is to overload std::cin >> int_var so ...
-2
votes
1
answer
116
views
std::cin , unwanted behaviour. and how can I fix this?
I tried to play with some code to test overloading functions. The overloading part went well, however, I learned something about std::cin that made me feel stupid for not noticing it before!
#include &...
0
votes
2
answers
8k
views
C++ std::istream to read from file
I am completely new to the c++ and I have problem with managing input. I have a program where I want to be possible to read input from console but also from the file.
I have class with private field ...
0
votes
2
answers
157
views
How to call the function that accepts std::istream&
I am a beginner in C++. What is the correct way to call a function that expects std::istream&?
Tried it with read(std::cin);, but I get an error from the compiler.
typedef double Element;
...
-1
votes
1
answer
27
views
Problem extracting formatted input from an istringstream that has been set twice
Thanks for the help.
My program reads lines from stdin. The first one has a single number which determines the mode at which the program is running, and the rest contain sequences of numbers of ...