Passing an array of pointers from a function to main()

I have a a group of text files that are used as input into a program. Another very similar program needs the same data in a different input format. I am writing a program that will read in each line of data and parse it. I have successfully written the program in main() such that it will read until the end of file. However, I am trying to re-write the program such that the algorithm is in an external function and will read one line at a time and then pass all of the character strings to the main program. The program will not compile and I am positive it has to do with an incorrect use of arrays in passing the variable "token". I am attaching a copy of the main program and the function. Can someone give me some guidance on where I am going wrong.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdio.h>

void Line_Parse(std::ifstream&,std::ostream&,const char* token);

int main(int argc, const char * argv[]) {
    const char* token;
    FILE *Output_File;
    
    Output_File = fopen ("Output_File.txt","w");
    std::ofstream out("Output_File.txt", std::ios::in | std::ios::binary);
    

    std::string Input_File("Input.txt");
    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
    }
    
    // Call routine to retrieve parsed line of characters
    Line_Parse(inp,out,token);
    std::cout << &token[1] << std::endl;
    if (strncmp (&token[0],"0",1) == 0)
    {
        for(int i = 0; i < 10; i++) out << &token[i] << " ";
    }
    out << std::endl;

    inp.close();
    
    return 0;
}

 void Line_Parse(std::ifstream& inp,std::ostream& out,const char* token)
{
    const int MAX_CHARS_PER_LINE = 1200;
    const int MAX_TOKENS_PER_LINE = 40;
    const char* const DELIMITER = " ";
    
    while(!inp.eof())
    {
        // 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
        const char* 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
            }
        }
    }
}

Off the top of my head -

In function Line_Parse

const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
// parse the line
token[0] = strtok(buf, DELIMITER);

I could be wrong but it could be that you are doing a lot of assignment to a const char buffer.
Wouldn't the const-ness of the buffer mean that you could not do that?
1. The program above compiles well.

2. Line 10, 12: You've defined a FILE* named Output_File but after assigning it a value, never used it.

3. Be careful: You shouldn't mix C-Stdio (FILE*, fopen(3), ...) with C++ streams. They use different internal buffers, which may confuse you.

4. Line 13: It's not a bad idea to open an output file for output instead of for input (see http://www.cplusplus.com/reference/fstream/ofstream/open/ for details).

5. Line 38: Line_Parse() declares a parameter named `out'. but this is never used there.

6. Line 38: Parameter token of Line_Parse() gets never assigned any value inside Line_Parse()! All token-assignments there are applied to the variable token defined local to your while-loop. Instead of defining a local variable token you may want to move this definition to main() replacing mains token definition at line 9.
Topic archived. No new replies allowed.