简体   繁体   English

在C ++函数中,如何读取在main()中打开的输入文件?

[英]In a C++ function how do I read from an input file that was opened in main()?

I have a feeling that this will be a very simple question to answer, but I am having trouble finding an example of this situation. 我觉得这将是一个非常简单的问题,但是我很难找到这种情况的例子。 I have an algorithm that reads in an input file and parses all the character strings on each line. 我有一种算法,它读取输入文件并解析每一行上的所有字符串。 If the first character is a 0, it writes the character strings on that line to an output file. 如果第一个字符为0,则它​​将该行上的字符串写入输出文件。 I have successfully implemented to program in main(), but I want to re-write it as a function (Line_Parse) that can be called from main(). 我已经成功实现了main()中的编程,但是我想将其重写为可以从main()中调用的函数(Line_Parse)。 In order to do this, the input file needs to be opened in main() and read from the function; 为此,需要在main()中打开输入文件并从函数中读取它。 however, since the iostream name "inp" is defined in main() it is not recognized in the function. 但是,由于iostream名称“ inp”是在main()中定义的,因此无法在函数中识别。 A copy of the function and main program as it exists now is attached, I would appreciate guidance on how the stream "inp" can be passed to the main program. 附加了现在存在的函数和主程序的副本,对于将流“ inp”如何传递到主程序的指导,我将不胜感激。

void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,
            int MAX_TOKENS_PER_LINE,char DELIMITER);

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

    std::string Input_File("Input.txt");
    const int MAX_CHARS_PER_LINE = 1200;
    const int MAX_TOKENS_PER_LINE = 40;
    const char* const DELIMITER = " ";

    std::ifstream inp(Input_File, std::ios::in | std::ios::binary);
    if(!inp) {
        std::cout << "Cannot Open " << Input_File << std::endl;
        return 1; // Terminate program
    }
    char *token;
    // read each line of the file
    while (!inp.eof())
    {
        Line_Parse(token,MAX_CHARS_PER_LINE,MAX_TOKENS_PER_LINE,
                   *DELIMITER);
    }
    inp.close();
    return 0;
}

void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,
                int MAX_TOKENS_PER_LINE,char DELIMITER)
{
    // read an entire line into memory
    char buf[MAX_CHARS_PER_LINE];
    inp.getline(buf, MAX_CHARS_PER_LINE);

    // parse the line into blank-delimited tokens
    int n = 0; // a for-loop index

    // array to store memory addresses of the tokens in buf
    *&token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0

    // parse the line
    token[0] = *strtok(buf, &DELIMITER); // first token
    if (token[0]) // zero if line is blank
    {
        for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
        {
            token[n] = *strtok(0, &DELIMITER); // subsequent tokens
            if (!token[n]) break; // no more tokens
        }
    }
}

Actually Input_File is a handler to your input.txt file, in order to use this handler in your Line_Parse function you need to pass it as a parameter to function. 实际上, Input_Fileinput.txt文件的处理程序,为了在Line_Parse函数中使用此处理程序,您需要将其作为参数传递给函数。

void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,int MAX_TOKENS_PER_LINE,
                                        char DELIMITER, std::ifstream & inp);

and you will call it like this. 你会这样称呼它。

Line_Parse(token, MAX_CHARS_PER_LINE, MAX_TOKENS_PER_LINE, *DELIMITER, Input_File);

Change the function to accept inp as an argument: 更改函数以接受inp作为参数:

void Line_Parse(char *&token,int MAX_CHARS_PER_LINE,
                int MAX_TOKENS_PER_LINE,char DELIMITER, std::ifstream inp)

Then call it as: 然后将其称为:

Line_Parse(token, MAX_CHARS_PER_LINE, MAX_TOKENS_PER_LINE, *DELIMITER, inp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM