I'm trying to create a simple little program that will accept arguments of files (i.e. file1 and file2), and look for occurrences of the "match" string in the included arg files. Then I want to print out the whole line where the match is found preceded by the number of the line. In other words, I want to find "is" in my file args, and have it print "2: block is enough", 2 being the line number, and "block is enough" being the line where "is" is found. I want to use strstr and was thinking that I could use this to do so:

char *strstr(const char *haystack, const char *needle);

I want this to return NULL if the needle string is not found in the haystack string and otherwise return the pointer to where the needle is returned.

I was hoping someone could help me find a way to implement it as I want. All I really need is a small chunk of code in my main to do this, and I'll be able to continue with my program from there. Any help/ideas!!??

Thanks in advance!

Recommended Answers

All 14 Replies

>I want this to return NULL if the needle string is not found
>in the haystack string and otherwise return the pointer to
>where the needle is returned.
You're in luck, that's precisely how strstr works.

>I was hoping someone could help me find a way to implement it as I want.

int line_number = 0;

while ( fgets ( line, sizeof line, in ) != NULL ) {
  ++line_number;

  if ( strstr ( line, "is" ) != NULL ) {
    printf ( "%d: %s", line_number, line );
    fflush ( stdout );
  }
}

Wait, what?! Lol I'm looking to put some code such as that in my main. Is the code you gave me suppose to be the defintion for "char *strstr(const char *haystack, const char *needle)", or what exactly? I'm a little confused.

>Lol I'm looking to put some code such as that in my main.
So what's the problem?

>Is the code you gave me suppose to be the defintion for <snip strstr declaration>
No, it's the code that uses strstr to search a file. That's what you said you wanted, right?

Well, I'm sure it's what I need but I don't really understand how strstr works, so I'm not sure how to use the code you gave me to my likings. Basically I want to be able to enter a file or two on my command line using this program to find whatever keyword I want....Is that code all I need to do that?!? I feel like I need more substance in my main to make it work...? Do you understand what I'm tryin to do? Thanks!

>>I'm sure it's what I need but I don't really understand how strstr works
Very simple -- it searches through the string in the first parameter for the first occurrence of the string in the second parameter. If the first string contains the second string then strstr() will return a pointer to the start of that string. If it is not found then strstr() will return NULL.

Example:

strstr("Hello World", "is"); // return NULL because "Hello World" does not contain the word "is"

strstr("Hello World", "orld"); // will return something other than NULL because "Hello World" does contain the string "orld". Note that this does not have to be an entire word but any sequence of characters.

If you want to use command-line arguments where the first argument is a filename and the seocnd argument is the string you want to find, to modify Narue's code a bit

int line_number = 0;
FILE* in = fopen(argv[1],"r");
while ( fgets ( line, sizeof line, in ) != NULL ) {
  ++line_number;

  if ( strstr ( line, argv[2] ) != NULL ) {
    printf ( "%d: %s", line_number, line );
    fflush ( stdout );
  }
}

>>I feel like I need more substance in my main to make it work
I think you are trying to make a mountain out of a mole hill :)

Ok, I see. That's exactly what I want. So my WHOLE main should simply look like this?!?:

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char **argv)
{
	int line_number = 0;
	FILE* in = fopen(argv[1],"r");
	while ( fgets ( line, sizeof line, in ) != NULL ) {
	  ++line_number;

	  if ( strstr ( line, argv[2] ) != NULL ) {
		printf ( "%d: %s", line_number, line );
		fflush ( stdout );
	  }
	}
}

If so, what do I do about these errors/warnings?:


warning C4996: 'fopen' was declared deprecated
error C2065: 'line' : undeclared identifier
error C2070: ''unknown-type'': illegal sizeof operand
fatal error C1903: unable to recover from previous error(s); stopping compilation

Well...."#define _CRT_SECURE_NO_WARNINGS" didn't work. fopen still comes back deprecated.
And if I define "line" as "char * line", it compiles but I get an error when I try to run it then.

Ahh I'm lost..

>fopen still comes back deprecated.
Ignore the warning then, it's harmless (as long as you consider annoying as hell harmless).

>And if I define "line" as "char * line", it compiles but I get an error when I try to run it then.
Most people don't jump over arrays and go on to pointers. You know how to make an array right?

char line[1024];

Yea I just realized that! haha..Thanks!

I had my arguments type incorrectly also, causing the error after compilation. Sorry

I'm still gettin fopens deprecated though...is that anything to worry about?
Everything runs properly, I just get that as a warning..??

Thanks

>>I'm still gettin fopens deprecated though
That's only a Microsoft thingy -- C and C++ standards make no such claim. You can use a pragma to disable that warning #pragma warning(disable: xxxx) just replace the x's with the warning number.

Where did you put the #define?
It needs to be before any of the include statements.

A better place is to put it in the project settings->compiler->preprocessor (kind of area), where you can create your own #defines which need to be set right at the start.

>I'm still gettin fopens deprecated though...is that anything to worry about?
Didn't I say it's harmless but annoying, and that you can ignore it? Those particular deprecation warnings are just Microsoft being stupid. They want you to use their non-standard "safe" libraries instead of the portable standard libraries.

I'm not gonna worry about it, since you all tell me that it's related to Microsoft. My mistake was that I put the define AFTER the includes, which I'm sure was the problem, but I'm pretty much done with my work in that program, so no biggee. Thanks for all the help though. That's EXACTLY what I needed!!!

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.