Accessing Data from a directed input file in C++ with Visual Studio - c++

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/

Related

C++: Problems of file write using FILE Library when file beat process is running

I have a code for file write using FILE Library, and usually works, but I found a case where doesn't work: When the code runs concurrently with filebeat process.
I don't know this cause of the problem because my c++ project does not support debugging mode.
I am participating in an open source project developed by someone else and i am not familiar with this project yet.
This is my c++ code:
FILE *fptr;
fptr = fopen("log_path.c_str()", "w");
if (fptr == NULL)
{
printf("Error!");
exit(1);
}
fprintf(fptr, "%s", log.c_str());
fclose(fptr);
Is there a any other good way to save log files?
Please give me some advice.
Your code have broken-pipe exception when you try to write a file.
This exception occurs when a c++ code try to write a log file while the filebeat software is reading the log file.
So, I recommend using this C++ code:
#include <fstream>
#include <iostream>
logFilePath = "this is path string of log file";
log = "this is log string";
ofstream output(logFilePath, ios::app);
output << log << endl;
output.close();
This code that used the offstream library will be solve the broken pipe exception.
If you use this c++ code, it is able to use string type for file write process, so it's not necessary to type convert via c_str().
I checked that this code able to used with File beat 7.10.0.
Thank you.

How to switch back to keyboard/terminal input after using freopen() in C++? [duplicate]

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.

Is there a way to use getline() with an external text file then get control back for cin to take input from console?

I am new to C++ and wondered of there is a way to use just standard iostream to read in an input file (from using debugging properties: < filename) or other then get control back to the console to input something else later with cin.
I separated the file read part into a different function but it seems when I specify in the project properties the command to grab the file contents line by line with getline() it skips over any cin commands I issue later.
I'm sure this could just be a setup issue or I am may need to break it off into another program in the project somehow? This is a console app but surely there is a way to do both in the same project?
I have read that you can't use both cin and getline together but how does one input a file then go ask for more info from the user in a C++ app using visual studio?
Separate program and functions for the file read
'int lineIter = 0;
cin.getline(rowData, arraySize); // Grab first row of data
while (!cin.eof()) {
// output each row of data to screen:
cout << rowData << endl;
}
///// Increment for next ROW
lineIter++;
cin.getline(rowData, arraySize);'
Then later how do I go back to being able to use cin or other to get input from user?
I tried many variations of below later:
'cin.clear();
cin.ignore(arraySize);
cin >> selectR[b];'
and other variations of getline() but none stop program execution and I can't get them to do anything except try to read the file again.
I am using VS 2019 Community Edition
I don't really understand what you're trying to achieve here.
Currently, the way you read your external file is by changing std::cin's "source" from the console to the given file ; this change is made outside your program, at the debugger's level. So you will not be able to change the source back to the console from the program itself. (You might be able to do from the debugger while it's running though).
If you want to read external files and still be able to use std::cin normally, why not using std::fstreams ?
On the other hand, if you absolutely have to pass the file to the program through std::cin, you should keep it default and simply copy-paste your file in the console. Be careful though : since the file probably contains newlines, you will not be able to use those as end of input, so you'll have to design another way for the program to reckognize the end of the file (two consecutive empty lines for example).

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<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.

C++ File Handling - Output does not match Input

I'm in an introductory C++ class and I am having a strange issue with reading from an input file. I have a text file named "inputFile.txt" that only contains the value of 5. The following code is meant to open the file, read the value of 5 and assign it to the variable 'a' then print the value of 'a' to the console. No matter what I do, the code always prints out "528".
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
int a;
//create and open file
ifstream input;
input.open("inputFile.txt");
//read value of 5 from file and assign to a
input >> a;
//print value of a
cout << a << endl;
system("PAUSE");
}
I hate to ask this question because it's so basic that I feel I MUST be overlooking something extremely simple. However, I am at a complete loss right now and no amount of web searches has given me any enlightenment. If you could point out what I've done wrong I would really appreciate it.
Nothing seems to be wrong with your code. I ran it exactly as is on my PC and it worked 100%.
Possible problems:
You have the file stored in the wrong place
The file isn't named properly
The file actually contains "528"
Your IDE is experiencing some glitch