Questions tagged [freopen]

freopen reopens a stream with a different file or mode.

Reuses stream to either open the file specified by filename or to change its access mode.

Reference: freopen

94 questions
45
votes
2 answers

Create a file if one doesn't exist - C

I want my program to open a file if it exists, or else create the file. I'm trying the following code but I'm getting a debug assertion at freopen.c. Would I be better off using fclose and then fopen immediately afterward? FILE *fptr; fptr =…
karoma
  • 1,488
  • 4
  • 25
  • 43
12
votes
2 answers

Using freopen() to print to file and screen

I am trying to use freopen() to print to a text file and the screen, but I am only achieving the printing to a file. I was wondering if there was an easy to save the programs output to a file and print it to the screen? Because I had this working…
Bob
  • 706
  • 3
  • 10
  • 24
8
votes
2 answers

iPhone: Once I have redirected NSLog to a file, how do I revert it to the console?

I'm using: #if TARGET_IPHONE_SIMULATOR == 0 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *logPath = [documentsDirectory…
Ben Clayton
  • 75,781
  • 25
  • 117
  • 124
5
votes
2 answers

Equivalent of freopen in Java

Please suggest a method to obtain a similar behaviour in Java as when we do freopen("filename","r",stdin) OR freopen("filename","w",stdout) in C.
avd
  • 12,513
  • 29
  • 74
  • 96
5
votes
4 answers

Writing to both terminal and file c++

I found this question answered for Python, Java, Linux script, but not C++: I'd like to write all outputs of my C++ program to both the terminal and an output file. Using something like this: int main () { freopen ("myfile.txt","w",stdout); cout<<…
Aly
  • 193
  • 2
  • 9
5
votes
3 answers

fclose works differently on android and linux

Following program: #include #include #include #include int main() { fclose( stderr ); printf( "%d\n", fileno( stderr ) ); return 0; } shows -1 on ubuntu 11.04 and 2 on ICS 4.0.3 emulator. Can't…
Andrey Starodubtsev
  • 4,588
  • 2
  • 24
  • 40
4
votes
3 answers

Redirecting the stdout and stdin - Java

While writing c/c++ code it's pretty handy to use freopen(). Please see the following code snippet - int main(){ int n1, n2, result; freopen("input.txt", "rb", stdin); freopen("output.txt", "wb", sdtout); while(scanf("%d %d", &n1, &n2)==2 &&…
Razib
  • 10,057
  • 10
  • 46
  • 71
3
votes
1 answer

Is it allowed to use freopen with "w+" mode for stdin?

Consider the following code: freopen("buffer.txt", "w+", stdin); fprintf(stdin, "hello"); fseek(stdin, 0, SEEK_SET); char str[16]; scanf("%s", str); printf("%s", str); I've found no entries in standard restricting me from doing that, but also no…
Denis Sheremet
  • 2,225
  • 1
  • 12
  • 30
3
votes
1 answer

How to freopen() both stdout and stderr into a single output file under Windows

I've got a Windows Win32/GUI application that sometimes prints interesting output to both stdout and stderr, so what I'd like to do is capture that output into a file for review after the application has exited. The problem is, I can successfully…
Jeremy Friesner
  • 57,675
  • 12
  • 103
  • 196
3
votes
2 answers

when using freopen() in visual studio c++ system("pause") is not working

I was trying to read from a file in vs17. But here system("pause") isn't working. The console window here just pops up and vanishes. The input.txt file contains only one integer. #include #include #include #pragma…
mosharaf
  • 293
  • 1
  • 13
3
votes
1 answer

Write log content into text file using freopen not working with Swift

I want to write every print() content into a text file in Swift 3.0. It was working fine in Objective-C but not worked in Swift 3.0. I am using the code as below, func redirectLogToDocuments() { let docDirectory: NSString =…
Gautam Sareriya
  • 1,807
  • 18
  • 30
3
votes
1 answer

Equivalent of freopen in Go

In C I can read and write files using scanf and printf by piping them as follows: freopen ("input.txt", "r", stdin); freopen ("output.txt", "w", stdout); In Java you can do the same with System.setIn And friends. This it very convenient if you…
Thomas Ahle
  • 28,005
  • 19
  • 77
  • 105
3
votes
4 answers

SDL Console output works when debuging, but not when run with the exe

I am writing an experimental networking program, basically a test program for learning networking. I am using SDL and SDL_net in Code::Blocks with mingw, so the console output was being directed to stdout.txt. I searched around and found that you…
QubicGames
  • 63
  • 1
  • 7
2
votes
1 answer

freopen not writing to the specified file

I am trying to redirect output of stdout and stderr using a file. I am using freopen and it creates the file in the correct directory but the file is blank. When I comment out the code to redirect the stdout and stderr - the output shows up on the…
user1185853
  • 49
  • 2
  • 7
2
votes
1 answer

Having child processes printf to redirected stdout correctly

I am redirecting stdout on a process with freopen(), and as long as it's just one process, everything's fine. However, if I do something like this: freopen("stdout.txt", "a+", stdout); printf("Initial line.\n"); int i=0; while(i<1000) { …
Jim
  • 43
  • 4
1
2 3 4 5 6 7