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
1
vote
1 answer

Using freopen to read file

I have an assignment where I implement binary search and linear search. The "hard" part is done and both of the those methods are implemented and work. My professor wants us to test arrays with large number of integers. He gave us a .in file that…
GusGus
  • 230
  • 6
  • 16
1
vote
1 answer

How to undo effects of freopen when launching a shell?

A piece of code ties stdout to a file in C using freopen. After this piece of code, a script is executed which launches a shell. The problem is now that all stdout output is going into that file, so any commands run in that shell are being placed in…
Deal
  • 106
  • 10
1
vote
2 answers

Passing an input file to an output file in C?

Currently I am able (I think) to open a file using fopen. For testing purposes I want to be able to pass the file's contents to an output file, but I'm not getting the desired results. Here is some code: #include #include int…
blutuu
  • 590
  • 1
  • 5
  • 20
1
vote
3 answers

can't open a file by reading its name from another file in c++

Hi I am trying to read a file name from another file and then read it. But I can only read the first file which contains the name of the second file I want to open. Here is how I am doing it.. int…
Tahlil
  • 2,680
  • 6
  • 43
  • 84
1
vote
2 answers

Freopen causes segmentation fault

Got any ideas why this: #include #include int nrpart; int k; void main() { printf("lol"); freopen("p2.in","r",stdin); freopen("p2.out","w",stdout); printf("roar"); } outputs only lol? I used freopen some other…
Casteurr
  • 956
  • 3
  • 16
  • 35
0
votes
3 answers

system("pause") won't work with freopen

See below in comment. int main(){ //freopen("input.txt","r",stdin);//if I uncomment this line the console will appear and disappear immediately int x; cin>>x; cout<
SevenDays
  • 3,718
  • 10
  • 44
  • 71
0
votes
1 answer

How can i use freopen() to redirect stdout & stdin in thread vise log-file in multi-thread application?

Extened question from In multi thread application how can i redirect stderr & stdout in separate file as per thread? see some how i want to keep all printf and error/warning message produced in each thread in different log-file. FILE * freopen (…
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
0
votes
0 answers

How can i close stream which open with freopen function

#include #include #include using namespace std; int main() { FILE* fd; fd = freopen("1.txt", "w", stdin); int a = 1; cin >> a; cout << a; fclose(fd); cin >> a; cout << a; return…
0
votes
1 answer

C/C++ streams and files

I have the following code: int checkCorrectness(int i,char *iStr) { if(atoi(iStr) == i) return 1; return 0; } void foo(int i) { printf("inside foo %d\n",i); } void print() { char mystring[100]; …
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
0
votes
2 answers

Console outputs gibberish code after re-redirecting stdout to CON

When I use C++ to invoke Python program output (By system command with parameters), it outputs gibberish code at the end of line. After that, I couldn't input any character (Include Backspace and Enter), it displays a hollow square. Console…
Yuchen Ren
  • 287
  • 5
  • 13
0
votes
1 answer

use freopen() and scanf() to read an int from file

I try to read an int from a file using freopen(). File in.txt simply have a number:1, but what I get in output is -858993460. My code is shown below: #include #pragma warning(disable:4996) using namespace std; int main() { …
0
votes
2 answers

Dynamic file stream handling in C with freopen

I am trying to write a program that ideally accepts arguments that specify both a source file (to read from) and a destination file (to write to). The program replaces specified characters in source file, with other specified characters, then writes…
croblin
  • 61
  • 4
0
votes
1 answer

Debug & run result differ in simple c app that redirects stdout using freopen

I'm using linux manjaro. I have the following code (cons.c) #include #include int main() { FILE *f=freopen("/dev/null", "a", stdout); //redirect stdout to /dev/null FILE *g=freopen ("/dev/tty", "a", stdout); // redirect…
NoComprende
  • 731
  • 4
  • 14
0
votes
2 answers

Freopen not writing output after a function call

I was doing a programming question and was using freopen to redirect the streams. The problem I am facing is that the printf command is not printing on the output file after stdout redirection. I even tried using fflush but couldn't get any…
Dhruv
  • 117
  • 11
0
votes
0 answers

cin doesn't work along with freopen in wsl windows

I am running this simple cpp code in wsl and it outputs infinite times. The reason being when I take input into the variable t, it simply doesn't take, and it still has the garbage value initiated to it. #include using namespace…