The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can search a String in one text file and replace the whole line in another file kkraja UNIX for Dummies Questions & Answers 6 08-06-2008 07:23 AM
Search a string and append text after the string kesu2k Shell Programming and Scripting 8 07-18-2008 07:35 PM
search text string itik AIX 2 01-28-2008 07:01 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 08:53 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-16-2009
Zykl0n-B Zykl0n-B is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 25
Search a string in a text file in C

Hello,
I need help with a program i'm coding in C.

What my program has to do is to search a string in a text file, if it finds a match, print the string to stdout.

The file looks like:

000001 California
100000 Texas
011110 Ohio
010101 Washington

So, if the program finds a match, prints "100000 Texas" to stdout.

Could you help me?
Please excuse my poor english.
  #2 (permalink)  
Old 04-16-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Code:
/* srch.c usage: srch filename "string to find"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
	FILE *fp=fopen(argv[1],"r");
	char tmp[256]={0x0};
	while(fp!=NULL && fget(tmp, sizeof(tmp), fp)!=NULL)
	{
		if (strstr(tmp, argv[2])
			printf("%s", tmp);
	}
	if(fp!=NULL) fclose(fp);
	return 0;
}
  #3 (permalink)  
Old 04-16-2009
Zykl0n-B Zykl0n-B is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 25
Thank You!!!, Thanks man,... Thank you...

I corrected 'fgets' and 'if' functions...

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
        FILE *fp=fopen(argv[1],"r");
        char  tmp[256]={0x0};
        while(fp!=NULL && fgets(tmp, sizeof(tmp),fp)!=NULL)
        {
        if (strstr(tmp, argv[2]))
        printf("%s", tmp);
        }
        if(fp!=NULL) fclose(fp);
        return 0;
}
  #4 (permalink)  
Old 04-17-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
I failed typing - twice.
  #5 (permalink)  
Old 04-17-2009
Zykl0n-B Zykl0n-B is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 25
I know
  #6 (permalink)  
Old 07-02-2009
c0d1f1ed c0d1f1ed is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 2
Doesn't this fail when the search string crosses two 'tmp' block?
  #7 (permalink)  
Old 07-02-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
No the search is line-by-line, fgets gets one line at a time. tmp[256] can read in as many as 255 chars. No line in the example text is longer than 40. No issue.

As a general solution, use tmp[LINE_MAX] where LINE_MAX is defined in limits.h Most UNIX tools limit line size to whatever LINE_MAX is for your system.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0