Text file parsing

Started by
14 comments, last by tmoneyksu 20 years, 10 months ago
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?
Super-newb extraordinaire.
Advertisement
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.
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.
Super-newb extraordinaire.
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?
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?
Super-newb extraordinaire.
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.
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]
Super-newb extraordinaire.
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]
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan
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?
Super-newb extraordinaire.
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