简体   繁体   English

使用Visual Studio从C ++中的定向输入文件访问数据

[英]Accessing Data from a directed input file in C++ with Visual Studio

I'm very new to programming and C++. 我是编程和C ++的新手。 I've been trying to access data from a text file in my C++ program. 我一直在尝试从C ++程序中的文本文件访问数据。 I found this: debugging with visual studio using redirected standard input which I found very helpful to getting the redirected input set up. 我发现了这一点: 使用重定向的标准输入在Visual Studio中进行调试,发现对设置重定向的输入非常有帮助。

I don't know how to access that file in my C++ program however. 我不知道如何在我的C ++程序中访问该文件。 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 . 据我了解, cin.get(c)在文件中的字符行下移并将它们暂时放置在c I thought that this program would do that and print the temporary value of c . 我认为该程序可以做到这一点,并打印c的临时值。 But that is not happening. 但这没有发生。

You can use fstream: Stream class to both read and write from/to files. 您可以使用fstream:Stream类来读取和写入文件。
source to refer: http://www.cplusplus.com/doc/tutorial/files/ 参考来源: http : //www.cplusplus.com/doc/tutorial/files/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM