Jump to content
  • Advertisement

Archived

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

tmoneyksu

Text file parsing

This topic is 5496 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 open a text file (an .asc file actually) and parse out the vertex data and index data. However, I have zero experience with c++ file i/o or tokenizing. Could someone offer some insight, or offer a link to a good tutorial?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Check this site out:
http://www.opengroup.org/onlinepubs/007908799/xsh/stdio.h.html

There I hope that helps a little you can also try using iostream.h just search for it on google.

Share this post


Link to post
Share on other sites
While that''s a valuable reference, and I appreciate the input, I could use something a little more newbie-oriented. I hate to be picky, but sample code and a good explanation is what I had in mind.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok no sweat...check these two out:

http://www.cprogramming.com/tutorial/lesson10.html
http://www.cpp-home.com/FileIO_tutorial.php

Search for "file I/O tutorial" or something like that on google, you might find something there...Though the only page I found useful for file I/O was the first one I gave you. Anybody else got links to better tutorials?

Share this post


Link to post
Share on other sites
Okay, here''s what I have so far:


int loadASC(char *path) //load ASC model data

{
FILE *pASC;
int filesize;
char *buffer;

int vindex;

pASC = fopen(path, "rb");
if (pASC == NULL)
return 0;

fseek(pASC, 0, SEEK_END);
filesize = ftell(pASC);
fseek(pASC, 0, SEEK_SET);

buffer = (char*)malloc(filesize + 1);
fread(buffer, sizeof(char), filesize, pASC);
}


I''ve read the whole thing into the buffer, but how do I go about rooting around the text stored in the buffer?

Share this post


Link to post
Share on other sites
You''re reading that file in binary mode, not ASCII mode. You probably want to replace

pASC = fopen(path, "rb");
with
pASC = fopen(path, "r");

After you''ve read in the file (and doing it all at once isn''t the best idea since it might require a lot of memory for large files) the first character is in buffer[0], the second in buffer[1], and so on. So, determine the first character or substring you wish to find in buffer and use the string functions to find a pointer to that point in buffer. For example, say you want to find the first occurance of ''vertex'' in the buffer. You''d use the strstr function:

char* ptr;
ptr = strstr(buffer, "vertex");

Now, ptr point to the first occurance of the string ''vertex'' in buffer. ptr[0] = ''v'', ptr[1] = ''e'' and so on. If you want to look for the first character, such as an asterisk, use strchr.

ptr = strchr(buffer, ''*'');

And work through the buffer like that. I find it easier to parse a file as you read it in instead of parsing a buffer in memory, but that might just be me.

Share this post


Link to post
Share on other sites
So THAT's what the "rb" is! Thanks a lot guys!

I agree it would be easier to parse through the text as it's read in, but this will eventually be adapted to read binary model data rather than ascii model data. I understand where you're coming from though.

[edited because it included a stupid question that was already answered]

[edited by - tmoneyksu on June 5, 2003 10:57:53 AM]

[edited by - tmoneyksu on June 5, 2003 11:03:43 AM]

Share this post


Link to post
Share on other sites
I said c++, but c works. As long as it reads, it''s fine for me. Now that I can scan throught the file, how do I convert chars to integers and floats?

Share this post


Link to post
Share on other sites
quote:
Original post by tmoneyksu
I said c++, but c works. As long as it reads, it''s fine for me. Now that I can scan throught the file, how do I convert chars to integers and floats?


use atoi and atof respectively. Just pass them the address of the first character in the string that represents the number. The functions will return an int or a float.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!