【问题标题】:Read Files using a function C++使用函数 C++ 读取文件
【发布时间】:2014-03-06 12:37:52
【问题描述】:

我想知道如何使用流读取文件以及在函数中使用它们。 到目前为止,我的代码是;:

    #include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void burbuja(int[]);
void imprimeArreglo (int[],int);
void leeArchivo(string&);
int arreglo[10];
int i;

void burbuja (int a[])
{
int i,j;
    for(i=0;i<10;i++)
    {
        for(j=0;j<i;j++)
        {
            if(a[i]>a[j])
            {
                int temp=a[i]; //swap
                a[i]=a[j];
                a[j]=temp;
            }

        }

    }
}
void imprimeArreglo(int a[],int tam)
{
    for(int i=0;i<tam;i++)
    cout << a[i] << " ";
}
void leeArchivo(string& nombre)
{
string filename = nombre;
ifstream myfile(filename);
string line;
if (myfile.is_open()) {
 while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
}
else cout << "Unable to open file"; 


}
int main()
{
    string nombre = "arr.txt";

  leeArchivo(nombre);
      cin >> i ;
      return 0;
}

我希望能够从 main 方法调用 leeArchivo("arr.txt")。 有了这个我得到了错误:

Error:  bubble.cpp(37,14):'ifstream' is not a member of 'std'
Error:  bubble.cpp(37,19):Statement missing ;
Error:  bubble.cpp(39,20):Undefined symbol 'file'
Error:  bubble.cpp(39,25):Could not find a match for 'std::getline(undefined,std::basic_string<char,std::string_char_traits<char>,std::allocator<char>>)'

我在这里缺少什么? (我是 C++ 新手) 我要读取的文件具有以下结构: &lt;number&gt;

&lt;number&gt; &lt;number&gt; &lt;number&gt; ...

例如:

5

19 28 33 0 1

===========================================

编辑: 我使用的是 Borland C++ 5.02

编辑 2: 更新代码,使用 Geany 现在错误是:BUBBLE.cpp:38:25: error: no matching function for call to 'std::basic_ifstream&lt;char&gt;::basic_ifstream(std::string&amp;)'

【问题讨论】:

  • 1) 你的 main 函数没有返回类型,2) 你 using namespace std; 但在某些地方仍然放了 std::。只需删除 using 指令并指定 std::, 3) @987654321 @绝对是命名空间std::的成员
  • 也就是说,您使用的是什么编译器? (我很惊讶它没有提到 main 没有返回类型)
  • main()this should compile correctly的非标准声明之外。无论您在发布时从代码中剥离了什么,都将其放回并更新帖子。这样做时,还要添加您正在构建的编译器和平台。 (我会使用cstdlib,但这不应该导致您遇到的问题;您显示的代码对此负责)。报告的错误声称它们位于第 37 和 39 行。显然缺少某些内容。
  • 刚刚添加了整个代码。

标签: c++ io stream readfile


【解决方案1】:

ifstream 的这种行为特别奇怪。试试这个编辑:

void leeArchivo(const string&);
void leeArchivo(const string& nombre)
{
    ifstream file(nombre.c_str());
    string line;
    while(getline(file,line)) {
        cout << line << endl;
    }
}

int main()
{
    leeArchivo("arr.txt");
    return 0;
}

另外,使用:

#include <cstdlib>

代替:

#include <stdlib.h>

【讨论】:

  • 尝试使用 cstdlib 时出现错误。 Error: bubble.cpp(2,2):Unable to open include file 'CSTDLIB.h'
  • @DavidMerinos:你正在尝试#include &lt;cstdlib.h&gt;。那是不存在的。应该是:#include &lt;cstdlib&gt;.
  • 不,我的电话是:#include &lt;cstdlib&gt;
  • 会不会是我的 Borland C++ 的错误? @jrd1
  • @DavidMerinos:改用这个:leeArchivo(nombre.c_str()); 那是因为ifstream 不接受std::string 参数 - 它需要const char * 参数:cplusplus.com/reference/fstream/ifstream/ifstream
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
相关资源
最近更新 更多