Jump to content
  • Advertisement
Sign in to follow this  
Ultra The Vampire

file I/O question

This topic is 4825 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

Advertisement
Are you programming or using a program-creation tool?
If you are programming, what language are you using?
Is your project also using any API or libraries?

Thanks,
Twilight Dragon

Share this post


Link to post
Share on other sites
Kewlness. ;)

Although Irrlicht provides basic file I/O through the IReadFile and IWriteFile interfaces, they don't currently handle text parsing that well. So I'd recommend you use the standard C++ fstream -- in this case ifstream. It might go something like this:

ifstream infile;
infile.open("myfile.txt");
if (!infile.is_open())
{
DisplayError();
return ERROR;
}
char buffer[MY_MAX_BUFFER_SIZE];
infile.ignore(MY_MAX_BUFFER_SIZE, "\n"); //MY_MAX_BUFFER_SIZE can be replaced by another large number, here
infile.ignore(MY_MAX_BUFFER_SIZE, "\n"); //ignore the second line
infile.getline(buffer, MY_MAX_BUFFER_SIZE); //get the third line in the buffer
infile.close();


Hope that helps,
Twilight Dragon

Share this post


Link to post
Share on other sites
uhhhh....i got this error.

-----------------------------------------------------------------------------

------ Build started: Project: ICE_Demo, Configuration: Debug Win32 ------

Compiling...
gs_DemoFakeGame.cpp
c:\coding\irrlicht\ice\ICE_3.0\ICE_3\src\Demo\GameStates\gs_DemoFakeGame.cpp(46) : error C2664: 'std::basic_istream<_Elem,_Traits>::ignore' : cannot convert parameter 2 from 'const char [2]' to 'std::basic_istream<_Elem,_Traits>::int_type'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
and
[
_Elem=char,
_Traits=std::char_traits<char>
]
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
c:\coding\irrlicht\ice\ICE_3.0\ICE_3\src\Demo\GameStates\gs_DemoFakeGame.cpp(47) : error C2664: 'std::basic_istream<_Elem,_Traits>::ignore' : cannot convert parameter 2 from 'const char [2]' to 'std::basic_istream<_Elem,_Traits>::int_type'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
and
[
_Elem=char,
_Traits=std::char_traits<char>
]
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

Build log was saved at "file://c:\coding\irrlicht\ice\Ice_3.0\Ice_3\projects\vc7.net.2003\Debug\BuildLog.htm"
ICE_Demo - 2 error(s), 0 warning(s)


---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped

------------------------------------------------------------------------

Share this post


Link to post
Share on other sites
Oops...try changing the double quotes ("\n") to single quotes ('\n'). Or you could just make it a 13, but it's harder to look at "13" and see "new line".

My bad.

Share this post


Link to post
Share on other sites
Better to do:
ifstream infile;
infile.open("myfile.txt");
if (!infile.is_open())
{
DisplayError();
return ERROR;
}
infile.ignore(numeric_limits< streamsize >::max(), '\n');
infile.ignore(numeric_limits< streamsize >::max(), '\n');
std::string line;
getline(infile, line);
infile.close();

Which doesn't have problems with long lines and gets the data directly into a string, which is easier to deal with than char arrays.

Enigma

Share this post


Link to post
Share on other sites
What I've done, because I tend to timestamp my files and add a copyright header, is add a method to ifstream called 'skiplines(n)'. It looks something like this:


void IO<ifstream>::skiplines(intu n) // intu is unsigned integer
{
for ( intu i = 0; i < n; ++i )
{
ignore(numeric_limits<int>::max(), '\n');
}
}





The above method uses a template specialization to accomplish the task, you could extend by just using simple inheritance, by declaring a new class, say 'Input', that inherits ifstream.

In this way, you can use this in your code as:


Input in(my_filename); // Input is an ifstream with a skiplines method.
in.skiplines(3); // skips 3 lines.





Hope that gives you some ideas...

--random

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!