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
}
}
}