-1

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. 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(). In order to do this, the input file needs to be opened in main() and read from the function; however, since the iostream name "inp" is defined in main() it is not recognized in the function. 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.

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
        }
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Jon
  • 1,621
  • 5
  • 23
  • 46

2 Answers2

2

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.

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);
Abdullah
  • 2,015
  • 2
  • 20
  • 29
  • Unfortunately this does not work either. If I pass the handle as inp it recognizes the function name and if I try to pass it as Input_File it no longer recognizes the function name. – Jon Nov 21 '14 at 02:42
1

Change the function to accept inp as an argument:

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • When I implement this method the program fails in compilation. I get the error message "Call to implicitly-deleted copy constructor of std::if stream", referencing the inp in the function call. It has no problem with how std::if stream inp is referenced in the function header, but it does not like how it is referenced in the function call. – Jon Nov 21 '14 at 02:30
  • I was able to solve the question by calling the function as Line_Parse(inp,out) and writing the prototype as void Line_Parse(std::if stream& inp,std::stream& out) – Jon Nov 21 '14 at 14:28