| View previous topic :: View next topic |
| Author |
Message |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Sun Jan 18, 2009 9:50 pm Post subject: Reading from a text file and storing in 2-D array |
|
|
Hey, I need some help reading from a text file that has 100 lines. Each line has a bunch of numbers on it.
What i'm trying to do is read from this text file and store each number (which are separated by spaces) into a 2-D array. This is what I have but it fails in immense amounts (don't laugh please )
| Code: | void ReadTriangle()
{
FILE* pFile = fopen( "triangle.txt", "r" );
int buffer[100][100];
for( int i = 0; i < 100; i++ )
{
for( int n = 0; n < 100; n++ )
{
fscanf( pFile, "%i", buffer[i][n] );
}
}
}
int main()
{
ReadTriangle();
getchar();
} |
It obviously doesn't work. Can someone help me?
_________________
Blog
| Quote: | Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that |
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Sun Jan 18, 2009 10:55 pm Post subject: |
|
|
| Code: |
fscanf( pFile, "%i", &buffer[i][n] );
|
It's a common mistake not to use the ampersand with a two dimensional array.
|
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Mon Jan 19, 2009 12:23 am Post subject: |
|
|
Oh ok. Well i tried it and it still doesn't work. Everything compiles fine but as soon as it compiles I get a "Debug Assertion Error!". Know what it means?
_________________
Blog
| Quote: | Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that |
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Mon Jan 19, 2009 12:54 am Post subject: |
|
|
| Only reason I can think of is that the file wasn't found and you're trying to read from something that doesn't exist.
|
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Mon Jan 19, 2009 10:31 am Post subject: |
|
|
So then should putting the txt file in the debug folder when i debug it work?
_________________
Blog
| Quote: | Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that |
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Mon Jan 19, 2009 12:19 pm Post subject: |
|
|
| If you're using visual studio, it will look in projects > project name > project name. That is assuming you're running the program through visual studio. If you're running the program through cmd, it has to be in the same location as the exe. Unless you specify a location like c:\filenamme.txt. Then you'll have to put the file where ever you specified.
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Jan 19, 2009 7:47 pm Post subject: |
|
|
use error checking to see if the file exists or not
_________________
|
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Mon Jan 19, 2009 11:50 pm Post subject: |
|
|
Well I have the text file I want to read from. And I put it in the debug folder. But i still get that message. But if i run the exe not through the compiler then I don't get a message. It's weird
_________________
Blog
| Quote: | Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that |
|
|
| Back to top |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Tue Jan 20, 2009 2:15 am Post subject: |
|
|
| If you run it from the compiler, the exe is run from your project folder, not from the debug/release folders.
|
|
| Back to top |
|
 |
Bizarro I post too much
Reputation: 0
Joined: 01 May 2007 Posts: 2648
|
Posted: Tue Jan 20, 2009 2:33 am Post subject: |
|
|
getlines
then process each line with stringstream
_________________
w8 baby.com Banner contest, Come join NOW!!
Check us out for Prize deatils |
|
| Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Tue Jan 20, 2009 8:39 pm Post subject: |
|
|
| If you use Microsoft Studio, the program is starting in the <project name> folder if you debug it with the compiler. If you want to debug with the compiler, move the .txt file into the <project name> folder or else, move the file to the debug folder and start your project there (as in double-click the .exe instead of going to the compiler and running it)..
|
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Wed Jan 21, 2009 12:18 am Post subject: |
|
|
Yeah thanks I already fixed it. But i need to work on the code because it doesn't seem to be working right
_________________
Blog
| Quote: | Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that |
|
|
| Back to top |
|
 |
|