Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Loading an array from a text file

 
Post new topic   Reply to topic Printable version
 View previous topic  "cant enter game" Post :: Post Proximity  View next topic  
Author Message
nintendo64
Seasoned Helper


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 104
Location: Dominican Republic
Offline

PostPosted: Fri Mar 26, 2004 10:04 pm    Post subject: Loading an array from a text file Reply to topic Reply with quote

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
Back to top
View users profile Send private message Add User to Ignore List
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Fri Mar 26, 2004 10:41 pm    Post subject: Reply to topic Reply with quote

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.
_________________
4,691 irradiated haggis!
Back to top
View users profile Send private message Add User to Ignore List
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:41
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3615
Location: Las Vegas
Offline

PostPosted: Sat Mar 27, 2004 12:31 am    Post subject: Reply to topic Reply with quote

Code: Show/Hide
while (!kbhit())

Could I recommend either adding a Sleep to that, or to use a different function, such as _getch
Back to top
View users profile Send private message Add User to Ignore List Send email
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sat Mar 27, 2004 11:35 am    Post subject: Reply to topic Reply with quote

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).
_________________
This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sat Mar 27, 2004 12:21 pm    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List
nintendo64
Seasoned Helper


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 104
Location: Dominican Republic
Offline

PostPosted: Sat Mar 27, 2004 12:46 pm    Post subject: Reply to topic Reply with quote

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
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sat Mar 27, 2004 3:53 pm    Post subject: Reply to topic Reply with quote

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?
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sat Mar 27, 2004 10:24 pm    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Sat Mar 27, 2004 11:49 pm    Post subject: Reply to topic Reply with quote

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.
_________________
Hyperspace Owner

Smong> so long as 99% deaths feel lame it will always be hyperspace to me
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sun Mar 28, 2004 1:29 am    Post subject: Reply to topic Reply with quote

Variables are initialized/modified where they are needed. A for loop control variable is no different than any other variable.
Back to top
View users profile Send private message Add User to Ignore List
Smong
Server Help Squatter


Joined: 1043048991
Posts: 0x91E
Offline

PostPosted: Sun Mar 28, 2004 8:41 am    Post subject: Reply to topic Reply with quote

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?
Back to top
View users profile Send private message Add User to Ignore List Visit posters website MSN Messenger
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sun Mar 28, 2004 9:44 am    Post subject: Reply to topic Reply with quote

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).
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sun Mar 28, 2004 11:34 am    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Jackmn
Newbie


Joined: Apr 02 2004
Posts: 13
Offline

PostPosted: Fri Apr 02, 2004 3:55 pm    Post subject: Reply to topic Reply with quote

One rather obvious mistake:

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

should be

file = fopen("c:\\matrices.txt","r");
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Fri Apr 02, 2004 6:28 pm    Post subject: Reply to topic Reply with quote

Hmm yeah, that should've given a compile error. (No such escape character as \m.) Good eye.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> General Questions All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 351 page(s) served in previous 5 minutes.

phpBB Created this page in 0.521685 seconds : 39 queries executed (91.3%): GZIP compression disabled