• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 655
  • Last Modified:

fgets and CreateProcess


I'm trying to read in a file of commands and create a seperate process for each one of the commands. I can use fscanf to get the commands from the file but fscanf only reads in characters until the first space which is useless because there may be command line parameters

e.g. C:\windows\system32\notepad.exe bob.txt

I'm trying fgets which reads in entire lines but it includes the end of line marker at the end of each line it reads and CreateProcess doesn't seem to like end of line markers as CommandLine names. The last command in the file executes fine as it does not have an eoln marker.

I keep getting Error Code 123. I think the problem is that fgets is including the eoln marker and CreateProcess doesn't like it.

Can anyone shed some light on the subject?

Thanks in advance


#include <stdio.h>
#include <windows.h>
#include <string.h>

int main(int argc, char *argv[])

     char temp[200];
     FILE *fp = NULL;

     fp = fopen("launchset.txt", "r");

     while (!feof(fp))

          fgets(temp, sizeof(temp), fp);

          ZeroMemory(&sInf, sizeof(sInf));
          sInf.cb = sizeof(sInf);

          if (!CreateProcess(NULL, temp, NULL, NULL, FALSE, HIGH_PRIORITY_CLASS | CREATE_NEW_CONSOLE, NULL, NULL, &sInf, &pInf))

               fprintf(stderr, "Create Process failed on error %d\n", GetLastError());




     return 0;

  • 2
1 Solution
If your problem is the EOL character, you can easily remove it by adding the following line after the fgets:

temp[strlen(temp)-1] = 0;
You can try to clean EOL sign from the line:
temp[strlen(temp)-2] = '\0';
Ignore my previous comment because qj62 is right and I am not.
Another way that I prefer is to write a function that trims any whitespace off the end of a line... I actually do this quite frequently. I will use fgets to read a line, which does include a newline character.. so after I have read my line from the file I use one of the following functions to trim and potential whitespace off of the begiining and or end of the buffer holding the file line...

take a look

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>

     ltrim (Left Trim)
     Removes any leading spaces from a given string (c).
char *ltrim(char *string)
     for (NULL; *string && isspace(*string); string++);

     return string;

     rtrim (Right Trim)
     Removes any trailing spaces from string c.
char *rtrim(char *string)
     int i=strlen(string), x = 0;
     char *newString = NULL;

     if (string)
          while (--i >= 0)
               if (!isspace(string[i]))

     (char *) newString=(char *)malloc( i * sizeof( char ) );

     for( x=0; x<=i && string[x]!='\0' ; x++)

     return newString;

   Removes all leading and trailing spaces from a given string (c).
char *trim(char *string)
     string = ltrim(string);
     return rtrim(string);

void main()

     char  *StartString="    Rock the casbah     ";
     char  *szSearchString="the";

     printf("Demonstration of String Functions for C\n\n"
               "Original String:\t\t\t\t\"%s\"\n\n"
               "--------------------------------------------------------------------------\n\n", StartString);

     printf("ltrim(): Remove leading spaces\t\t\t\"%s\"\n", ltrim(StartString));
     printf("rtrim(): Remove trailing spaces\t\t\t\"%s\"\n", rtrim(StartString));
     printf("trim(): remove trailing and leading spaces\t\"%s\"\n", trim(StartString));
     printf("nested: calls to ltrim() and rtrim()\t\t\"%s\"\n", ltrim(rtrim(StartString)));
     printf("original: original string\t\t\t\"%s\"\n\n", StartString);

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now