| | |
Search string in a text file
![]() |
•
•
Join Date: Oct 2005
Posts: 6
Reputation:
Solved Threads: 0
I have a problem. I have a function that search a string in a text file. This function return an int: 0 if the string I must be search isn't in the file, 1 otherwise.
The problem is: if the searching string is "input1" and in the file there is a string "counter_input1" the function return 1 instead must be return 0.
Bye
The problem is: if the searching string is "input1" and in the file there is a string "counter_input1" the function return 1 instead must be return 0.
Bye
•
•
Join Date: Oct 2005
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Micko
Well, in that case it would be nice to post your solution...
C Syntax (Toggle Plain Text)
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include <malloc.h> #define IOError -1 #define MEMORYError -2 #define NOError 0 const char searchingString[20]; //string to search char sizeLineInput[512]; char sizeLineOutput[512]; char *pStart; char *pEnd; char *token; int k; int findWord(FILE *SourceFile, char searchingString[20]){ if (SourceFile != NULL){ while (fgets(sizeLineInput, 512, SourceFile)!=NULL){ sizeLineOutput[0] = '\0'; pStart = sizeLineInput; token = strtok(sizeLineInput ," "); do{ token = strtok('\0',", "); if (token){ if(strcmp(token,searchingString) == 0){ //printf("%d\n",k); k = 0; } if(strcmp(token,searchingString) == -1){ //printf("%d\n",k); k = -1; } if(strcmp(token,searchingString) == 1){ //printf("%d\n",k); k = 1; } } }while(token); //printf("%d\n",k); if (k==0) return k; } } return -1; }
[code][/code] tags >> Last edited by Dave Sinkula; Nov 8th, 2005 at 11:02 am.
•
•
•
•
hi i m trying but couldnt able to do the progrm.
the program is in c & its on finding a string from a text file.
so please help me out to doing it
Could you state it in other words?.
Did you tried the posted code and it didn't work for you? or you are having problems in creating a program that does the same thing?.
Last edited by Aia; Apr 4th, 2007 at 7:17 pm.
•
•
•
•
hi i m trying but couldnt able to do the progrm.
the program is in c & its on finding a string from a text file.
so please help me out to doing it
1) explain what you've tried
2) show what you've tried (read this)
3) not hijack someone else's thread -- make your own
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 0
hi, i have done the following codding its of searching perticular string
from text file. but it couldnt work please help me out
from text file. but it couldnt work please help me out
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <ctype.h> #include <conio.h> #include <string.h> #define NULL 0 //char cut(char,char); main() { clrscr(); int i=0,j=0; char s[18]={"<nx-charging:date>"}; char s1[23]={"<nx-charging:serviceID>"}; char s2[25]={"<nx-charging:finalStatus>"}; //char s[]={"<nx-charging:date>"}; FILE *fpt; char ch,c,b[23],a[18]; if((fpt=fopen("d:\\sample2.txt","r"))==NULL) printf("\nERROR - cannot open the file "); else do { c=fgetc(fpt); a[i]=c; i++; a[i]; if(i==18) { if(strcmp(a,s)==0) { /*int k; for(k=0;k<18;k++){ while(a[k]!='<') { printf("%s",ch); }*/printf("\n%s",a); break; } /* else cut(a,n); /*{ int l; for(l=0;l<strlen(a);l++) { if(l!=strlen(a)-1) { a[i]=a[i+1]; } else { goto m; } } }*/ } c=fgetc(fpt); b[j++]=c; if(j==23) { if(strcmp(b,s1)==0) { /*int j; for(j=0;j<18;j++){ /*while(a[j]!='<') { printf("%c",ch); }*/printf("\n%s",b);break; } } ch=fgetc(fpt); b[j++]=ch; if(j==25) { if(strcmp(b,s2)==0) { /*int j; for(j=0;j<18;j++){ /*while(a[j]!='<') { printf("%c",ch); }*/printf("\n%s",b);break; } } }while(c!=EOF); fclose(fpt); getch(); return 0; } /*char cut(char v[18],char m) { int l; for(l=0;l<strlen(v);l++) { if(l!=strlen(v)-1) { v[l]=v[l+1]; } else v[l++]=m; } } */
Last edited by Ancient Dragon; Apr 9th, 2007 at 12:04 pm. Reason: add code tags
![]() |
Similar Threads
- Please anybody to help to read string value in text file using C# (C#)
- Binary Search on a text file (Java)
- Read in a string from a text file (C)
- New User (C)
Other Threads in the C Forum
- Previous Thread: letter p
- Next Thread: C- Syntax to allocate Global variables to consecutive memory locations
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi






