You are here:

C++/C++>>Read certain text from a text file

Advertisement


Question
QUESTION: HI expert Zlatko,

How can i read a certain range of word from a text file which contain 100+ of lines.
eg, i would like file this few lines from a text file
Chain: c1
Scan In: datai[0]
Scan Out: tdo
Length: 984

and output it as
ScanChain "c0" {
ScanLength 984;
ScanIn "tms";
ScanOut "crcerror";

Thanks.
Regards,
Terrance

ANSWER: Hello Terrance

I can show you how to read the lines of the file. I do not know how to get from the input to the output because I don't know the meaning of your input and output. The program below opens a file called infile.txt and displays the lines. For each line it breaks the line down into words and prints the words. That should show you how to get at your file.

The program will need to read each line from the start of the file until it finds the lines it needs. This is called sequential access. If will need to access the data often, you should store it in some kind of data structure that will give you faster access.

Best regards
Zlatko


#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

/*
function trim. Trims whitespace from the start and end of a word.
*/
void trim(std::string& word)
{
   word.erase(word.find_last_not_of(" \n\r\t")+1);
   word.erase(0, word.find_first_not_of(" \n\r\t"));
}

int main(void)
{
   ifstream in("infile.txt");
   while(in)
   {
       std::string line;
       getline(in, line);
       if (in)
       {
         cout << "Read line: '" << line << "'\n";


         stringstream ss(line);
         cout << "Parsed line into these words\n";
         while(ss)
         {
         string word;

         /*
         You can use getline here with ':'
         as the character to split on
         or you can use
         ss >> word;
         to break split the line on whitespace
         If you use ss >> word, you don't need to call trim
         */
         getline(ss, word, ':');

         if (ss)
         {
         trim(word);
         cout << "\t" << word << endl;
         }
         }
         cout << "-----------------------------\n";
       }
   }

}

---------- FOLLOW-UP ----------

QUESTION: why this occur when i execute the file? for the line with (ss)
error C2451: conditional expression of type 'class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> >' is illegal

Answer
Hello Terrance

I don't know why you are getting that error. I cannot get the error. However, instead of using while(ss), try using while(ss.good()). With that you don't even need the if(ss) line. It makes the code shorter and clearer. Try this:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

/*
function trim. Trims whitespace from the start and end of a word.
*/
void trim(string& word)
{
   word.erase(word.find_last_not_of(" \n\r\t")+1);
   word.erase(0, word.find_first_not_of(" \n\r\t"));
}

int main(void)
{
   ifstream in("infile.txt");
   while(in.good())
   {
       string line;
       getline(in, line);
       cout << "Read line: '" << line << "'\n";


       stringstream ss(line);
       cout << "Parsed line into these words\n";
       while(ss.good())
       {
         string word;

         /*
         You can use getline here with ':'
         as the character to split on
         or you can use
         ss >> word;
         to break split the line on whitespace
         If you use ss >> word, you don't need to call trim
         */
         getline(ss, word, ':');

         trim(word);
         cout << "\t'" << word << "'\n";
       }
       cout << "-----------------------------\n";
   }

}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.