Questions tagged [cstdio]

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

55 questions
34
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
  • 61,704
  • 67
  • 242
  • 415
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
  • 3,132
  • 3
  • 29
  • 32
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
  • 18,088
  • 20
  • 102
  • 200
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
  • 8,990
  • 6
  • 57
  • 101
4
votes
6 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
  • 482
  • 6
  • 17
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
  • 4,045
  • 1
  • 15
  • 22
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
  • 87
  • 9
2
votes
2 answers

Compiler warning on mix of "stdio.h" and

The following program results in compiler warnings if the include statement in line 4 is added (uncommented). Compiler: gcc version 8.1.0 (i686-win32-dwarf-rev0, Built by MinGW-W64 project) #define __STDC_FORMAT_MACROS // Adding this and -Wall…
jgreen81
  • 705
  • 1
  • 6
  • 14
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
  • 10,323
  • 3
  • 29
  • 56
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
  • 3,573
  • 1
  • 18
  • 39
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
  • 650
  • 1
  • 6
  • 16
1
vote
1 answer

C getline() function: return value vs second argument

Do the return value (type ssize_t) of the C getline function and the second argument *n (type size_t) contain the same information, after invocation? Empirically, it seems that *n equals (size_t)pow(2, ceil(log2( + 1))). Is this…
fmg
  • 813
  • 8
  • 18
1
vote
2 answers

Wrap cstdio print function with C++ variadic template

I'm writing a lightweight parsing library for embedded systems and try to avoid iostream. What I want to do is write variables to a buffer like vsnprintf() but I don't want to specify the format string, much rather the format string should be…
glades
  • 3,778
  • 1
  • 12
  • 34
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
  • 918
  • 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
  • 2,452
  • 16
  • 31
1
2 3 4