首页 文章

我想在c中创建一个函数来读取一个以文件为参数的文件,但是在编译程序时遇到了问题

提问于
浏览
-1

这是读取文件的函数

void read_function(istream& filename, vector< vector<double> >& v)
{
//vector<vector<double> > v;
    if (filename)
    {
        string linea; 
        int i = 0;
        while (getline(filename, linea))
        {
            v.push_back(vector<double>());
            stringstream split(linea);
            double value;
            while (split >> value)
            {
                v.back().push_back(value);
            }           
        }
    }
};

主要功能

int main(int argc, char const *argv[]){

if (argc < 2)
{   
    cerr << "input file's name\n"<< endl;
}   

string program_name = argv[0];
ifstream input;
input.open(argv[1]);

vector< vector<double> > array;

for (int i = 0; i < array.size(); i++) 
{             
    for (int j = 0; j < array[i].size(); j++) 
        cout << read_function(argv[1], array) << '\t';    
        cout << endl;
}

return 0;
}

当我编译代码时,我收到以下错误消息

错误:从类型'const char *'cout << read_function(argv [1],array)<<'\ t'的表达式初始化'std :: istream&{aka std :: basic_istream&}'类型的引用无效

错误:传递'void read_function(std :: istream&,std :: vector>&)的参数1'void read_function(istream&filename,vector <vector>&v)

1 回答

  • 1

    请不要采取错误的方式,但是您在代码中所犯的错误看起来像这对于您的编程技能水平来说可能是一项非常困难的任务 .

    你犯了几个错误:

    • 你(有点)在 main 中声明 input 但是尝试在 read_function 中使用它

    • 您在 getline 中使用的参数不正确

    • read_function 的第一个参数是 ifstream 类型 char*

    • getlineifstream 的成员函数

    • read_function 什么都不返回,所以 cout<<read_function(...) 不正确

    我建议在尝试使用更复杂的东西之前,比如 sstreamfstreamvector ,首先要了解如何调用函数,参数及其类型是什么,对象是什么以及如何访问其成员 .

    我纠正了你的错误,下面的代码只读取输入文件中的数字,没有字符 . 我认为这是你的目标,因为你使用了 double 向量 . 还有更优雅的方法来做到这一点,但我尽量保持尽可能接近你的代码 .

    码:

    #include<fstream>
    #include<iostream>
    #include<vector>
    #include<sstream>
    
    using namespace std;
    
    void read_function(char* filename, vector< vector<double> >& v)
    {
        int maxNumberOfCharsPerLine=1000;
        ifstream input;
        input.open(filename);
        if(input.is_open())
        {
            char* inputChar;
            string linea; 
            while (input.getline(inputChar, maxNumberOfCharsPerLine))
            {
                linea=inputChar;
                v.push_back(vector<double>());
                stringstream split(linea);
                double value;
                while (split >> value)
                {
                    v.back().push_back(value);
                }           
            }
        }
        input.close();
    }
    
    int main(int argc, char const *argv[]){
    
        if (argc < 2)
        {   
            cerr << "no input file's name\n"<< endl;
        }   
    
        vector< vector<double> > array;
        read_function((char*)argv[1], array); 
    
        for (int i = 0; i < array.size(); i++) 
        {             
            for (int j = 0; j < array[i].size(); j++)  
            {
                cout << array[i][j] <<" ";
            }
            cout << endl;
        }
        return 0;
    }
    

相关问题