fgets and CreateProcess

Posted on 2003-02-23
Medium Priority
Last Modified: 2007-12-19

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;

Question by:Glytch
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

gj62 earned 300 total points
ID: 8004290
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;
LVL 11

Expert Comment

ID: 8004306
You can try to clean EOL sign from the line:
temp[strlen(temp)-2] = '\0';
LVL 11

Expert Comment

ID: 8004360
Ignore my previous comment because qj62 is right and I am not.

Expert Comment

ID: 8004583
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question