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 > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
finding string at runtime inderpunj Shell Programming and Scripting 1 05-01-2008 03:19 AM
help with finding a string sandyg Shell Programming and Scripting 2 09-28-2007 10:15 AM
Finding funny charaters in a string ahjaeasedqa UNIX for Dummies Questions & Answers 4 04-02-2007 02:05 AM
Finding part of a string DAFNIX Shell Programming and Scripting 4 09-20-2006 11:24 PM
Finding a certain string on each line in a file VODAFUN Shell Programming and Scripting 5 10-07-2005 05:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-24-2008
Ume1986 Ume1986 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 1
Exclamation finding a string in a file

hi all..
I dont know how to search for a string in a file.. I have tried doing.. I did google but didnt get effective answers..my code is as follows:


int search(char* filename,const char*

username,const char* passwd)
{
int flag=0;
unsigned long fsize=0;
unsigned long current=0;
int fdsearch=-1;
int cun,cpw;
char *user,*pass;
int un,pw;
pw=strlen(passwd);
un=strlen(username);
fdsearch=open(filename,O_RDONLY);
fsize = lseek(fdsearch, 0L, 2);
user=(char*)malloc(un);
pass=(char*)malloc(pw);


while (current<fsize)
{
lseek(fdsearch,current,SEEK_SET);
cun=read(fdsearch,user,un);
cpw=read(fdsearch,pass,pw);

if(!memcmp(username,user,un))
{
if(!memcmp(passwd,pass,pw))
{
flag=1;//found
close(fdsearch);
return(flag);
}
printf("incorrect password");
close(fdsearch);
return(flag);
}
//increment current position by 1
current++;
}//while
return flag;
}

I dont know how to read end of string(space will be the delimiter in the file),then end of line.
i guess the bolded areas are where I am doing mistakes..
kindly help me...
thanks in advance
  #2 (permalink)  
Old 01-24-2008
quintet quintet is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 94
i think the code works perfectly fine...

i think what u r seraching for is passed username and password in a file (they shld occur consecutively).

If not let us know your requirement and the sample data file
  #3 (permalink)  
Old 01-24-2008
ennstate ennstate is offline
Registered User
  
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Quote:
Originally Posted by Ume1986 View Post
hi all..
I dont know how to search for a string in a file.. I have tried doing.. I did google but didnt get effective answers..my code is as follows:


int search(char* filename,const char*

username,const char* passwd)
{
int flag=0;
unsigned long fsize=0;
unsigned long current=0;
int fdsearch=-1;
int cun,cpw;
char *user,*pass;
int un,pw;
pw=strlen(passwd);
un=strlen(username);
fdsearch=open(filename,O_RDONLY);
fsize = lseek(fdsearch, 0L, 2);
user=(char*)malloc(un);
pass=(char*)malloc(pw);


while (current<fsize)
{
lseek(fdsearch,current,SEEK_SET);
cun=read(fdsearch,user,un);
lseek(fdsearch, cun+1, SEEK_SET);
cpw=read(fdsearch,pass,pw);

if(!memcmp(username,user,un))
{
if(!memcmp(passwd,pass,pw))
{
flag=1;//found
close(fdsearch);
return(flag);
}
printf("incorrect password");
close(fdsearch);
return(flag);
}
//increment current position by 1
current++;
}//while
return flag;
}

I dont know how to read end of string(space will be the delimiter in the file),then end of line.
i guess the bolded areas are where I am doing mistakes..
kindly help me...
thanks in advance
Does you input file contains only one username/password pair or mutliple entries?

Thanks
Nagarajan G
Closed Thread

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 12:04 PM.


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