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

105 questions
48
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,548
  • 5
  • 24
  • 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
  • 746
  • 3
  • 11
  • 26
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
  • 80,996
  • 26
  • 120
  • 129
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
  • 13,993
  • 32
  • 78
  • 99
5
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,965
  • 11
  • 53
  • 80
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
  • 383
  • 1
  • 4
  • 14
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
  • 5,139
  • 3
  • 32
  • 46
4
votes
1 answer

Do you need to fclose() both files after freopen()?

I'm writing a code which takes one file and saves it into another with a different name - however, I am not sure whether or not I need to fclose both files or not? FILE *logfile = fopen("log.txt", "a+"); while(1) { char filename[500]; char…
Daniel
  • 47
  • 5
4
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
  • 30,774
  • 21
  • 92
  • 114
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,453
  • 2
  • 18
  • 34
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
  • 70,199
  • 15
  • 131
  • 234
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…
mosharaf13
  • 336
  • 2
  • 15
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,833
  • 19
  • 30
3
votes
3 answers

How can I tell the program to stop using freopen

I am beginner in C++ and I have a question that is beyond my limits. I compile under GNU GCC. I use #include also known as: #include At some point in my program I tell the program to use the file de_facut.txt as an in…
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
1
2 3 4 5 6 7