Thread: FILE:string seaching how many times occur

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

    Thumbs up FILE:string seaching how many times occur

    Hi,
    Here is my code I tried to count occurence of string "01.01."in FILE .
    There is no output coming,it shows blank window. Please guide me regarding this.

    Munisamy

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    1) void main() is incorrect. it should be int main(void)
    2) what you are doing is
    Code:
            if(str1==str2)
            count++;
    str1 and str2 are the starting addresses of the array elements and cannot be equal
    use strcmp() for comparing the strings
    3)strcpy() is used incorrectly in your code. strcpy is defined as
    Code:
         char *strcpy(char*temp,const char *src)
    it will copy the string src to temp
    4) what are you trying to do with fseek() ?? you are positioning the pointer to the end of file rite at the start..that wont do.
    5) clrscr() is nonportable..check the faq for ways to clear the screen
    6) getch() is defined in conio.h so include that header.
    7) strcpy() and strcmp() are defined in string.h so include that as well.
    8) dont forget to change void main() to int main (void)

    edit: as quzah pointed out, i missed the "=" sign in the if() statement..
    Last edited by PING; 02-26-2005 at 02:26 AM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just to add..
    > strcpy(str2,fp);
    Use fgets to read a line from a file. Simply referring to fp here doesn't cause the file to be read.

    Also, even for such a small program, your indentation is poor.

    > char str2[6];
    You could never store a valid string in that array which would ever match char str1[]="01.01.";
    because it's at least one character too short.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by PING
    2) what you are doing is
    Code:
            if(str1=str2)
            count++;
    str1 and str2 are the starting addresses of the array elements and cannot be equal
    You've missed the error here where they're using the assignment operator instead of the comparison operator. If they were in fact trying to compare the two addresses (which is allowable) then they should be using == and not =

    That's actually the only "error" in that code. And if in fact those are the names of arrays, their compiler would be telling them this. I'd suggest the origional poster pay attention to their compiler's warnings and errors. If they aren't getting any, then I'd suggest turning them on.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 11:08 AM
  2. Something about probablility
    By mike_g in forum A Brief History of Cprogramming.com
    Replies: 116
    Last Post: 03-13-2008, 05:33 PM
  3. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 12:00 PM
  4. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  5. Fastest STL container?
    By Shakti in forum C++ Programming
    Replies: 18
    Last Post: 02-17-2006, 02:07 AM