Server Help

General Questions - Loading an array from a text file

nintendo64 - Fri Mar 26, 2004 10:04 pm
Post subject: Loading an array from a text file
My knowledge of C/C++ is not very much advanced, i stil don't know I/O in C/C++.

can someone help me in this ?

this is what i got
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];
      }
   }
}


the file content is

333
444
555
Mr Ekted - Fri Mar 26, 2004 10:41 pm
Post subject:
Your file contains 3 numbers, but you want them broken out as single digits? There's lots wrong with your code, but I need more info on the results you want to help more.
Mine GO BOOM - Sat Mar 27, 2004 12:31 am
Post subject:
Code: Show/Hide
while (!kbhit())

Could I recommend either adding a Sleep to that, or to use a different function, such as _getch
Cyan~Fire - Sat Mar 27, 2004 11:35 am
Post subject:
If you want your numbers stored as single digits, I wouldn't recommend the use of fgets(). Instead, I would use fgetc() and store the characters right into the matrices. But since ASCII character codes do not equal the actual numbers, you need to subtract '0' from each ASCII number that you get.
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
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'
}

Keep in mind that that code will screw up if the file is not in the exact format you described (ie: extra spaces, extra newlines, etc).
Mr Ekted - Sat Mar 27, 2004 12:21 pm
Post subject:
Cyan~Fire wrote:
It is also considered better practice to declare the variables used in for loops inside the for declarator.


I can't stress enough how much I disagree with that.
nintendo64 - Sat Mar 27, 2004 12:46 pm
Post subject:
Thx all for the help, i was just helping a friend with some applications in C/C++, he is studying mechanical engineering and he had some Computer Algorithms class using Borland C++ 6, and coding them in console apps.

The problem was his teacher didn't teach him most of the fundamentals he needed, they didn't taught him how to use Windows APIs or what they were(i know it's just simple include and use), how to read/write files, how to use properly for/while, and etc... and they wanted him to make for the midterm five projects, i don't know how the teacher expect him to do all that, if he doens't even understand properly the basics.

a Hangman Game
a Mastermind Game
a Clerk's Machine (the machine when you pay and give you your devolution?)
a Multiplication of matrixes (loading them from a file and then saving them to a file)
a Counter of letters (load a file then count letters from A-Z, spaces, including special characters like "á,é,í,ó,ú,ñ")

i helped him made mastermind, hangman and the multiplication of matrixes, the problem is i didn't know how to I/O in C/C++, tends my crappy looking code icon_smile.gif.

Well he just delivered his projects today, thanks for the help.

-nintendo64
Cyan~Fire - Sat Mar 27, 2004 3:53 pm
Post subject:
Ekted wrote:
I can't stress enough how much I disagree with that.

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 - Sat Mar 27, 2004 10:24 pm
Post subject:
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?


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.
Dr Brain - Sat Mar 27, 2004 11:49 pm
Post subject:
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.


So instead you look around in a 4 page 3-nested for look looking for what the HECK count does. You see where it's declared, but is it ever initialized? I agree with you on the type thing, but you will have to come up with a more convincing argument for the for loop declaration.
Mr Ekted - Sun Mar 28, 2004 1:29 am
Post subject:
Variables are initialized/modified where they are needed. A for loop control variable is no different than any other variable.
Smong - Sun Mar 28, 2004 8:41 am
Post subject:
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
for (int y = 0; y < 3; y++)
{
   ...
}
Does y get destroyed after the for()? Or can it still be used later on without declaring it again?

What is this thing about 'scope'? Surely a function should be be short, then the scope would be small anyway.

OT:
I notice some people do
Code: Show/Hide
fprintf(stdout, "Error: can't open file.\n");
instead of
Code: Show/Hide
printf("Error: can't open file.\n");
When should each be used?
Mr Ekted - Sun Mar 28, 2004 9:44 am
Post subject:
The handling of scope of declarations inside for statements depends on the compiler. Microsoft scopes the variable local to the function since it is technically outside the braces (the normal scope delimiters). The "standard" defines the scope as inside the for statement. So if you have:

Code: Show/Hide
for (int i = 0; i < 100; i++)
   ;

for (int i = 0; i < 100; i++)
   ;


Under MS this is a compile-time error ('i' already declared).
Cyan~Fire - Sun Mar 28, 2004 11:34 am
Post subject:
Yeah I hate MS.

About the loop variable type thing, if the coder re-declares a variable with the same name but different type, that's just bad programming. Saying "Write good code so you don't write bad code" is a little redundant.

About printf() and fprintf(): The people who use fprintf(stdout, ...) either don't realize that printf(...) is the same thing or they think forintf() is somehow cleaner.
Jackmn - Fri Apr 02, 2004 3:55 pm
Post subject:
One rather obvious mistake:

file = fopen("c:\matrices.txt","r");

should be

file = fopen("c:\\matrices.txt","r");
Cyan~Fire - Fri Apr 02, 2004 6:28 pm
Post subject:
Hmm yeah, that should've given a compile error. (No such escape character as \m.) Good eye.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group