Thread: Problem in a function

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Problem in a function

    I m trying to code a function that finds a specafic char in a string and add to it a another char but i get weird result i dont know why
    Code:
    #include <stdio.h>
    #define DEL_IT(P,N)P[N]=0
    void copy_safe(char *name,char *buffer,int start)
    {
         int i,x=0;
         for(i=start;name[x]=buffer[i];x++,i++);
    }
    void adv_strcat(char *buffer)
    {
         int x=0,i;
         char *save;
         for(i=0;buffer[i]!=0;i++) {
             if(buffer[i]=='\')) {
                 save=malloc(sizeof(char) * (100));
                 x++;
                 }
             if(x==1)
              {
                 x=0;
                 copy_safe(buffer,save,i);
                 DEL_IT(buffer,i);
                 strcat(buffer,"\");
                 strcat(buffer,save);
                 free(save);
              }
          }
    }
    int main(void)
    {
        char name[]="D:\program\program.com";
        adv_strcat(name);
        puts(name);
        getchar();
        return 0;
    }
    when i coded it it seemed pretty logical the adv_strcat
    read the string
    if we found a char we looking for we
    increment x
    then we check if x==1
    if it is we reset it back to 0
    then we copy the string from that place so we put it again l8er one after we deleted it
    then del the string
    then reput the save then free it
    then repeat the process untill there no more stuff char we searching for

    that was its english explaination before i did it i dunno why it failed .

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elwad View Post
    I m trying to code a function that finds a specafic char in a string and add to it a another char but i get weird result
    If by "weird" you mean a bunch of compiler errors and no executable, that's true.

    One big issue: the "\" is the escape character in C. A literal \ must be done as "\\" or '\\'. That goes for every single instance of \ in your program.

    A couple of other odd things; this:
    Code:
    #define DEL_IT(P,N)P[N]=0
    DEL_IT(buffer,i);
    is ridiculous. Just use "buffer[i]=0"

    And mallocing "save" is pointless. Just declare it
    Code:
    save[100];
    since it does not outlive the function anyway. Beyond that, you'll have to explain better what you want -- what the result of "puts(name)" should be.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    dunno i thought it would be the name after being edited well i wanted to do it for FILE functions so in case i can do d:\program.txt without adding another \

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elwad View Post
    dunno i thought it would be the name after being edited well i wanted to do it for FILE functions so in case i can do d:\program.txt without adding another \
    wow, you really made an effort to clarify there. not. well, like, y'know, so as not to make sense to others mostly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by elwad View Post
    dunno i thought it would be the name after being edited well i wanted to do it for FILE functions so in case i can do d:\program.txt without adding another \
    I don't know what this means. What mk27 is saying is that the string "D:\program\program.com" has no '\' characters in it. It has a 'D', a ':', a '\p', an 'r', an 'o', a 'g', another 'r', an 'a', an 'm', a '\p', etc. (Of course, that assumes that \p exists, which I don't think it does, which means you just have an ill-formed string.)

    Other things:
    1. Your copy_safe is not really safe so far as I can see.
    2. I'm not sure exactly what's going on here, but I'm pretty sure nothing useful; there's no reason to eliminate \\ that I know of, since nothing is ever stored with \\ in it (when you type "D:\\program\\program.exe" no \\ are stored, just a single \).

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    like you when you save a file for writing it to a folder you do D:\\FILE.txt
    i wanna make a save as like notepad but everytime i have to do \\ not one \
    so i wanted to do a program that find the \ and add another \ to it so it becomes "\\"

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elwad View Post
    like you when you save a file for writing it to a folder you do D:\\FILE.txt
    i wanna make a save as like notepad but everytime i have to do \\ not one \
    so i wanted to do a program that find the \ and add another \ to it so it becomes "\\"
    Great, but in the context of the C code itself you do need "\\". \ begins an escape sequence for "unrepresentable" byte values like the newline (\n) or the tab (\t) or to include a quote inside quotes:
    Code:
    printf("Say \"Hello,\" world.");
    Whenever the compiler hits a "\" it presumes the next character is going to be interpreted in a special way. So to print \hello\world you use:
    Code:
    printf("\\hello\\world");
    Get it? And the individual character \ is '\\'. If you are looking for \, use '\\'.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course, had you just compiled with warnings on, it would have given you warnings about invalid escape sequences...


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

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by elwad View Post
    like you when you save a file for writing it to a folder you do D:\\FILE.txt
    i wanna make a save as like notepad but everytime i have to do \\ not one \
    so i wanted to do a program that find the \ and add another \ to it so it becomes "\\"
    If you are reading in from somewhere else (people typing, for instance) then double slashes are neither necessary nor desirable. (For instance, if you ask the user for a filename and they type D:\\file.txt then it won't work. And if they do type in a single slash, then that's what you want, so don't mess it up.)

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just use forward slashes and save yourself the headache.


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

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    sorry was away one more question
    how notepad or any other editor save stuff with only one slash in it ? is there a function where i can like File save as in bla bla directory ??

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by elwad View Post
    sorry was away one more question
    how notepad or any other editor save stuff with only one slash in it ? is there a function where i can like File save as in bla bla directory ??
    Because you only need two slashes when you specify a path in the C language - it turns into a single slash when the compiler processes it.

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

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh oke thanks mate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM