Sign in to follow this  

Reading from a file

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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;
int adjMatrix[length][width];

1>c:\users\downey\college\main game\testprograms\readfromfil\readfromfile.cpp(40) : error C2057: expected constant expression
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 C2057: expected constant expression
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 C2087: 'adjMatrix' : missing subscript
1>c:\users\downey\college\main game\testprograms\readfromfil\readfromfile.cpp(40) : error C2133: 'adjMatrix' : unknown size

Share this post


Link to post
Share on other sites
Quote:
Original post by discodowney
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;
int adjMatrix[length][width];

1>c:\users\downey\college\main game\testprograms\readfromfil\readfromfile.cpp(40) : error C2057: expected constant expression
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 C2057: expected constant expression
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 C2087: 'adjMatrix' : missing subscript
1>c:\users\downey\college\main game\testprograms\readfromfil\readfromfile.cpp(40) : error C2133: 'adjMatrix' : unknown size
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 this post


Link to post
Share on other sites
Quote:
Original post by discodowney
1>c:\users\downey\college\main game\testprograms\readfromfil\readfromfile.cpp(40) : error C2057: expected constant expression
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 C2057: expected constant expression
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 C2087: 'adjMatrix' : missing subscript
1>c:\users\downey\college\main game\testprograms\readfromfil\readfromfile.cpp(40) : error C2133: 'adjMatrix' : unknown size


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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this