finding a string in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users finding a string in a file
# 1  
Old 01-24-2008
Error 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 advanceSmilie
# 2  
Old 01-24-2008
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  
Old 01-24-2008
Quote:
Originally Posted by Ume1986
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 advanceSmilie
Does you input file contains only one username/password pair or mutliple entries?

Thanks
Nagarajan G
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Finding specific string in file and storing in another file

Text in input file is like this <title> <band height="21" isSplitAllowed="true" > <staticText> <reportElement x="1" y="1" width="313" height="20" key="staticText-1"/> <box></box> <textElement> <font fontName="Arial" pdfFontName="Helvetica-Bold"... (4 Replies)
Discussion started by: aankita30
4 Replies

2. Shell Programming and Scripting

Finding a string in a list of files, print file names

I'm interested in writing a report script using BASH that searches all of the files in a particular directory for a keyword and printing a list of files containing this string... In fact this reporting script would have searches for multiple keywords, so I'm interested in making multiple... (2 Replies)
Discussion started by: chemscripter904
2 Replies

3. Shell Programming and Scripting

Finding a string in a file

Hello All, I have a text file, i want to search for a string in it and the string is repeated multiple times in a file i want to get the first occurence of the string in a variable. the content of file is like: I want to grepthe first occurance of "Configuration flow done" and store the... (7 Replies)
Discussion started by: anand2308
7 Replies

4. UNIX for Dummies Questions & Answers

Finding files with a certain name string inside of another file.

Hi, I have a very large file that contains a listing of all files on the system. I need to create a listing from that file of all files that start with the following format: s???_*, whereas the '?' represents characters, so the file name begins with an 's' followed by three other characters and... (4 Replies)
Discussion started by: tes218
4 Replies

5. Shell Programming and Scripting

Reformatting single column text file starting new line when finding particular string

Hi, I have a single colum file and I need to reformat the file so that it creates a new line every time it come to an IP address and the following lines are corresponding rows until it comes to the next IP address. I want to turn this 172.xx.xx.xx gwpusprdrp02_pv seinwnprd03... (7 Replies)
Discussion started by: kieranfoley
7 Replies

6. Shell Programming and Scripting

mappin strings of two different file and finding the mapped string and then map other fields.

As i am new to unix so facing some problems in scripting: here is my question: i m having two files. 1st file say a.txt contain 3 column like SPECIALITY|UMP_CODE|SPECIALTY_CODE Addictive Diseases|25ADD|ADD Addictive Diseases/Family Practice|25ADD|ADD/FP Aerospace Medicine|1.041666667|AM... (4 Replies)
Discussion started by: dsh007
4 Replies

7. UNIX for Dummies Questions & Answers

finding string in very long file without newlines

What's the best way to find a string in a very long file without newlines in Unix? The standard utility I'm aware of for finding a string in a single file is grep, but for a long file without newlines, I think the output is just going to be the input. I suppose I could use sed to replace the... (5 Replies)
Discussion started by: aaronpoley
5 Replies

8. Shell Programming and Scripting

Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2): searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found. In this case, it's known that there... (13 Replies)
Discussion started by: busdude
13 Replies

9. Shell Programming and Scripting

Need help with finding unique string in log file

Shell script help Here is 3 sample lines from a log file <date> INFO <java.com.blah> abcd:ID= user login <date> DEBUG <java.com.blah> <nlah bla> abcd:ID=123 user login <date> INFO <java.com.blah> abcd:ID=3243 user login I want to find unique "ID" from this log... (3 Replies)
Discussion started by: gubbu
3 Replies

10. Shell Programming and Scripting

Finding a certain string on each line in a file

Hi, I need a script to get every line from a file where there are less then 17 ; on a line. Thank's (5 Replies)
Discussion started by: VODAFUN
5 Replies
Login or Register to Ask a Question