0

I am having the following code in C++

char *Names[];
int counter=0;
int _tmain(int argc, _TCHAR* argv[])
{
    int data;
    ifstream fileX;
    fileX.open("myfile",ios::in);
    assert (!fileX.fail( )); 
    fileX >> data; 
    while(fileX!=eof())
    {
        createNamesList(data);
        fileX >> data;
    }
    return 0;
}

void createNamesList(char *tmp)
{
    Names[counter] = tmp;
    counter++;
}

What I want to read the data from file line by line and store each line in a two dimension array char* Names[], so that a whole list is saved with me. the size of data in each line is variable length as well as number of lines are; like

 Name[0] ="Data from Line 1"
 Name[1] ="Data from Line 2"
 Name[2] ="Data from Line 3"
 Name[3] ="Data from Line 4"
 .
 .
 .

The above code give me the following error

error LNK2001: unresolved external symbol "char **Names" (?Names@@3PAPADA)

Your help will be appreciated.

7
  • What are you expecting the first line of code char *Names[]; to do? Commented Jan 8, 2014 at 5:48
  • @davidSchwartz taking a two dim array and storing line by line values from file in it Commented Jan 8, 2014 at 5:53
  • I'm just asking what you are expecting the first line of code to do. Be as specific as you can. Commented Jan 8, 2014 at 5:56
  • @davidSchwartz char pointer array Commented Jan 8, 2014 at 5:59
  • 1
    What about a char pointer array? How big of an array? Are you expecting individual elements to be allocated? If so, how many? (It's hard to explain what's wrong with the code without understanding what the author's expectations were. Were you expecting it to somehow know how many items to allocate and how big each one would be such that you could just fill them in later? If not, what were you expecting?) Commented Jan 8, 2014 at 6:01

1 Answer 1

2

The error message you're seeing is barely the tip of the iceberg in the problems with this code.

I'd recommend using the std::vector and std::string classes included with your compiler to make this a bit simpler.

int main() {
    std::ifstream fileX("myfile");

    std::vector<std::string> Names;

    std::string temp;
    while (std::getline(fileX, temp))
        Names.push_back(temp);
    return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

If C++11 is available, it is better to use Names.push_back(std::move(temp)) because its value is discarded after the push any way.
And when c++11 is not available, every time the vector needs to expand its capacity, it will copy the content of every string, making this method extremely inefficient.
@C.R.: At best, it's sometimes a little better. What it means is that when you read the next line, temp has to allocate a new buffer, so what you gained in the push_back, you lose again immediately afterwards. The "extremely inefficient" is pure nonsense. While the contents do get copied, vector resizes itself geometrically, so the number of copies tends to a constant (usually about 3). It's been tested thousands of times under all sorts of conditions, and quite dependably performs very well.
What I mean is that the copy constructor of std::string copies the whole string content, while in C++11 it will be moved. For example, if the average length of string in the vector is 100 chars, then every time the vector resizes itself, there will be 100 N copy operation, rather than N copies.
@C.R.: Yes, I know what you mean. Try to keep in mind that we're talking about reading data from a file though. As an exercise, look up the bandwidth of your memory, and compute how many strings you can copy in the time taken by one disk seek.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.