I am beginner in C++ and I have a question that is beyond my limits.
I compile under GNU GCC.
I use
#include<stdio.h>
also known as:
#include<cstdio>
At some point in my program I tell the program to use the file de_facut.txt as an in file:
freopen("de_facut.txt","r",stdin);
How can I tell the program to use the console to put the input (as default) instead of the in file?
First I want to read from that file, but later in the program I want the user to enter input in the console.
I hope you understood my problem, I am not very good at english.
The same question has been asked about stdout: How to redirect the output back to the screen after freopen("out.txt", "a", stdout), but the answer is the same for both - there's no clean way of doing this: http://c-faq.com/stdio/undofreopen.html
Since you do need to read from the console later in the program, I'd suggest you just open the file as, well, a file. If the reason you wanted to use stdin to read from a file is the convenience of not having to pass the file handle to functions like fscanf, you could consider using fstream facilities instead - the code can look exactly as when reading from the console:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int x;
cin >> x; // reading from console
{
ifstream cin("input.txt");
cin >> x; // reading from file
}
cin >> x; // again from console
return 0;
}
Use fclose():
FILE *fp=freopen("data.txt","r",stdin);
/*your code here*/
fclose(fp);
ref: C library function - freopen()
In windows,
freopen("CON","r",stdin);
this code worked for me. It switches the stdin to console.
P.S: The text file used to take input before this, must be ended with a newline.
Related
Generally we need to type the input after running any file where we have std::cin, like the C++ code in below
#include <iostream>
using namespace std;
int main()
{
// your code goes here
int t;
cin >> t;
}
I just don't like to enter the same input every time. So I want to automate this process for the same input, say I save my input in a file called input.txt and after running the file, it should take the input from input.txt and output the results. Of course saving input to the clipboard is one way but I might want to copy other things while coding and copy-pasting is itself is again one small job.
I use VS code editor in windows and run code in terminal extension.
The solution is to learn to use your shell. I’m assuming a Unix-like shell here.
Type all of your inputs into a file as you would enter them while the program is running. Save it.
When you run your program, use the command a.out < input.txt. Substitute the appropriate names, obviously.
Your program will read the inputs from the file as if they had been typed in.
Note that because nothing was actually typed in, your formatting might look a bit off, but it’s not a big deal compared to the time you’re saving in running your tests.
You could use, following function inside your main function
int main() {
freopen('input.txt', 'r', stdin);
freopen('output.txt', 'w', stdout); //if you want to save output to a file.
/*
... your code
*/
}
I am beginner in C++ and I have a question that is beyond my limits.
I compile under GNU GCC.
I use
#include<stdio.h>
also known as:
#include<cstdio>
At some point in my program I tell the program to use the file de_facut.txt as an in file:
freopen("de_facut.txt","r",stdin);
How can I tell the program to use the console to put the input (as default) instead of the in file?
First I want to read from that file, but later in the program I want the user to enter input in the console.
I hope you understood my problem, I am not very good at english.
The same question has been asked about stdout: How to redirect the output back to the screen after freopen("out.txt", "a", stdout), but the answer is the same for both - there's no clean way of doing this: http://c-faq.com/stdio/undofreopen.html
Since you do need to read from the console later in the program, I'd suggest you just open the file as, well, a file. If the reason you wanted to use stdin to read from a file is the convenience of not having to pass the file handle to functions like fscanf, you could consider using fstream facilities instead - the code can look exactly as when reading from the console:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int x;
cin >> x; // reading from console
{
ifstream cin("input.txt");
cin >> x; // reading from file
}
cin >> x; // again from console
return 0;
}
Use fclose():
FILE *fp=freopen("data.txt","r",stdin);
/*your code here*/
fclose(fp);
ref: C library function - freopen()
In windows,
freopen("CON","r",stdin);
this code worked for me. It switches the stdin to console.
P.S: The text file used to take input before this, must be ended with a newline.
So i'm making an extremely simple guessing console game and i want to store data permanently in a file (highscore). However everytime i compile the file i'm using empties itself. Is there anyway to stop that?
I've tried a lot of thing which didn't work and i honestly don't know where the problem is. I'm guessing it has to do with the fin and fout but for others it seemed to work
#include <iostream>
#include <fstream>
#include <time.h>
#include <conio.h>
int hs;
//this would be the play_game() function, unrelated to the subject
int main()
{
std::ofstream fout;
fout.open("HS.txt");
std::ifstream fin;
fin.open("HS.txt");
srand(time(NULL));
//menu with 4 options, play, quit, help and highscore (which i'm working on)
fin.close();
fout.close();
}
Don't open your file twice in parallel with two streams. Also, a simple open-for-writing will not truncate your file, but will place you at the start of the file, so you'll be overwriting existing data; see this question. You have to open files with the write mode.
You need to either:
Open your file for both input and output - and without truncating it; see: What does it mean to open an output file as both input and output? , or
Open your file for reading only when your app starts, and open it for writing, and write to it, when it exists (or every once-in-a-while for better resiliency).
I'm very new to programming and C++. I've been trying to access data from a text file in my C++ program. I found this: debugging with visual studio using redirected standard input which I found very helpful to getting the redirected input set up.
I don't know how to access that file in my C++ program however. I think my project found the file because before I found the above linked post I was getting an error saying it couldn't find the file. Now I have no more error.
I need to be able to put the data from the file and put it into variables for use in my program. Can you please provide some guidance on how to extract data from the file for use in my program?
I've tried running this code to print the contents of the file but when I run it, nothing happens:
#include <iostream>
using namespace std;
int main() {
char c;
cin.get(c);
while (!cin.eof()) {
cout << c;
cin.get(c);
}
return 0;
}
From my understanding, the cin.get(c) goes down the line of characters in the file and temporarily puts them in c. I thought that this program would do that and print the temporary value of c. But that is not happening.
You can use fstream: Stream class to both read and write from/to files.
source to refer: http://www.cplusplus.com/doc/tutorial/files/
I have just started learning c++ and I want the programme to stay open after the result is displayed. So I used getch(); and c++ is showing that it should have a prototype. What does it mean? and how do I resolve this>
it means one of the followings:
you are programming under DOS and forgot to include conio.h (https://en.wikipedia.org/wiki/Conio.h). Possibly you copied a source from an older textbook, since conio.h is a very old concept. What sources do you use for learning? I'd recommend one from: The Definitive C++ Book Guide and List
You are programming under Linux and you forgot to include curses.h (http://linux.die.net/man/3/getch)
Include:
1) <conio.h> on Windows.
2) <curses.h> on UNIX
Looks like you only want to PAUSE your console application on the screen. Use this#include <stdio.h> and try getchar(); instead of getch(); or simply system("pause"); or cin.ignore() will do the job for you.
Also, "Start without Debugging" using Ctrl-F5 will allow you to Press Any Key To Continue at the end of your program. This way it won't close until you press some key and the console will pause on the display screen.
If you're on Windows then getch is a function from the Windows-only <conio.h> library. You need to include it (#include <conio.h>).
It's possible only on Windows.
Also, getch() is deprecated.
Use _getch() instead.
If you're on GNU+Linux, then getch is a function from the <curses.h> library. You need to include it (#include <curses.h>).
The objective as I understand is :
I want programme to stay open after the result is displayed
Why not do it the typical c++ way?
#include<iostream>
int main(void)
{
int i;
char ch;
std::cout<<"Enter any character : ";
std::cin.get(ch); // For testing enter a string at this step say "String"
/* The input to cin is line-buffered, so after reading 'S' to ch,
* the remaining "tring" is still in the buffer.
*/
std::cout<<"Entered character : "<<ch<<"\n";
while(std::cin.get()!='\n')
;;
/* cin.get() is an overloaded function in the istream class.
* If no arguments are passed to 'get()' this function reads single next character
* In essence, we wait for the cin.get() to clear the buffer that is
* read all characters including '\n'
*/
std::cout<<"Press any key to continue..\n";
std::cin.get();
/* Since we have already cleared the buffer using the loop
* 'get()' expects us to enter a character this time
*/
return 0;
}