LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 12-13-2007, 11:47 PM   #1
abhisheknayak
LQ Newbie
 
Registered: Nov 2007
Posts: 6

Rep: Reputation: 0
Find multiple new lines char


Hi,
I want extract the text from a text file when the word "Positive" appears until a new line character( '\n') is repeated 3 times in continuation, how do I do it in c++. The problem using peek() is it detects only single character and if I write ( file.peek() =="\n\n\n"). it isn't giving me the required result. Below is the way how I tried.




fstream iF;
iF.open("filename");
char test1[50];


do{
iF>>test1;
if (strcmp(test1,"Positives")== 0)
{
do{
iF.get(ch);
cout<<ch;
}while( iF.peek() != '\n' && iF.peek() != EOF);

//now iF.peek() != "\n\n\n" wont work
}
}while(iF.peek() != EOF);



See the sample file below

/////////////

Score = 58.2 bits (139), Expect = 9e-08
Identities = 27/141 (19%), Positives = 65/141 (45%)

Query: 9 NVGNIVLRLTNIGLKIMKVGPKIPNVRPNVGNIGPIITKVGLTCSNIGPNITNVGLKLTN 68
N + L + +G+ + VG + N+ +G + VG+ S +G + +G+ ++
Sbjct: 431 NASGVGLTASGMGMNMSGVGLTASGMGMNMSGVGLTASGVGMNMSGVGLTASGMGMNMSG 490

Query: 69 LGLKLTNLGLKLTNIGPKIMKVGPQRPNVGPNVGNIGPNITNIGPKVMKVDPEIPNIGPN 128
+GL + +G+ ++ +G +G VG +G N++ +G + + +G
Sbjct: 491 VGLTASGMGMNMSGVGLTASGMGMNMSGVGLTASGMGMNMSGVGLTASGMGMNMSGVGLT 550

Query: 129 VGNIGPKITNLGHKGTSVGLN 149
+G ++ +G + +G+N
Sbjct: 551 ASGMGMNMSGVGLTASGMGMN 571




////////////////
 
Old 12-15-2007, 12:04 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Well ... all you need to do is take count of "\n" as
you encounter it, reset the count to zero if a non-"\n"
follows.


Cheers,
Tink
 
Old 12-15-2007, 04:51 PM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
This seems to work.
Code:
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int
main()
{
   ifstream infile("test.txt");

   if (!infile)
   {
      cerr << "Error opening file test.txt." << endl;

      return 1;
   }

   unsigned short consequtive_newlines_count = 0;
   string line;
   bool positives_string_found = false;

   while (getline(infile, line) && consequtive_newlines_count < 3)
   {
      if (line.find("Positives") != string::npos)
      {
         positives_string_found = true;
      }

      if (positives_string_found)
      {
         cout << line << endl;

         if (line.empty())
         {
            ++consequtive_newlines_count;
         }
         else
         {
            consequtive_newlines_count = 0;
         }
      }
   }
It assumes that three newlines have to be on an otherwise empty line, i.e. three empty lines will break the output. It doesn't count the newline just before an empty line.

For the following file test.txt
Code:
/////////////

Score = 58.2 bits (139), Expect = 9e-08
Identities = 27/141 (19%), Positives = 65/141 (45%)

Query: 9 NVGNIVLRLTNIGLKIMKVGPKIPNVRPNVGNIGPIITKVGLTCSNIGPNITNVGLKLTN 68
N + L + +G+ + VG + N+ +G + VG+ S +G + +G+ ++
Sbjct: 431 NASGVGLTASGMGMNMSGVGLTASGMGMNMSGVGLTASGVGMNMSGVGLTASGMGMNMSG 490

Query: 69 LGLKLTNLGLKLTNIGPKIMKVGPQRPNVGPNVGNIGPNITNIGPKVMKVDPEIPNIGPN 128
+GL + +G+ ++ +G +G VG +G N++ +G + + +G
Sbjct: 491 VGLTASGMGMNMSGVGLTASGMGMNMSGVGLTASGMGMNMSGVGLTASGMGMNMSGVGLT 550

Query: 129 VGNIGPKITNLGHKGTSVGLN 149
+G ++ +G + +G+N
Sbjct: 551 ASGMGMNMSGVGLTASGMGMN 571




////////////////
the output is
Code:
$ ./runme.exe
Identities = 27/141 (19%), Positives = 65/141 (45%)

Query: 9 NVGNIVLRLTNIGLKIMKVGPKIPNVRPNVGNIGPIITKVGLTCSNIGPNITNVGLKLTN 68
N + L + +G+ + VG + N+ +G + VG+ S +G + +G+ ++
Sbjct: 431 NASGVGLTASGMGMNMSGVGLTASGMGMNMSGVGLTASGVGMNMSGVGLTASGMGMNMSG 490

Query: 69 LGLKLTNLGLKLTNIGPKIMKVGPQRPNVGPNVGNIGPNITNIGPKVMKVDPEIPNIGPN 128
+GL + +G+ ++ +G +G VG +G N++ +G + + +G
Sbjct: 491 VGLTASGMGMNMSGVGLTASGMGMNMSGVGLTASGMGMNMSGVGLTASGMGMNMSGVGLT 550

Query: 129 VGNIGPKITNLGHKGTSVGLN 149
+G ++ +G + +G+N
Sbjct: 551 ASGMGMNMSGVGLTASGMGMN 571



$
 
Old 12-17-2007, 03:27 AM   #4
abhisheknayak
LQ Newbie
 
Registered: Nov 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks the problem is fixed
 
  


Reply



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
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
Find Length of char * array in C++ burninGpi Programming 6 08-08-2006 08:53 AM
remove singel char lines Ljohan Programming 4 03-16-2006 04:11 AM
ISDN multiple lines schimmelpilz Linux - Newbie 1 02-24-2004 05:39 PM
Can't find char-major-10 at boot? wop138 Linux - Newbie 4 08-31-2002 01:41 PM

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

All times are GMT -5. The time now is 03:34 PM.

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