Help required in File Handling

This is a discussion on Help required in File Handling within the C Programming forums, part of the General Programming Boards category; Hi I am a beginner in C. I am working on a program that asks to search words from one ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    Question Help required in File Handling

    Hi I am a beginner in C. I am working on a program that asks to search words from one file whether they exist in other or not. Here is the code.

    The problem seems to be that when the first word is extracted from one file and searched and compared through the whole other file ,the next time outer loop starts the inner loop exits because dt_val is still seems be be EOF . After the loop I do assign it to 0 but it does'nt help the search of the complete first file in other.

    Please help
    Code:
     int main(void)
    {
     
    	FILE* tx;
    	FILE* dt;
    	int dt_val=0;
    	int tx_val=0;
    	int c=0;
    	char buff_tx[20];
    	char buff_dt[20];
                    tx= fopen("C:\\newdt1.txt","r");
    	dt= fopen("C:\\newdt.txt","r");
    	if((tx==NULL)&&(dt==NULL))
    		printf("Both Failed\n");
    	else
    	{
    	    while(tx_val!=EOF)
    	    {
                           tx_val=fscanf(tx,"%s",buff_tx);
    	       while(dt_val!= EOF)
    	       {
    		      dt_val=fscanf(dt,"%s",buff_dt);
    		      if(strcmp(buff_dt,buff_tx)==0)
    			    c++;
                           }
                          dt_val=0
                          if(c==0)
    		  printf("It does not exists\n");
    		   else
    			  printf("Does\n");
    		   c=0;
    	   }
    	}
    
    	    fclose(tx);
    	    fclose(dt);
    
            return 0;
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You could fix this by making a copies of each file pointer. Then use the copies to scan the files. When you reach EOF the set the copies back to their original position at the start of the file.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Making a copy of the file pointer won't help (that just means that they both access the same file, but what happens to the file itself will be reflected in both pointers). If you need to re-read a file, try rewind().

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh I guess I got that confused with strings then. rewind() should do it fine then, or you could load the content to ram.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21