Thread: pointers-string search

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    13

    pointers-string search

    I know an element in an array, say file1[10][10].

    I know a pointer to one of the character in file1[10][10]
    *****************

    say if I have file1[10][10]="whereiam"

    and I know a pointer to "i" in whereiam

    I want to print "where"


    I tried this, but have problem with these pointers.

    *******************
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char *s[30];
    	char ch, ch1[100][100];
    	char file1[100][100], file2[100][100];
    	int i=0, j, k, l, z=0, m, collect[100];
    	char one_file[] = "one.txt";
    	char two_list[] = "two.txt";
    	char file_list[100][100];
    	FILE *one,*two;
    	clrscr();
    	one = fopen(one_file,"r");   
    	two = fopen(two_list, "w");
    	while((ch=getc(one))!=EOF)
    	{
    		if(ch=='"') /*trying to search text between "..."*/
    		{
    			j=0;
    			while((ch=getc(one))!='"')
    			{
    				file1[i][j]=ch;   /* text between "...." is stored*/
    				fprintf(two, "%c", ch);
    				j++;
    			}
    			file1[i][j]='\0';
    			fputc('\n',two);
    			i++;
    		}
    	}
    
    	for(k=0;k<i;k++)  /*trying to search stringf in file1 with "text"*/
    	{
    		s[k]=strstr(file1[k],"text");
    		if(s[k]!=NULL)
    		{
    			printf("\n\n%s",s[k]);
    			collect[z]=k;
    			z++;
    		}
    	}
    
    /*if i have text "org.text#extra" , i get a pointer to "t" in org.text from above s[k]*/
    /* i actually want org.text. I know pointer to "o" in org.text#extra, and i know pointer to "t". */
    /*Now I am trying to read from pointer to "o" to pointer to "t"*/
    
    
    /* to try this i implemented as below, this goes to infinite loop*/
    
    
    	for(k=0;k<z;k++)
    	{
    		m=0;
    		while((file1[collect[k]])!=(s[k]-1))
    		{
    			file2[k][m]=file1[k][m];
    			m++;
    			++*file1[collect[k]];
    		}
    		file2[k][m+1]=NULL;
    	}
    	for(k=0;k<z;k++)
    	{
    		printf("\n\nfinal strings = %s",file2[collect[k]]);
    	}
    	fclose(one);
    	fclose(two);
    	return(0);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    2nd doubt:

    Why am I not able to identify "#" in a string?

    if "one" is a string,

    while(ch=getc(one))!='#'), is never being satisfied, whereas I have "#" in the "one" string

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) 'ch' will never equal EOF, because EOF cannot fit in a char. Use an integer instead. Read the FAQ entry on EOF for more.

    2) You should escape your quotation marks. This:
    Code:
    if(ch=='"')
    Should really be:
    Code:
    if( ch == '\"' )
    That'll get you started.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM