Code: Show/Hide int matriz1[3][3];
int matriz2[3][3]; int matriz3[3][3]; int main() { int x; int y; char c[10]; char c2[10]; char c3[10]; FILE *file; file = fopen("c:\matrices.txt","r"); if(file==NULL) { printf("Error: can't open file.\n"); while (!kbhit()) { } return 1; } for(y=0; y<3; y++) { for(x=0; x<3; x++) { if (fgets(c, 10, file)!=NULL) { matriz1[x][y]=c[x]; } } } |
Code: Show/Hide while (!kbhit()) |
Code: Show/Hide for (int y = 0; y < 3; y++)
{ for (int x = 0; x < 3; x++) matriz1[y][x] = fgetc(file) - '0'; fgetc(file); //ignore that '\n' } |
Cyan~Fire wrote: |
It is also considered better practice to declare the variables used in for loops inside the for declarator. |
Ekted wrote: |
I can't stress enough how much I disagree with that. |
Cyan~Fire wrote: |
So you say loop variables should be in-scope through the entire function, even if they're not ever going to be used again? Why? |
Mr Ekted wrote: |
[..]
When you are reading a function, you should only have to look in one place to find all local variables: the top. You could be inside a 4 page 3-nested for loop looking at the variable 'count'. You look at the top--not there. Maybe it's the outside for loop counter--not there. Maybe the next level in--not there. Etc. Having to search for where a variable is declared is bad. And allowing variables to be declared at different nesting levels allows for the possibility that the same variable can assume different types within the same function. This is very bad. |
Cyan~Fire wrote: | |
It is also considered better practice to declare the variables used in for loops inside the for declarator (right word?). Here's an example combining the two:
|
Code: Show/Hide fprintf(stdout, "Error: can't open file.\n"); |
Code: Show/Hide printf("Error: can't open file.\n"); |
Code: Show/Hide for (int i = 0; i < 100; i++)
; for (int i = 0; i < 100; i++) ; |