C++ 使用函数C+读取文件+;

C++ 使用函数C+读取文件+;,c++,io,stream,readfile,C++,Io,Stream,Readfile,我想知道如何使用流读取文件,但也要在函数中使用流。 到目前为止,我的代码是;: #include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; void burbuja(int[]); void imprimeArreglo (int[],int); void leeArchivo(string&); i

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

    #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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
无效的burbuja(int[]);
无效无效(int[],int);
void leeArchivo(字符串&);
int arreglo[10];
int i;
无效的burbuja(int a[])
{
int i,j;

对于(i=0;i而言,
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;
}
void leeArchivo(常量字符串&);
void leeArchivo(常量字符串和名称)
{
ifstream文件(nombre.c_str());
弦线;
while(getline(文件,行)){

cout 1)您的主函数没有返回类型,2)您
使用名称空间std;
但在某些地方仍然将
std:
放入。只需删除using指令并指定
std:
,3)肯定是名称空间
std:
的成员,也就是说,您使用的编译器是什么?(我很惊讶它没有提到main没有返回类型)在
main()
的非标准声明之外。发布代码时,无论从代码中剥离了什么,都要将其放回去并更新文章。同时,还要添加编译器和构建平台。(我会使用
cstdlib
,但这不应该导致您遇到的问题;您没有显示的代码应该对此负责)。报告的错误声明它们位于第37行和第39行。显然缺少一些内容。只是添加了整个代码。在尝试使用cstdlib时,我遇到了一个错误。
错误:bubble.cpp(2,2)无法打开包含文件“CSTDLIB .H’/Cux> @ DavidMerinos:您尝试的是代码> >包含< /COD>。这是不存在的。它应该是:<代码>包含“< /代码>”。不,我的行是:<代码>包含“< /代码> >这是否是我的Borland C++的bug?@jrd1@DavidMerinos:改为使用:
leeArchivo(nombre.c_str())
这是因为
ifstream
不接受
std::string
参数-它需要
const char*
参数:
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>