reading files character by character
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
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;
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;
Visit the ROAD Programming Website for more programming help.
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
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
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);
}
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);
}
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?
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?
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement