LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-09-2003, 12:41 PM   #1
alrawab
Member
 
Registered: Dec 2002
Location: Libya
Distribution: RHLE 5 + slackware 12
Posts: 60

Rep: Reputation: 15
grep in c++


hi ......
any one can help me to convert this command to c/c++
cat a.txt | grep "Osa" >b.txt


thnx
 
Old 11-09-2003, 03:25 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Do you want to run this command from within a C/C++ program, or do you want a C/C++ program that will do the same thing as this command does?

Last edited by wapcaplet; 11-09-2003 at 03:26 PM.
 
Old 11-10-2003, 12:13 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Another answer might be:

Code:
#include <stdlib.h>
int main(int argc, char *argv[]){
      shell("cat a.txt | grep 'Osa' >b.txt");
      return 0;
}
In this case, using the commandline is the most efficient approach.
 
Old 11-10-2003, 02:57 PM   #4
alrawab
Member
 
Registered: Dec 2002
Location: Libya
Distribution: RHLE 5 + slackware 12
Posts: 60

Original Poster
Rep: Reputation: 15
rpl

i know how to use the system command
but need to do that with pure c/c++
 
Old 11-10-2003, 03:38 PM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
      FILE *in,
           *out;
      char tmp[256];
      in =fopen("a.txt","r");
      out=fopen("b.txt","w");
      if (in==NULL || out==NULL){
               perror("Error opening input or output file ");
               exit(EXIT_FAILURE);
      }
      
      while(!feof(in)){
             memset(tmp,0x00,sizeof(tmp));
             if(fgets(tmp,255,in)!=NULL){
                  if(strstr(tmp,"Osa")!=NULL){
                     fprintf(out,"%s",tmp);
                  }
             }
      }       
      fclose(in);
      fclose(out);
      return 0;
}
 
Old 11-11-2003, 10:28 AM   #6
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
can i ask out of interest why you need to do this? it looks a lot like a programming assignment to me.
 
Old 11-11-2003, 07:44 PM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
I agree - but it's C code which prolly won't win him too many points.
 
Old 11-12-2003, 01:29 AM   #8
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
if you're looking for some good performance as well as teh ability to add such options as -A or -B easily you should use mmap.
 
  


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
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM
help on grep!!!! sanjith11 Programming 5 01-20-2005 05:43 PM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 01:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

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

All times are GMT -5. The time now is 04:37 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