FILE:string seaching how many times occur

This is a discussion on FILE:string seaching how many times occur within the C Programming forums, part of the General Programming Boards category; Hi, Here is my code I tried to count occurence of string "01.01."in FILE . There is no output coming,it ...

  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
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    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 01:26 AM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 01:07 AM

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