weird strings

This is a discussion on weird strings within the A Brief History of Cprogramming.com forums, part of the Community Boards category; i made this to separate the string argument created from a drag and drop. but i'm not sure why the ...

  1. #1
    Unregistered
    Guest

    Unhappy weird strings

    i made this to separate the string argument created from a drag and drop. but i'm not sure why the 'justpath' prints extra charactors after the path?. hope you see what i mean from the code. i tryed adding NULL thingy to the end but this didn't help.
    Code:
    #include <stdio.h>
    main(argc,argv)
    int argc;
    char *argv[];
    {
    int go,go2,pathlen;
    char *path,*filename,*extention,*justpath;
    path=(char*)malloc(60);
    filename=(char*)malloc(20);
    extention=(char*)malloc(10);
    pathlen=strlen(argv[1]);
    path=argv[1];
    printf("value of [argv[0]]= %s\n",argv[0]);
    printf("value of [argv[1]]= %s\n",argv[1]);
    printf("amount of arguments[argc]= %d\n\n",argc);
    printf("path length= %d\n",pathlen);
    printf("name of [path]= %s\n",path);
    printf("starting address of [path]= %p\n",path);
    for(go=pathlen;path[go]!='.';go--){
    printf("%d: to the dot [path[go]]= %c\n",go,path[go]);
    }
    
    for(go2=pathlen;path[go2]!='\\';go2--);
    
    strncpy(extention,path+go,5);
    printf("\n extention == %s\n",extention);
    printf("starting address of [exten]= %p\n",extention);
    
    strncpy(filename,path+go2,20);
    printf("\n filename == %s\n",filename);
    printf("starting address of [file]= %p\n",filename);
    
    strncpy(justpath,path,go2);
    //strcat(justpath,"\0");
    printf("\n justpath == %s\n",justpath);
    printf("starting address of [justpath]= %p\n",justpath);
    return(0);}

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You'll have to be a bit more specific. What type of program is this, and what is this drag and drop program? If you know what the length of the string is, add a null character to the end based on that. Otherwise, with your current method, you could be placing the null after the extra characters. This would also be cause by have an array that wasn't initialized properly, or was initialized to be too big. What you should do is create a blank array first, and then filling it with the data. Make it blank by having every element contain \0, otherwise you'll end up with a bunch of characters that just depend on what was in memory last time it was used.

  3. #3
    Unregistered
    Guest

    Smile

    its not really a program in its self but a function for a program. the main program just converts a files binary data into a format a C program can use. this works fine but you have to type the file name and its this i want to get rid of. i also want to use this data, location etc.. and transfer it to the output file.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Again, you'll have to be more specific. First of all, what is the drag and drop for? What kind of program is this function for? Why are they typing the program path? Why do you want to get rid of it?

  5. #5
    Unregistered
    Guest

    Smile

    while working on what you said in your first post i discovered i didn't allocate memory to the justpath variable. i'm sorry i was clear about the drag and drop i was on about how you can drag the file icon other the program icon and it would send the files path and name as a string to the second argument.
    sorry to waste your time but what you've said has helped. thanks.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    lol - No problem. Glad I could help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Strings??
    By Paul22000 in forum C++ Programming
    Replies: 12
    Last Post: 07-14-2008, 02:02 PM
  2. Replies: 5
    Last Post: 04-15-2008, 11:24 AM
  3. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  4. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 03:49 AM

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