简体   繁体   中英

C++ Multi dimensional array from external file question

I'm trying to get a contiguous line with values separated by "&" to load into a multi-dimensional array. Here's the way I'm trying to do it - Everything checks out in the code, except the string "str" which contains my separated values in the format "value1, value2, value3, etc..." just loads that whole string into array[0][0]. I know there are better ways of doing this, but what I would like to know is why C++ won't treat "str" as if I had typed out the individual values and hard coded "array".

Here is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;


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

    string str, strTotal;

    ifstream in;

    in.open("Desktop/01_001.PAC");

    getline(in,str);
    while ( in ) {
        strTotal += str;
        getline(in,str);
    }


    string searchString( "&" ); 
    string replaceString( ", " );

    assert( searchString != replaceString );

    string::size_type pos = 0;
    while ( (pos = str.find(searchString, pos)) != string::npos ) {
        str.replace( pos, searchString.size(), replaceString );
        pos++;
    }



    string array[4][5] = {str};

    cout << array[0][0];

    return(0);
}

And here is the external file ( "Desktop/01_001.PAC" ):

void&void&void&void&a&a1&a2&a3&b&b1&b2&b3&c&c1&c2&c3&d&d1&d2&d3

Thanks in advance!

If I'm reading your code correctly, you appear to be searching through the string (loaded from file), and only assigning the very last result to an array index (x=4, y=5). So your code is doing something like this:

while (have not found last variable)
    search for next variable in string

assign variable to (4,5) in matrix

So that last assignment might even work, but since you only assign at the end, the array is not going to be filled the way I think you want it to be filled.

I'm going to assume the matrix you want is always the same size, otherwise things get more complicated. In this case, you could use something like this:

let xMax = 4
let yMax = 5

for (x from 0 to xMax)
    for (y from 0 to yMax)
        find the next variable in the string
        assign it to the current (x,y) location in matrix

Debug statements are your friend here! Try the above solution without saving it to an array, and instead print out each term, to see if it is working correctly.

I would also point out that the string "void" is not the C++ keyword void, and so will not work if you want an array index to be void. Try getting your code to work without voids at first.

Because code and data are different things. Your code is compiled before it runs.

It sounds as if this is what you expect:

  1. The string contains the text "foo, bar, baz".

  2. The statement string[] whatever = {str}; is run.

  3. Since "str" contains "foo, bar, baz", you want it to have the same effect as if the line of code were actually string[] whatever = {"foo", "bar", "baz"} .

Asking something like this implies a complete misunderstanding of how programming works.

Nothing this magical will ever happen in C++. It cannot , because (a) what if you actually wanted to put 'str' into the array? (b) what if 'foo', 'bar' and 'baz' were also variables in your program - should they be interpreted the same way?

Variable names are not text. They no longer exist, for all practical purposes, at the time that your code runs . They are only there so that you, as the programmer, can say "the value that is used over here should be the same one that is used over there ".

Further, array initializations in C++ do not care how many elements are actually in the initialization vs. the declared size of the array . Any additional elements will be default-initialized (ie, assigned empty strings).

A string cannot be treated like an array of strings, because it isn't one . If you want an array of strings, then build it, using the individual string elements as you determine them.

But since you don't know in advance how many elements there are, you should use std::vector instead of an array. And why are you trying to arrange the data into a 2-dimensional structure? How are you expecting to know how "wide" it should be?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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