hay i am reading strings from a file and I have to store the first six char into string array. I used this to get the file and the first six char input.

while (getline(inFile, list ))
{
line.substr(0, 6)
}

there are also some blank lines for the first six char, there i used an If statement.

how can i store the first six char that is not empty?

Recommended Answers

All 2 Replies

You need to trim the string accepted from the file. Use something like this:

#include <iostream.h> 
 #include <string> 
 
using namespace std; 
 
string trimLeft(const string &value) 
  { 
   string::size_type where = value.find_first_not_of(' '); 
 
  if (where == string::npos) 
    // string has nothing but space 
    return string(); 
 
  if (where == 0) 
    // string has no leading space, don't copy its contents (assuming COW 
 strings) 
    return value; 
 
  return value.substr(where); 
  }

Please don't start a second thread for the same problems discussed in a prior thread.

while (getline(inFile, list ))
  line.substr(0, 6)

Where/how is the return value of substr() stored in an array of strings? If you read the file using this code then the array of strings representing the list of first 6 char of each string read will be empty.

If the file can have empty lines then you need to be able to deal with that.

If the string has leading spaces that you want to ignore, then you need to trim them, as indicated by S.O.S.

As a side note, probably a typo, you need a semicolon at the end of the second line of the code you posted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.