Thread: constant char lengths are getting me down

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    constant char lengths are getting me down

    i have a little program which takes a keyboard input from fgets() and puts it in a string (char cmdLine[15])

    it then passes that string as a command line to a program which loads the bitmap specified by the command line

    the problem is that i dont know how to edit the string so that if i dont enter a full 15 characters, it wont pass along NULL characters and all that.

    what i mean is that if the filename I pass along is "picture.bmp", the bitmap loader will tell me that it cant find the file:
    "picture.bmp
    "

    notice there is a newline included there, i assume that the newline is being passed along in the char.

    here is the launcher:

    Code:
    #include <stdio.h>
    #include <process.h>
    
    int main()
    {
        char cmdLine[15];
    
        printf("Command Line: ");
        fgets(cmdLine,sizeof(cmdLine),stdin);
    
        spawnl(P_OVERLAY,"unchain4.exe","unchain4.exe",cmdLine,NULL);
    
        return 0;
    }
    obviously, cmdLine contains some unwanted characters, but how do i get rid of them?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Something like this
    Code:
    if (cmdLine[strlen(cmdline)-1] == '\n') cmdLine[strlen(cmdline)-1] = '\0';
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM