stdio

Which I/O library do you use in your C++ code?

In new C++ code, I tend to use the C++ iostream library instead of the C stdio library. I've noticed some programmers seem to stick to stdio, insisting that it's more portable. Is this really the case? What do you use? ...

Why is fread reaching the EOF early?

I am writing a C library that reads a file into memory. It skips the first 54 bytes of the file (header) and then reads the remainder as data. I use fseek to determine the length of the file, and then use fread to read in the file. The loop runs once and then ends because the EOF is reached (no errors). At the end, bytesRead = 10624, ft...

C equivalent of autoflush?

In Perl, I can type: $|++; and anything printed to STDOUT will be automatically fflush()ed. Is there an equivalent in C? In other words, is there some way I can tell stdio to automatically fflush stdout after every printf(), the way it automatically flushes stderr? ...

What's the best way to return a random line in a text file using C?

What's the best way to return a random line in a text file using C? It has to use the standard I/O library (<stdio.h>) because it's for Nintendo DS homebrew. Clarifications: Using a header in the file to store the number of lines won't work for what I want to do. I want it to be as random as possible (the best being if each line has a...

How can you flush a write using a file descriptor?

It turns out this whole misunderstanding of the open() versus fopen() stems from a buggy I2C driver in the Linux 2.6.14 kernel on an ARM. Backporting a working bit bashed driver solved the root cause of the problem I was trying to address here. I'm trying to figure out an issue with a serial device driver in Linux (I2C). It appear...

Is close/fclose on stdin guaranteed to be correct?

It seems as though the following calls do what you'd expect (close the stream and not allow any further input - anything waiting for input on the stream returns error), but is it guaranteed to be correct across all compilers/platforms? close(fileno(stdin)); fclose(stdin); ...

Redirect stout from child process with .net

I'm using the following code System::Diagnostics::Process^ p = gcnew System::Diagnostics::Process(); p->StartInfo->FileName = "tnccmd.exe"; p->StartInfo->UseShellExecute = false; p->StartInfo->RedirectStandardInput = true; p->StartInfo->RedirectStandardOutput = true; p->Start(); System::IO::StreamWriter^ tnc_stdin = p->StandardInput; ...

issue with FILE

I'm getting the folowing error for below code, "1506-221 (S) Initializer must be a valid constant expression" FILE *fp[] = {stdout, dump_f}; is this acceptable? what is the proper way to achive this? ...

C++ stdio::rename(); synchronous?

I was just wandering if stdio::rename() function call is fully synchronous. So is the file immediately after return from the function call available under new name or it might take some time (some miliseconds) until this happens? I am investigating an irritating timing bug and suspect that the latter case happens. My software runs on Wi...

Ignoring line breaks in C

I'm trying to write some code that reads a file and ignores the line breaks (\n), so far I have: c = fgetc(fp); for(int loop = 0; c != EOF; loop++) { if((c != '\n') && (c != '\\')) { buffer[loop] = c; } c = fgetc(fp); } but its just not seeming to ignore the '\n' bits (not sure about the '\') And sorry for the la...

What is a good programming pattern for handling return values from stdio file writing functions

I'm working on some code that generates a lot of ignoring return value of ‘size_t fwrite(const void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result warnings when compiled with g++, and I'm wondering about the best programming pattern to actually record and handle the return value of a large number of separate se...

Rerouting stdin and stdout from C

I want to reopen the stdin and stdout (and perhaps stderr while I'm at it) filehandles, so that future calls to printf() or putchar() or puts() will go to a file, and future calls to getc() and such will come from a file. 1) I don't want to permanently lose standard input/output/error. I may want to reuse them later in the program. 2) ...

supressing blankspace in input through scanf

to supress newline we use %[^\n].can you suggest what should be the format to skip blank in the input,ie if we have to input "hallo stackflow".i know fgets and gets but i dont want to use them ,they are creating problem. ...

How to get the underlying stdio FILE* of a managed System.IO.FileStream?

I'm writing a .NET adaptor for a C/C++ library where a method "bar" takes a regular stdio FILE*. Is it possible to build an interface so that managed code user can pass a managed (File)Stream? That is without creating an intermediary buffer and code to pipe the data between. Also does the assumption that bar() reads only make things any ...

Determining the (opened) filename from a FILE *

Given a stdio FILE * pointer, is there a method by which I can discover the name of the (opened) file? ...

Tried and true simple file copying code in C?

Hello! This looks like a simple question, but I didn't find anything similar here. Since there is no file copy function in C, we have to implement file copying ourselves, but I don't like reinventing the wheel even for trivial stuff like that, so I'd like to ask the cloud: What code would you recommend for file copying using fopen()/f...

C++: Correct order for including both <cstdio> and <stdio.h>?

I need to use system-specific functions, e.g. ftello() (defined in stdio.h as per POSIX standard). I also need to use standard C++ features, e.g. std::sprintf() (defined in cstdio, as per ISO C++ standard). AFAIK, including only <cstdio> doesn't guarantee defining non-standard-C++ stuff, so I guess I have to include both. I've read a lo...

for what purpose are there $deferr, $defout and why there is no $defin

For what purpose are there $deferr, $defout and why there is no $defin if there are $stderr, $stdout and $stdin, and also STDIN, STDOUT and STDERR Changing any of them will not change others ...

I'm getting compile errors in the standard library. What's up?

I'm trying to compile an unspecified piece of software, and I'm getting errors in the standard headers such as stdio.h. The errors are mostly undeclared identifiers such as _In_. IntelliSense finds their definitions just fine. In what general direction should I look for the cause of this? Added: For example, in one cpp file stdio.h is t...

How to get a pointer to the beginning of a file in C++

Hi Is it possible in C++ to somehow get a pointer to the beginning of an opened file so that it ( the pointer ) can be passed to the unix write function together with the size of the file? Just to be clear: I want to pass the whole file to a write-like function - how do I do this? ...