reading files character by character

Started by
6 comments, last by gameprogrammerwiz 23 years, 11 months ago
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
==============================
whats a signature?
htm[s]l[/s]
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.

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

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

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
aig
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);
}


"If you build it, it will crash."
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?
==============================
whats a signature?
htm[s]l[/s]

...check your comparison ''=='' vs. ''=''

__________________________________________

Yeah, sure... we are laughing WITH you ...
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
hey, thanks everyone! i (with all of the expert help) got it to work.
==============================
whats a signature?
htm[s]l[/s]

This topic is closed to new replies.

Advertisement