1

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.

1 Answers1

0

You can use fstream: Stream class to both read and write from/to files.
source to refer: http://www.cplusplus.com/doc/tutorial/files/

Hồng Phúc
  • 408
  • 3
  • 10
  • Thanks for the response. That's a good reference. I actually used that reference to write the above code ( I just edited the question to include it). I should've included the code I wrote to begin with. – JimHoward199 Sep 19 '18 at 02:15