Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的countlines函数总是返回0?_C++ - Fatal编程技术网

C++ 为什么我的countlines函数总是返回0?

C++ 为什么我的countlines函数总是返回0?,c++,C++,因此,我正在为一个简单的日历应用程序制作一个程序,该程序读取文件input.csv的输入,它是一个文本文件,有两列,每列用逗号分隔,每个命令用新行分隔 我要做的第一件事是计算输入文件中的行数,它作为命令行中的第三个参数传递,因此我可以创建一个数组来分别保存每一行,但函数countLines始终返回0 项目代码: #include<iostream> #include<string> #include<fstream> using namespace std;

因此,我正在为一个简单的日历应用程序制作一个程序,该程序读取文件input.csv的输入,它是一个文本文件,有两列,每列用逗号分隔,每个命令用新行分隔

我要做的第一件事是计算输入文件中的行数,它作为命令行中的第三个参数传递,因此我可以创建一个数组来分别保存每一行,但函数countLines始终返回0

项目代码:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;


//Prototypes
int countLines (ifstream& countfiles);


int countLines(ifstream& countfile)
//counts number of lines in file passed to function
{
   string line;
   int numberOfLines;

   numberOfLines = 0;

   //reads through each line until end of file
   while(getline(countfile, line))
   {
       numberOfLines++;
   }

   return numberOfLines;
}


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

    if(argc != 3) cout << "Usage: calendar.out datafile inputfile";

    //Create input streams to both files
    ifstream apptsfp;
    ifstream inputfp;

    //Open streams to both files
    apptsfp.open(argv[2]);
    inputfp.open(argv[3]);

        int numberOfInputs=0;

    numberOfInputs = countLines(inputfp)-1;

        cout << "number of input commands: " << numberOfInputs << endl;

    return 0;
}

几乎可以肯定,因为您无法打开输入文件

inputfp.open(argv[3]);
if (!inputfp.is_open())
{
    cerr << "failed to open input file " << argv[3] << '\n';
    return 1;
}
似乎您只需要两个参数,而不是三个,正如您在问题中所说的,第一个参数是程序名。这意味着输入文件位于argc[2]中,而argv[3]是空指针

这意味着您的open呼叫将失败,但您没有进行检查。

您对argv[3]的访问不正确。第二个文件名third arg(包括arg[0]中的程序名)位于插槽2中。该数组以零为基础

尝试:

您正在尝试访问argv[3],该argv[3]为空。试试这个:-

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

    if(argc != 3)
        cout << "Usage: calendar.out datafile inputfile";

    //Create input streams to both files
    ifstream apptsfp;
    ifstream inputfp;

    //Open streams to both files
    apptsfp.open(argv[1]);
    inputfp.open(argv[2]);

    int numberOfInputs=0;

    numberOfInputs = countLines(inputfp)-1;

    cout << "number of input commands: " << numberOfInputs << endl;

    return 0;
}

您在什么时候验证文件已成功打开?对不起!我打错标题了,我现在改了。不管输入文件有多少行,它总是返回0。调试它,看看getline是否适合您。如果文件没有打开,它可能会失败。我将不得不研究这个功能,因为我不知道它是如何工作的,但谢谢你的建议,这表明我实际上没有打开文件!你是说向量和推回?向量就像一个数组,但它是动态增长的,PurthyBead在向量的末尾添加了一个项目,用于表示。哦,我对C++是相当新的,所以我从来没有见过向量对象。这是超级整洁的,你能像数组一样访问它的条目吗?是的,lines[i]就像数组一样工作,lines.size会告诉你向量有多大。
apptsfp.open(argv[1]);
inputfp.open(argv[2])
int main (int argc, char* argv[])
{

    if(argc != 3)
        cout << "Usage: calendar.out datafile inputfile";

    //Create input streams to both files
    ifstream apptsfp;
    ifstream inputfp;

    //Open streams to both files
    apptsfp.open(argv[1]);
    inputfp.open(argv[2]);

    int numberOfInputs=0;

    numberOfInputs = countLines(inputfp)-1;

    cout << "number of input commands: " << numberOfInputs << endl;

    return 0;
}