Archived

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

werdy666

c++ File I/O

Recommended Posts

werdy666    743
i am having afew problems trying to read a text file into my program. I''m trying to read in an ase file, however using fstream''s getline, it is reading the tabbed spaces into my buffer. Looking at acouple of ase loader examples has been no good cause they seem to only use C File I/O. i did work it out eventually by actually just pressing tab in the compare string.

	while(memcmp( line, "	*MESH",5) != 0) 
		{
		openfile.getline(line,255);
		}
 
is this normal for c++ File I/O or am i missing something??? It seems easier to read in text files such as the ASE format in C and not in C++. Any comments or advice would be appreciated. Werdy666

Share this post


Link to post
Share on other sites
Kuladus    380
i personally have not used getline before, i just read in strings/numbers in one at a time...

specifically for the *mesh example...


  

string temp;
...
openfile >> temp;
if (temp=="*MESH")
{

// do stuff...


}



it will automatically skip spaces, tabs and newlines, if u open it in txt mode that is.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
what about Readfile(), writefile() and those associated functions??

msdn.microsoft.com

Share this post


Link to post
Share on other sites
Xai    1848
I think you are slightly confused ... when you call getline() you tell it what delimiter you want to use ... so if you have a file format where every line is 3 tab delimited columns and then an end of line ... you would think of your parser like this


  
// read in the entire file

while(openfile)
{
// for each line in the file

openfile.getline(column1,''\t''); // reads till first tab

openfile.getline(column2,''\t''); // reads till second tab

openfile.getline(column3,''\n''); // read till end of line

}


obviously, I don''t know the ASE format, so I can''t give you specific code ... and the above will get more compilated, when the file has variable numbers of columns, etc ...

I''m mainly just trying to show how getline is meant to be used

Share this post


Link to post
Share on other sites
Xai    1848
I think you are slightly confused ... when you call getline() you tell it what delimiter you want to use ... so if you have a file format where every line is 3 tab delimited columns and then an end of line ... you would think of your parser like this


  
// read in the entire file

while(openfile)
{
// for each line in the file

openfile.getline(column1,''\t''); // reads till first tab

openfile.getline(column2,''\t''); // reads till second tab

openfile.getline(column3,''\n''); // read till end of line

}


obviously, I don''t know the ASE format, so I can''t give you specific code ... and the above will get more compilated, when the file has variable numbers of columns, etc ...

I''m mainly just trying to show how getline is meant to be used

Share this post


Link to post
Share on other sites