Public Group

This topic is 3091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey. I can read from a file no problem. thats easy. Im just wondering if there is a way to read the first two values from a text file and then use them in the proceeding for loops i will use to read the rest of the values. I want to read values from a text file, storing them in an adjacency matrix. Im gonna use a nested for loop to add the values to the 2D array. But i want the first two values in the text file to be the length and width of the adjacency matrix. Anyone know hoe id accomplish this? Cheers.

##### Share on other sites
Quote:
 Im just wondering if there is a way to read the first two values from a text file and then use them in the proceeding for loops i will use to read the rest of the values. .
Sure, that should be straightforward to do. What programming language are you using?

##### Share on other sites
Cheers. Using C++

##### Share on other sites
How familiar are you with stream input/output in C++?

There's more than one way to do what you're describing, but at its simplest, reading the width and height of the array might look something like this:
std::ifstream file("data.txt");unsigned int width, height;file >> width >> height;
You would then proceed to read in the individual array elements as appropriate (the details would depend on how the text file was laid out, and on how the array data was stored).

Note that in practice, you'd probably want to include some error checking.

If you need additional help, perhaps you could post the code that you have so far, along with an example text file showing how the data is arranged.

##### Share on other sites
problem i have is that when i read the values in and try to use them when i define the array i get from the following code:

int length, width;
myfile >> length;
myfile>> width;

1>c:\users\downey\college\main game\testprograms\readfromfil\readfromfile.cpp(40) : error C2466: cannot allocate an array of constant size 0
1>c:\users\downey\college\main game\testprograms\readfromfil\readfromfile.cpp(40) : error C2466: cannot allocate an array of constant size 0

##### Share on other sites
Quote:
Right, that doesn't have anything to do with reading data from a file, but rather with how to correctly create and manage dynamic arrays in C++.

These threads may not answer all of your questions, but take a look here and here (each of these threads includes information on creating and managing dynamic 2-d arrays in C++).

##### Share on other sites
Moved to For Beginners.

##### Share on other sites
Quote:

These mean exactly what they say. (Well, the 'expected constant expression' errors mean exactly what they say; the other ones are the result of trying to ignore that error and re-interpret the code, and still failing.)

"Constant" means "unchanging"; i.e. known ahead of time.

You can't, in C++, use a variable (i.e. something calculated by running the program, whether by reading a file or asking the user to input a number or whatever else) to set the size of an ordinary array like that. You have to pick a size ahead of time.

So you have to use something more advanced. See the links in jyk's post.

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5

• 20
• 11
• 13
• 14
• 78
• ### Forum Statistics

• Total Topics
632142
• Total Posts
3004393

×