Printing array
Author |
Message |
wtd
|
Posted: Tue Nov 15, 2005 3:03 pm Post subject: (No subject) |
|
|
Are you certain that your input file is properly formatted? Turn on visible whitespace in your text editor to check. I know Textpad has this feature. Not sure if other text editors on Windows do.
The reason this might be a problem is that your while loop which reads in the file doesn't do any error checking. It assumes the file will be absolutely perfectly formatted. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
md

|
Posted: Tue Nov 15, 2005 3:04 pm Post subject: (No subject) |
|
|
Tubs wrote: Why can't I edit my posts?
Here is the code for the above output:
c++: |
#include <stdio.h>
int main(int argc, char *argv[])
{
int x = 0, y = 0, k, j;
char cells[25][25], i;
FILE *in, *out; //Pointers to open and close the file
in = fopen( "data.txt", "r" ); // open a file for reading
while( fscanf( in, "%c", &i ) != EOF ) // Read from the file until EOF
{
if (x == 24)
{
cells[x][y] = i;
x = 0;
y += 1;
}
else
{
cells[x][y] = i;
x += 1;
}
}
fclose (in);
for (j = 0; j <= 25; ++j)
{
for (k = 0; k <= 25; ++k)
{
printf ("%c", cells[k][j]);
}
printf ("\n");
}
system ("pause");
return 0;
}
|
** note how much more readable the cleaned up code is...
There are a couple of problems I see, notably when x == 24 you should still have one character left in the array to read; only when x == 25 should you be at a new line. But the question begs to be asked: if you know what you're inputing then why are you using the while loop? Why not use a much simpler method since the data is guarunteed to be correct. For example
c++: |
for(int y = 0; y < 25; y++)
{
for(int x = 0; x < 25; x++)
fscanf( in, "%c", &grid[x][y] );
}
|
Yes... it does absolutely zero error checking, but from what I understand your input is guarunteed anyways. |
|
|
|
|
 |
wtd
|
Posted: Tue Nov 15, 2005 3:39 pm Post subject: (No subject) |
|
|
To post working code or not... that is the dilemna.
It might help you to see working code and see what 'm doing that makes it work well, but then, you might just copy it and claim it as your own without attempting to gain understanding. |
|
|
|
|
 |
Tubs

|
Posted: Tue Nov 15, 2005 5:03 pm Post subject: (No subject) |
|
|
What would be the point of that, if I don't learn it I'm screwed in future courses aren't I  |
|
|
|
|
 |
Tubs

|
Posted: Tue Nov 15, 2005 5:09 pm Post subject: (No subject) |
|
|
The reason it was printing jibberish is because i had <= instead of just <, it works now. Thanks a lot guys  |
|
|
|
|
 |
Hikaru79
|
Posted: Tue Nov 15, 2005 5:58 pm Post subject: (No subject) |
|
|
Tubs wrote: The reason it was printing jibberish is because i had <= instead of just <, it works now. Thanks a lot guys 
...that's what wtd has been telling you since his first post  |
|
|
|
|
 |
Tubs

|
Posted: Fri Nov 18, 2005 12:08 pm Post subject: (No subject) |
|
|
I can't figure out why this code only prints up to the last line of the array! The text file is a 25x25 grid of characters.
code: |
for (y = 0; y < 25; ++y)
{
for (x = 0; x < 25; ++x)
fscanf(in, "%c", &cells[x][y]);
}
fclose (in);
for (j = 0; j < ROWS; ++j)
{
for (k = 0; k < COLS; ++k)
printf("%c", cells[k][j]);
}
|
|
|
|
|
|
 |
Tubs

|
Posted: Fri Nov 18, 2005 12:09 pm Post subject: (No subject) |
|
|
Sorry, ROWS and COLS are both defined as 25. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
md

|
Posted: Fri Nov 18, 2005 4:12 pm Post subject: (No subject) |
|
|
First, if you have access to a debugger check to see if the array is being read properly. If not then that's probably the problem, if it is then I see no reason why it's not working... |
|
|
|
|
 |
|
|