Text file parsing
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?
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.
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.
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.
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?
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?
Okay, here''s what I have so far:
I''ve read the whole thing into the buffer, but how do I go about rooting around the text stored in the buffer?
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?
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.
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.
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]
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]
Uhm... if I didn't misread the original post, I think he was asking for C++ file-i/o. FILE etc is the C way to do it. In C++ you use streams.
Grabbed the first tutorial I Googled: http://cplus.about.com/library/weekly/aa022302a.htm
[edited by - Jedyte on June 5, 2003 11:05:22 AM]
Grabbed the first tutorial I Googled: http://cplus.about.com/library/weekly/aa022302a.htm
[edited by - Jedyte on June 5, 2003 11:05:22 AM]
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?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement