Jump to content
  • Advertisement
Sign in to follow this  
Alundra

Read one text line without limit correctly

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

Hi,

Using fgets you can read one line correctly but you need to know the max line size possible.

How read one line without limit correctly ? Read each character until '\n' is the only way ?

Thanks

Share this post


Link to post
Share on other sites
Advertisement
I write this kind of code a lot:

  std::ifstream ifs(filename);
  std::string line;
  while (std::getline(ifs, line)) {
    // do something with `line'
  }

Share this post


Link to post
Share on other sites

I missed to mention it's to read from a FILE pointer and output into a custom string class variable, here the actual code :

bool CFile::ReadLine( CString& Data, const UInt32 MaxLineLength )
{
  // Allocate the buffer used to read the line.
  char* Buffer = new char[ MaxLineLength ];

  // Read the line into the buffer.
  if( fgets( Buffer, MaxLineLength, m_File ) != nullptr )
  {
    Data = Buffer;
    delete[] Buffer;
    return true;
  }
  else
  {
    delete[] Buffer;
    return false;
  }
}
Edited by Alundra

Share this post


Link to post
Share on other sites

Stealing Alvaro's code:

 

std::ifstream ifs(filename);
std::string line;
while (std::getline(ifs, line))

{

    if("" == line)

        continue;

// do something with `line'
}

Edited by taby

Share this post


Link to post
Share on other sites

In case you can acquire the length of red string from the function call, you could reliably settle for reading lines of any length, since you could test against the maximum length you provide. But quick glance at the fgets function tells it does not return amount of red characters, and counting them after they are returned is sure an overkill.

Share this post


Link to post
Share on other sites
I think Data=Buffer it is wrong, if you delete Buffer, Data points to a released data. To copy the contents, use memcpy.

Share this post


Link to post
Share on other sites

I think Data=Buffer it is wrong, if you delete Buffer, Data points to a released data. To copy the contents, use memcpy.


data is of a type CString. While I'm not familiar with the type it is reasonable to assume it will do its own copy.

Share this post


Link to post
Share on other sites

Ah ok, I didn't know that it was part of <afx.h>, just checked . 

Edited by Ordnas

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!