【问题标题】:How to take data from a .txt file?如何从 .t​​xt 文件中获取数据?
【发布时间】:2019-02-07 18:39:15
【问题描述】:

我尝试编写一个从 .txt 文件中检索数据并在终端中显示的小程序,但出现错误。 我不得不说我是视觉工作室的新手;到目前为止,我一直在研究代码:块

我已经尝试了错误代码中的建议,在开头添加了#include“pch.h”,但它仍然不起作用。

错误代码是 C1010(如果我构建的代码没有 #include "pch.h" 行);如果我用该行构建它,我会收到多个错误代码:

"1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(10): error C2065: 'ifstream': undeclared identifier
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(10): error C2146: syntax error: missing ';' before identifier 'inFile'
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(10): error C2065: 'inFile': undeclared identifier
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(11): error C2065: 'inFile': undeclared identifier
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(11): warning C4129: 'B': unrecognized character escape sequence
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(11): warning C4129: 'D': unrecognized character escape sequence
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(14): error C2065: 'inFile': undeclared identifier
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(15): error C2065: 'cout': undeclared identifier"

#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    ifstream inFile;
    inFile.open("C:\Users\Bogdan\Documents\UID.txt");

        int x;
    inFile >> x;
    cout << x; 

    return 0; 
}

【问题讨论】:

  • 请告知错误代码/文本是什么。
  • 你需要转义你的\所以用\\替换\
  • 对不起,我的错,我忘了说。代码是C1010(如果我构建的代码没有#include“pch.h”行);如果我用该行构建它,我会收到多个错误代码:
  • 在询问构建错误时,请务必完整完整地包含 实际 错误,包括任何可能的信息说明。并复制粘贴为文本。所以请编辑您的问题以包含它。
  • @Someprogrammerdude 我重写了我的问题

标签: c++ file-io


【解决方案1】:

ifstream 和 cout 都是 std 命名空间的一部分。您不是using namespace std,因此您需要在引用它们时包含命名空间。您的代码的固定版本是:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream inFile;
    inFile.open("C:\Users\Bogdan\Documents\UID.txt");

    int x;
    inFile >> x;
    std::cout << x; 

    return 0; 
}

【讨论】:

  • 谢谢你,布兰登!我添加了这一行: using namespace std; (因为我没有与 std::) 一起使用,现在它可以工作了......部分;在 txt 文件中我有数字 123,但在输出中它类似于 -880513。我会自己解决这个问题:D 感谢您的帮助
  • 这看起来像一个未初始化的 int。我敢打赌文件无法打开,这通常意味着文件路径错误。
  • 你是对的。我修改了文件路径,现在可以完美运行了!
【解决方案2】:

这是 C++ 中的一个小示例,向您展示如何使用 fstream 打开文件,在我添加一个循环 while ( getline (myfile,line) ) 以检查文件流是否有一行,如果它有一行则程序将打印它 cout

#include <iostream>
#include <fstream>
#include <string>

int main () {
  string line;
  std::ifstream myfile ("/path/to/file.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      std::cout << line << std::endl;
    }
    myfile.close();
  }

  else std::cout << "Error unable to open file" << std::endl; 

  return 0;
}

【讨论】:

  • 你改变了什么?为什么?仅代码的答案会鼓励货物崇拜编程,这是很糟糕的。更不用说你的程序做了很多 OP 并没有真正询问的事情。
  • 不使用命名空间std的版本;避免污染全局命名空间并增加编译时间
猜你喜欢
  • 1970-01-01
  • 2014-03-17
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2019-09-15
相关资源
最近更新 更多