Questions tagged [cstdio]

A C++ wrapper for C standard IO header stdio.h.

42 questions
30
votes
5 answers

cstdio streams vs iostream streams?

I just learned of the existence of the ios_base::sync_with_stdio function, which basically allows you to turn off (or on if you already turned it off) the synchronization between iostream streams that are used in C++ and the cstdio streams that are…
Tony The Lion
  • 57,181
  • 57
  • 223
  • 390
7
votes
5 answers

Skipping expected characters like scanf() with cin

How to achieve scanf("%d # %d",&a,&b);sort of effect with cin in C++ ?
eldos
  • 2,831
  • 3
  • 25
  • 30
5
votes
0 answers

Is it possible to make C++ iostream std::cout be as performant as cstdio printf()?

Note: This is not a duplicate of existing std::ios::sync_with_stdio(false) questions. I have gone through all of them and yet I am unable to make cout behave as fast as printf. Example code and evidence shown below. I have three source code…
Lone Learner
  • 12,094
  • 14
  • 66
  • 159
5
votes
1 answer

difference between << s.str() and << s.rdbuf()

Can someone explain the subtle difference in: ofstream f("test.txt") std::stringstream s; s<<""; f << s.rdbuf(); f.good() // filestream is bad!! ofstream f("test.txt") std::stringstream s; s<<""; f << s.str(); f.good() // is still ok! I mostly…
Gabriel
  • 7,477
  • 5
  • 47
  • 87
3
votes
1 answer

How do I intentionally trigger an error in fgets()?

Summary When using fgets(), I have error checking code that performs some remediation in case fgets() returns null but isn't yet at end of file. I'd like to exercise this section of code to verify it's working as intended. Is there a canonical way…
JohnFilleau
  • 3,191
  • 1
  • 13
  • 21
3
votes
1 answer

How can I separate the stdin/stout/stderr streams of a java application executed in C++

I'm creating a C++ wrapper for an existing jarfile. In this case, I'm doing this with the Spigot Minecraft server jarfile. When I execute the application, I have the issue where the input and the output of the application is dominated by the Java…
humroben
  • 77
  • 7
2
votes
1 answer

Is there a guaranteed order of assignment in scanf?

I came across some code and was wondering if it was just a fluke that it worked as expected or was just bad practice. Consider the following MCVE (ideone): #include struct dummyStruct { unsigned short min[4]; unsigned short…
Avi Ginsburg
  • 9,517
  • 3
  • 24
  • 50
2
votes
0 answers

cstdio and cstddef conflict on std::size_t

test.cpp #include #include using std::size_t; To compile: > g++ -c test.cpp -o test.o In file included from /.../include/stdio.h:75:0, from /.../gcc/include/c++/cstdio:42, from…
Panwen Wang
  • 1,889
  • 11
  • 25
2
votes
5 answers

How to avoid dangerous vsprintf when you don't know the buffer size

__inline int my_sprintf (char *dest,char *format,...) { va_list va; va_start(va,format); return vsprintf(dest,format,va); } My issue is that I can't add the buffer size parameter to my_sprintf because it's used in more than 50k places,…
user327843
  • 442
  • 6
  • 17
2
votes
3 answers

C++ I/O library

I tried googling this but I get different answers at different places. I want to know the cases wherein one should use one of the following: #include #include #include I cannot figure out the difference since in my case…
ibp73
  • 632
  • 6
  • 14
1
vote
0 answers

Retrieve information about an open file

Can I retrieve information about a file previously opened with fopen() using only the pointer it returned? The reason I ask is that I am trying to write a RAII-style wrapper class for FILE *s, and I want to make it as general as possible, and one of…
Paulo1205
  • 877
  • 4
  • 9
1
vote
3 answers

Why sscanf can't read an uint64_t and a char from one string?

#include #include #include int main() { std::uint64_t ui; char c; auto ret = std::sscanf("111K", "%lu64%[B, K, M, G]", &ui, &c); assert(ret == 2); assert(ui == 111); } I tried to use sscanf to read…
JiaHao Xu
  • 1,556
  • 8
  • 21
1
vote
1 answer

Creating a custom iterator struct to work with cstdio

I'm trying to create a iterator to go through my file. My file is binary and have int values inside, so in my point of view, it should work like that. But I'm getting errors says "invalid use of data-member 'IntFile::file' "So i marked in code…
1
vote
0 answers

Eclipse CDT Debug Has Extra Lines In Scanf

I have a simple piece of code that will echo whatever you type into the console: int main(){ setvbuf(stdout, NULL, _IONBF, 0); char c = -1; while(scanf("%c", &c) == 1){ printf("%c", c); } } But when I Debug this code in…
ModDL
  • 360
  • 2
  • 13
1
vote
1 answer

Locate the source of a bug with sscanf

I've been struggling with this for too long. Let's say i have this minimal code: test.cxx #include #include int main (int argc, char *argv[]) { const char *text = "1.01 foo"; float value = 0; char other[8]; int code…
neok
  • 802
  • 8
  • 14
1
2 3