0

Help please.. I need some help in C to open a file and search for a string. If the string is found then move one space and copy the next string to end-of-line back to buffer. I am creating a program using pthreads and need to search this file in a thread. Just a simple code if possible would help. Thanks.

3
Contributors
12
Replies
14
Views
6 Years
Discussion Span
Last Post by jeffhorton
Featured Replies
  • I now have the program working and is should work. Thanks for all the help and leading me in the right direction... Jeff Horton Read More

0

I have already found them since my post.. Thanks...

these functions will be your friends:

fopen()
strstr()
strcpy()
fclose()

look 'em up.

0

Ok.. Still not working. I am trying to create this so I can convert it to a function in a pthread. I have all other aspects of my main program running but can get this to run correctly. What is suppose to happen is when is receives the information, it looks in a file and finds the information. Then it returns the next information. I am creating a client/server program for dns lookup. My dnsfile looks like: www.abc.com 10.100.100.1
www.123.edu 10.101.101.1
(and so on)..
I need to get it to see the url and return the ip address. Code is below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
 char abc[30];
 char tur[30];
 char *strstr(const char *abc, const char *tur);
 FILE *f;
 int i;

 i = 0;
 f = fopen("dnsfile", "r");
 printf("Finding IP Address for URL: \n");
 scanf("%d", &abc[30]);
 if ( f != NULL)
 {
     while (fgets(tur, 29, f) != NULL)
     {
     if (strstr(abc, tur) != 0)
     {
       fgets(tur, 30, f);
       printf("Your IP Address is: %s", tur);
     }
     }
 }
 fclose(f);
 return (0);
}

Edited by WaltP: Added CODE Tags

0

What two things are wrong with this line scanf("%d", &abc[30]); ???

hint for 1: you have defined abc as a character array, didn't you?

You need to study the printf() function closer...

0

I have changed a few things around. Still not completely working.

Updated code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
 char abc[30];
 char tur[30],rut[30];
 char *strstr(const char *abc, const char *tur);
 FILE *f;
 int c=0;

 f = fopen("dnsfile", "r");
 printf("Finding IP Address for URL: \n");
 fgets(abc, 30, stdin);
 if ( f != NULL)
 {

    do
       {
          fscanf(f, "%s\n", tur);
          if (strstr(abc,tur) == 0)
             {
                printf("match found");
                fscanf(f, "%s\n", &tur);
                printf(" %s\n",tur);
             }
          c=c++;
       }
       while (c < 10);
}

 fclose(f);
 return (0);
}

What two things are wrong with this line scanf("%d", &abc[30]); ???

hint for 1: you have defined abc as a character array, didn't you?

You need to study the printf() function closer...

-1

FYI... printf will go away once I have code working. I will be passing the string.

I have changed a few things around. Still not completely working.

Updated code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
 char abc[30];
 char tur[30],rut[30];
 char *strstr(const char *abc, const char *tur);
 FILE *f;
 int c=0;

 f = fopen("dnsfile", "r");
 printf("Finding IP Address for URL: \n");
 fgets(abc, 30, stdin);
 if ( f != NULL)
 {

    do
       {
          fscanf(f, "%s\n", tur);
          if (strstr(abc,tur) == 0)
             {
                printf("match found");
                fscanf(f, "%s\n", &tur);
                printf(" %s\n",tur);
             }
          c=c++;
       }
       while (c < 10);
}

 fclose(f);
 return (0);
}
Votes + Comments
Was there a need to quote your other post?
0

I have changed a few things around. Still not completely working.

Maybe you're using the wrong logic.

If you want to ask a question, ask a detailed question and you'll get help.

If you want us to say "Awww, poor program :'(" post a statement like you did with no indication of what's not working.

0

Sorry about not posting the right question but not an everyday programmer here..

What I can't get to work is comparing the "abc" string to the "tur" string. I have tried the "strcmp" and "strstr" which should return a 0 if matching or found. I use the "printf" just to see what is in "abc" and "tur".. Any suggestions on comparing two strings...

0

Did you remove the trailing '\n' from your input string using fgets() ? It's always good to examine the data read to see if there are anomalies in your console input. There usually are... strstr() is used to search a string. It looks like you simply need strcmp() .

Edited by WaltP: n/a

1

I now have the program working and is should work. Thanks for all the help and leading me in the right direction...

Jeff Horton

Votes + Comments
you're welcome
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.