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

Jon

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

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java: How do I read and store data from input file?

From Dev

How do I read from file using pebble c?

From Dev

Python Django- How do I read a file from an input file tag?

From Dev

Having trouble looping over a function every time I read in a '\n' from an input file in C

From Dev

Having trouble looping over a function every time I read in a '\n' from an input file in C

From Dev

How do I read a matrix from a file?

From Dev

How do I run a function from a class file in C++?

From Dev

How can i read a number in c with a function and use it in main?

From Dev

How to do unit testing of main file's function in c

From Dev

How do I return a value from the main function in MIPS?

From Dev

How do I pass a string array from a function to main

From Dev

From a file, how do I read from a certain point in C++

From Dev

How do I read a single String from standard input?

From Dev

How do I match on a string read from standard input?

From Dev

How do I get setTimeout to read the value from the INPUT?

From Dev

How do I match on a string read from standard input?

From Dev

read in MIPS file opened in C

From Java

How do I read both from keyboard and from file?

From Dev

How To Read Input From Test Case File in C

From Dev

How to read a .txt file from user input in C

From Dev

How to read structure information from an input file in c

From Dev

C++ Bank Project; How do I read string from file convert to float and sum all

From Dev

How do I read two strings on each line, line by line, from a file in C

From Dev

How do I upload csv file from FTP directly which will be as input in c#?

From Dev

How do I read this function?

From Dev

File I/O in C - How to read from a file and then write to it?

From Dev

How do I read a Hiera value from a Puppet function?

From Dev

How do I read a variable from another function?

From Dev

In Java, how do you obtain a Socket or DatagramSocket from the file descriptor of an already opened C socket?

Related Related

  1. 1

    Java: How do I read and store data from input file?

  2. 2

    How do I read from file using pebble c?

  3. 3

    Python Django- How do I read a file from an input file tag?

  4. 4

    Having trouble looping over a function every time I read in a '\n' from an input file in C

  5. 5

    Having trouble looping over a function every time I read in a '\n' from an input file in C

  6. 6

    How do I read a matrix from a file?

  7. 7

    How do I run a function from a class file in C++?

  8. 8

    How can i read a number in c with a function and use it in main?

  9. 9

    How to do unit testing of main file's function in c

  10. 10

    How do I return a value from the main function in MIPS?

  11. 11

    How do I pass a string array from a function to main

  12. 12

    From a file, how do I read from a certain point in C++

  13. 13

    How do I read a single String from standard input?

  14. 14

    How do I match on a string read from standard input?

  15. 15

    How do I get setTimeout to read the value from the INPUT?

  16. 16

    How do I match on a string read from standard input?

  17. 17

    read in MIPS file opened in C

  18. 18

    How do I read both from keyboard and from file?

  19. 19

    How To Read Input From Test Case File in C

  20. 20

    How to read a .txt file from user input in C

  21. 21

    How to read structure information from an input file in c

  22. 22

    C++ Bank Project; How do I read string from file convert to float and sum all

  23. 23

    How do I read two strings on each line, line by line, from a file in C

  24. 24

    How do I upload csv file from FTP directly which will be as input in c#?

  25. 25

    How do I read this function?

  26. 26

    File I/O in C - How to read from a file and then write to it?

  27. 27

    How do I read a Hiera value from a Puppet function?

  28. 28

    How do I read a variable from another function?

  29. 29

    In Java, how do you obtain a Socket or DatagramSocket from the file descriptor of an already opened C socket?

HotTag

Archive