stdio

Is it ‘safe’ to remove() open file?

I think about adding possibility of using same the filename for both input and output file to my program, so that it will replace the input file. As the processed file may be quite large, I think that best solution would to be first open the file, then remove it and create a new one, i.e. like that: /* input == output in this case */ F...

Getting another program's output as input on the fly

I've two programs I'm using in this way: $ c_program | python_program.py c_program prints something using printf() and python_program.py reads using sys.stdin.readline() I'd like to make the python_program.py process c_program's output as it prints, immediately, so that it can print its own current output. Unfortunately python_prog...

Detecting background operation

In C, what is the way to detect a program was called in "background mode" ? I have a program I would like to launch either interactively or in background. How can I detect I should not be reading from stdin and end in a "Stopped : tty input" state ? Should I test that stdin is closed ? How can I do that ? Edit : isatty seems like a go...

getline over a socket

Is there a libc function that would do the same thing as getline, but would work with a connected socket instead of a FILE * stream ? A workaround would be to call fdopen on a socket. What are things that should be taken care of, when doing so. What are reasons to do it/ not do it. One obvious reason to do it is to call getline and co,...

Close a FILE pointer without closing the underlying file descriptor

By using fdopen(), fileno() it's possible to open streams with existing file descriptors. However the proper way to close a file, once you've opened it with a stream is to fclose() the FILE pointer. How can one close the stream, but retain the open file descriptor? This behaviour is akin to calling fflush() and then fileno(), and then n...

char *a, *b; what type is (b-a) and how do I printf it?

{ char *a, *b; printf("%lx\n",(b-a)); } Usually works, in fact, I can't imagine it giving a warning or failing on a 32-bit or 64-bit machine. But is that the proper thing to do for ANSI C and size awareness? I'd like this code to work on every platform possible, including non-Unixes and embedded systems. ...

freopen: reverting back to original stream

Hello, I needed to forward stdout to different files to separate some prints produced and the reverting back to normal stdout. I used freopen to switch to the file in this way: char name[80]; memset(name, 0, 80); strcpy(name, "./scripts/asm/"); strcat(name, m_func->m_name->m_value); strcat(name, ".shasm"); freopen(name, "w", stdout); ...

fclose()/pclose() may block on some file pointers

Calling fclose() here after dup()ing its file descriptor blocks until the child process has ended (presumably because the stream has ended). FILE *f = popen("./output", "r"); int d = dup(fileno(f)); fclose(f); However by manually performing the pipe(), fork(), execvp() of the popen(), and then dup()ing the pipe's read file descriptor,...

opening a file with fopen

I've been trying to open a file and output text, but I keep getting errors. So I thought I would start at the very beginning and just try opening the file. This is my code: #include <stdio.h> #include <stdlib.h> #define CORRECT_PARAMETERS 3 int main(void) { FILE *file; file = fopen("TestFile1.txt", "r"); if (file == NULL)...

Implementing a File Object (C++)

I've been looking over the Doom 3 SDK code, specifically their File System implementation. The system works (the code I have access to at least) by passing around an 'idFile' object and I've noticed that this class provides read and write methods as well as maintaining a FILE* member. This suggests to me that either the FILE* is 'o...

Benefits of opening a file for read and write

Can anyone point me to some discussion covering the pro's and con's of opening a file for read and write as opposed to (for example) opening a file for read, closing it and then reopening for write. I've tried searching for in-depth information without joy. Many thanks ...

include stdio makefile

Im trying to use the sprintf() function. Therefore I has to include the stdio.h in my C project. If I compile the project without including the stdio.h in my makefile, the compiler generates the error that sprintf() is a unknown function. Including the stdio.h to the makefile generates the error that there is "no rule to make target." ...

Ruby stdio consts and globals, what are the uses?

Ruby has constants and global variables for stdio. Namely, the consts STDIN, STDOUT, STDERR, and their variable counterparts, $stdin, $stdout, $stderr. I understand the difference between a constant and a variable. I know the constants are immutably set to the file descriptors at the moment the script was exec'd. I also understand tha...

forcing a program to flush its standard output when redirected

i have a closed source program that prints output to standard output. i need to parse the output. so i redirect the output to a fifo (from which i can read in the parent process that forks and execs the binary) using dup2 and then exec the program. the problem is that the fprintf calls in the file become buffered because it is now writin...

fgets() linux vs mac

Does fgets on a mac read until a carriage return '\r' or does it also depend on the new line '\n'? Reason is I am using fgets to read a file a line at a time. However if it is run on a mac file with only '\r' for the line ending it fails to do what I want. (run in linux) I don't want to be writing library type functions to deal with cr...

how to stop reading from file in C

hi I am new to C. I am just trying to read each character of the file and print it out but when the file finishes reading, but I am getting a bunch of ? after it finishes reading. How do I fix it? #include <stdio.h> int main(void){ FILE *fr; /* declare the file pointer */ fr = fopen ("some.txt", "r"); /* open the f...

C equivalent to fstream's peek

Hi, I know in C++, you're able to peek at the next character by using: in.peek();. How would I go about this when trying to "peek" at the next character of a file in C? ...

How does scanf() really work?

On Windows, char c; int i; scanf("%d", &i); scanf("%c", &c); The computer skips to retrieve character from console because '\n' is remaining on buffer. However, I found out that the code below works well. char str[10]; int i; scanf("%d", &i); scanf("%s", str); Just like the case above, '\n' is remaining on buffer but why scanf s...

Fast 'C' library to tranparently manage very large files

I need to save very large amounts of data (>500GB) which is being streamed (800Mb/s) from another device connected to my PC. The speed rules out use of a database e.g. MySQl/ISAM and I am looking for a fast, light library which sits on top of the 'C' stdio file lib (i.e. fopen/fclose/fwrite) which will allow me to write/read a very larg...

capturing empty output returned from commandline and display some message if its empty

Thank you guys for answering "capturing commandline output directly in a buffer" .. thanks a lot.. I used popen() to get my results correct. here i have one more thing may be small one I need to print something in my window when the script "check.sh" returns nothing, means validation when no output from the script is returned. check.s...