Jump to content
  • Advertisement

Archived

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

gameprogrammerwiz

reading files character by character

This topic is 6938 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

i need to know how to read a .txt file character by character. i have some good code to do this in DOS, but im working on a tilebased game in Win32 mode and i need to know how to do this to read my map files. thanks

Share this post


Link to post
Share on other sites
Advertisement
if you are using fstream, it is really easy:

fstream myfile ("test.txt",ios::in);
char c;
while (myfile.get(c))
// process the character c

if you want to use stdio.h''s FILE, do the following

FILE *myfile = fopen("test.txt", "r");
char Buffer[2];
while (fgets(Buffer, 1, myfile)) // you may need to make it get 2 characters if it puts a \n on the end
// process the character Buffer[0]

If that doesn''t work, try getting a whole line (or 80 chars, whatever) from the FILE and then reading those one at a time before going back for more.


--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

Share this post


Link to post
Share on other sites
Well, with a name like gameprogrammerwiz, you should be able to figure it out. ;-)

Why not just read a line at a time into a buffer, then work with the bytes from the buffer? What you are trying to do is like shopping for groceries by making a separate trip to the store for each item on your list. Just a thought.

aig

Share this post


Link to post
Share on other sites
Hello,

This might be 16 bit, but you might try it this way...
I hope I didn''t make any errors typing it in.
It''s inefficient so cover your eyes!
----------------------------------------------------------
int Load_CharsfromFileexample(void *parms = NULL, int num_parms=0)
{
// reads 36 characters from a file (chars going down)
FILE *fp_file; // working file
int i;
char initials[36];

// try and open file
if ((fp_file = fopen("hiscore.txt","r+t"))==NULL)
{
// create a new file code here
}
else
{
for (i=0;i<36;i++)
// read in the chars (one char per
// line)
fscanf(fp_file,"%c\n",&initials);
// close the file
fclose(fp_file);
}
return(1);
}


Share this post


Link to post
Share on other sites
ok, none of those codes are working. this is what my file looks like:
g
g
g
g
g
g
g

the g''s stand for grass. and this is the code i''m using:

tilex = 0;
tiley = 0;

FILE *readmap = fopen("c:/windows/desktop/mymap.txt","r+t");
int i;
char map;

for (i=0;i<7;i++)
{
fscanf(readmap,"%c\n",&map);

if(map = ''g'')
{
lpDDSPrimary->BltFast( tilex, tiley, lpDDSgrass, NULL,
DDBLTFAST_WAIT / DDBLTFAST_SRCCOLORKEY );
tilex = tilex + 50;
}
}

fclose(readmap);

ok, now, this prints 7 of my tiles on the screen. but, if i change the line: if(map = ''g'') to if(map = ''a'') it STILL prints the 7 grass tiles. it shouldent be doing that. whats wrong here?

Share this post


Link to post
Share on other sites
quote:
Original post by gameprogrammerwiz
...
change the line: if(map = ''g'') to if(map = ''a'') it STILL prints the 7 grass tiles. it shouldent be doing that. whats wrong here?


Hi.
You probably mean to use == not = . I know, I''ve made this mistake often enough

Andrew

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!