LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2005, 04:24 AM   #1
Hady
Member
 
Registered: Nov 2003
Posts: 55

Rep: Reputation: 15
C++ question: strlen error!?


Hi!

I have a text file, I want to check the max length of the lines.
I get an error in the middle of the file.
Does strlen works even for very large strings (size > 30000)

Code:
const int textLineSize = 30000;
char theLine[textLineSize] = "";

ifstream textFile;
textFile.open("text.txt", ios::in);


while (!textFile.eof())
{
	textFile.getline(theLine, textLineSize);
	
	length = strlen(theLine);

	if (length > maxLength)
		maxLength = length;
}
This stops in the middle of file... any size limit for getline or strlen?

Any suggestions?!

Thanks for your time.
 
Old 03-16-2005, 04:37 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Don't loop on that condition because it won't work as you expect. And since you're using C++, use std::string instead of a char array.

Code:
std::ifstream textfile("text.txt"); /* No need to specify std::ios::in for an ifstream. */

std::string line;
std::size_t max_length = 0;

/* Use the call to getline as the loop condition. It will fail (and the loop break) when EOF is encountered. */
while(getline(file, line))
{
   if(line.length() > max_length())
      max_length = line.length();
}

std::cout << "Longest line was " << max_length << " characters." << std::endl;
 
Old 03-16-2005, 05:08 AM   #3
Hady
Member
 
Registered: Nov 2003
Posts: 55

Original Poster
Rep: Reputation: 15
Thanks!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strlen() function problem in C++ sajith Programming 9 01-16-2006 09:14 PM
strlen problem salahuddin_66 Programming 2 11-13-2005 04:21 AM
confused about strlen() mcd Programming 19 02-26-2005 08:04 PM
GAIM error question errorduck Linux - Newbie 3 06-08-2004 01:35 PM
question about auth error digsby0007 Linux - Software 0 07-09-2003 11:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration