Thread: Searching a text file for double quotation marks "

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    Searching a text file for double quotation marks "

    hey, I am using the code below to search a text file for double quotation marks, it seems not to be working because the result is returning 0 zero

    Code:
    /*Function counts the number of double quotation marks*/
    
    void N_Quotations(FILE *fptr,int quotes)
    {
    	
    	
    	fptr=fopen("lammin.txt","r");
    	
    	char numb_quotes;    /*numb_quotes searches the file for quotes*/ 
    	char n_quotes ='"';  /*n_quotes is a constant*/ 
    	int quotes=0;        /*quotes reps. the counter*/
    
    
    while(!feof(fptr))
    	{
    		fscanf(fptr,"%c",&numb_quotes);
    			if(numb_quotes == n_quotes)
    				{
    					quotes++;
    					printf("%c",numb_quotes);
    				}
    	}
    printf("The Estimated Number Of Double Quotation Marks Are %d \n",quotes);
    fclose(fptr);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you get any of the output from this line:
    Code:
    					printf("%c",numb_quotes);
    Also:
    Code:
    		fscanf(fptr,"%c",&numb_quotes);
    can be written as:
    Code:
        numb_quotes = fgetc(fptr);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    I am not getting any output from print("%c",numb_quotes);

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, do this:

    Add at the start of the function:
    Code:
    int count = 0;
    Then
    Code:
    			if(numb_quotes == n_quotes)
    				{
    					quotes++;
    					printf("%c",numb_quotes);
    				}
                            else 
                                    {
                                            if (count < 50)
                                                 printf("%c", numb_quotes);
                                            count ++;
                                    }
    That will show the 50 first characters. See if there is any quotes in that.

    Also beware that if you wrote your text in WORD or some such, it may have changed the simple quotes into som funny "lean left" and "lean" right quots, rather than the generic "straight up" quotes we get in a text editor - and those will of course not match.

    If there is no quotes in the first 50, you may want to do something like this:
    Code:
                                            if (count > 50 && count < 100)
                                                 printf("%c", numb_quotes);
                                            count ++;
    Change the 50 and 100 as you feel required.

    This method of "finding a problem in the code" has a name: "Printf debugging".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    while(!feof(fptr))
    do not do this
    FAQ > Explanations of... > Why it's bad to use feof() to control a loop

    use something like
    Code:
    while(fscanf(fptr,"&#37;c",&numb_quotes) == 1)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. Function is called and I am trying to open a file
    By tommy69 in forum C Programming
    Replies: 88
    Last Post: 05-06-2004, 08:33 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM