• Advertisement

• Popular Now

• 9
• 10
• 12
• 12
• 14
• Advertisement
• Advertisement
• Advertisement

Archived

This topic is now archived and is closed to further replies.

one answer to someone question

This topic is 5647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

int main(int argc, char *argv[])
{
long SizeOfString;
FILE* RobotFile;
char* RobotString;
int ErrorNumber;

RobotFile = fopen("robot.txt", "rb");
if (RobotFile == NULL)
{
printf("fopen error\n");
exit(0);
}

/* seeks to the end of the file */
ErrorNumber = fseek(RobotFile, 0, SEEK_END);
if(ErrorNumber != 0)
{
printf("fseek error\n");
exit(0);
}
/* this equals the end of the file */
SizeOfString = ftell(RobotFile);
if(SizeOfString == -1)
{
printf("ftell error");
exit(0);
}
/* extra char for null termination */
RobotString = (char*)malloc(sizeof(char) * (SizeOfString + 1));
if (RobotString == NULL)
{
printf("memory error\n");
exit(0);
}
rewind(RobotFile);
fread(RobotString, sizeof(char), SizeOfString, RobotFile);

/* arrays accesses start at 0, so RobotString[SizeOfString]
* is the extra byte for null termination
*/
RobotString[SizeOfString] = NULL;
printf(RobotString);
printf("\n%ld\n", SizeOfString);
return 0;
}


[edited by - coreyh on November 4, 2002 7:16:55 PM]

Share this post

Share on other sites
Advertisement
There is no question?
#define TRUE 1 looks a little obsolete, that''s one thing.

Share this post

Share on other sites
You might get help faster if you tell us what the problem is, or what it's supposed to do.

[edited by - smart_idiot on November 4, 2002 8:06:30 PM]

Share this post

Share on other sites
Thank you for the post there
It helped me a lot

Share this post

Share on other sites
Now perchance would you now how to set all the values in a string to NULL, so that I would be able to write separate commands (say, GOLFT for the robot to go left, and then another command entirely, say GOFWD, in the same string?) The program will eventually be used to input sets of four numbers (aspects of a robot that I hope to build that will detect light and objects) and using them to determine waht my robot will do.
Basically, If you will, the program will be a testing ground for the intelligence of my bots.

Once again, thank you.

Share this post

Share on other sites
memset(RobotString, 0, SizeOfString);
or
bzero(RobotString, SizeOfString);

Depending on how many commands you have you could just have a byte/char stand
for one command instead of having to parse words in a string. If you needed
more commands then a byte could represent you use more then one for each
command.

FOWARD = 0
LEFT = 1
RIGHT = 2
BACK = 3

There was a old mac game called chipwits that was like your project.
There is a playstation game called carnage heart also. Both would probably
be good practice for programming robots.

http://www.gamefaqs.com/console/psx/data/19966.html

kdIXfA.gamedev.10.coreyh@xoxy.net
www.ipeg.com/~rlfc

Share this post

Share on other sites

• Advertisement