Thread: Help finding solution

  1. #16
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i fixed it coz i didnt have { in my for loop so next txt was its body so the other didnt work
    Code:
    #include <stdio.h>
    void anylze_it(char *buffer);
    void delete_it(char *buffer,int start);
    int read_line(char *buffer,int num)
    {
        FILE *fp;
        fp=fopen("D:\\finizhad fbot\\8.11.2008\\8.11.2008\\h.c","r");
        if(fp==NULL) return 0;//program failed
        while(fgets(buffer,num,fp)!=NULL)
    	{
            anylze_it(buffer);
    		puts(buffer);
    		getchar();
    	}
    }
    void anylze_it(char *buffer)
    {
         int i,x=0;
         for(i=0;buffer[i]!='\0';i++){
             if(buffer[i]=='/')
                 x++;
                 if(x>=1){
                     printf("%d\n",i);
                     delete_it(buffer,i); 
                     }
                 }
    }
    void delete_it(char *buffer,int num)
    {
         int i;
         for(i=num;buffer[i]!=0;i++)
             buffer[i]=0;
    }
    int main(void)
    {
    	char buffer[100];
    	if(!read_line(buffer,sizeof buffer))
    		exit(0);
    	getchar();
        return 0;
    }

  2. #17
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    but for that while shouldnt it quit when its ! = NULL so i dont need to add error chking for it ?

  3. #18
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your code is still wrong. Read my last post again.

  4. #19
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh yah i ill just use strstr then or any other suggestions?

  5. #20
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    im not sure if this code still have same problem or not
    Code:
    #include <stdio.h>
    void anylze_it(char *buffer);
    void delete_it(char *buffer,int start);
    int read_line(char *buffer,int num)
    {
        FILE *fp;
        fp=fopen("D:\\finizhad fbot\\8.11.2008\\8.11.2008\\h.c","r");
        if(fp==NULL) return 0;//program failed
        while(fgets(buffer,num,fp)!=NULL)
    	{
            anylze_it(buffer);
    		puts(buffer);
    		getchar();
    	}
    }
    void anylze_it(char *buffer)
    {
         int i,x=0;
         for(i=0;buffer[i]!='\0';i++){
             if(buffer[i]=='/'){
                 if(buffer[i+1]=='/'){
                     delete_it(buffer,i);}
                   }
             }
    }
    
    void delete_it(char *buffer,int num)
    {
         int i;
         for(i=num;buffer[i]!=0;i++)
             buffer[i]=0;
    }
    int main(void)
    {
    	char buffer[100];
    	if(!read_line(buffer,sizeof buffer))
    		exit(0);
    	getchar();
        return 0;
    }

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your code still isn't returning anything from read_line().
    Code:
    int read_line(char *buffer,int num)
    {
        FILE *fp;
        fp=fopen("D:\\finizhad fbot\\8.11.2008\\8.11.2008\\h.c","r");
        if(fp==NULL) return 0;//program failed
        while(fgets(buffer,num,fp)!=NULL)
    	{
            anylze_it(buffer);
    		puts(buffer);
    		getchar();
    	}
        /* No return statement here */
    }
    You realize, to "delete" part of a string from the end of the string, all you have to do is set one NULL character? In other words, your delete_it function could look like this.
    Code:
    void delete_it(char *buffer, int num) {
        buffer[num] = 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #22
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    its returning if
    Code:
    if(fp==NULL) return 0;//program failed
    and
    Code:
    void delete_it(char *buffer, int num) {
        buffer[num] = 0;
    }
    will it just delete 1 num or all ? since its alrdy in a for loop it will delete all ?

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It will end the string at position num, which means that it deletes "the rest of the string". 0 means "string ends here", no matter what is after that 0.

    --
    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.

  9. #24
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh thats cool thanks for the info.

  10. #25
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i can also use it in this macro #define DEL_IT(P,N)P[N]=0

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by elwad View Post
    i can also use it in this macro #define DEL_IT(P,N)P[N]=0
    Yes, but that makes it much harder to debug, should you ever need to do that.

    --
    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.

  12. #27
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i was just trying to get the picture

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Solution' and 'Project' usage in VC++
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2007, 09:50 AM
  2. anyone know the solution?
    By heeroyung in forum C Programming
    Replies: 15
    Last Post: 09-30-2005, 06:46 AM
  3. Help finding solution to illegal operator
    By nickadeemus2002 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2004, 10:32 PM
  4. My Unix/Linux SECURITY SOLUTION - pls read
    By bjdea1 in forum Linux Programming
    Replies: 3
    Last Post: 04-11-2004, 09:28 PM
  5. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM