Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

How to find out the length of the string?

I define a string:
string line;

I read the string from a file:
ifstream fin("abc.txt");
while (getline(fin,line)) {

I check for keyword "Failed" at position 78
if (line.substr(78,6) == "Failed") {

etc
etc
}
}
fin.close();

Now, the problem: sometimes, the string is shorter than 78 characters, and the program aborted.

Please tell me how to find out the string length. I tried strlen, but strlen can only be used for string character.

Please show me your script.

Thanks.
0
ccjong
Asked:
ccjong
  • 2
  • 2
2 Solutions
 
Sys_ProgCommented:
Hi ccjong,
line.size() would give u the lenngth

Cheers!
0
 
AxterCommented:
Just to add to Sys_Prog,

You can modify your condition statement so it first checks the size before checking substr.
If size condition fails, it will not check substr condition.
if (line.size() > 98 &&line.substr(78,6) == "Failed") {
0
 
AxterCommented:
Correction:
//78+6=84 (so size should be at least 84)
if (line.size() > 83 && line.substr(78,6) == "Failed") {
0
 
ccjongAuthor Commented:
Thanks everyone.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now