Sign in to follow this  

Read one text line without limit correctly

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

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.


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
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.

Yes, all is handled by the custom class.

 

This is a nonstandard function, but you didn't say what platform you're on, so...

Cross-Platform is needed, but I didn't think a platform test was needed for this specific case.

Edited by Alundra

Share this post


Link to post
Share on other sites


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 ?

If you're reading from a C standard library FILE object, yes, that's the only portable, reliable way.  Use the fgetc() function to read one character at a time until it returns EOF or static_cast<int>('\n').  Don't worry, a C FILE object performs buffered input, so it's not actually inefficient.  The fgets() function is probably implemented that way underneath.

Share this post


Link to post
Share on other sites

This topic is 679 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this