Problem with a code and passing an array to a function

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
I added a second file, I did not change it. The single dash does not work, it has to be double dash. Thanks Raymond, I tried that before, it did not work. Do you have a solution for the blank spaces in the path?
 

spinnaker

Joined Oct 29, 2009
7,830
I added a second file, I did not change it. The single dash does not work, it has to be double dash. Thanks Raymond, I tried that before, it did not work. Do you have a solution for the blank spaces in the path?

God your confusing. You never mentioned program files. Doesn't matter what you changed. You changed something and did not say anything then wondered why it did not work.


Put quotes around the filename in your file "C:\Program Files\foo.exe"

You should be able to use single quotes if you want to hard code and avoid escaping system("'C:\\Program Files\\foo.exe'");
 
I added a second file, I did not change it. The single dash does not work, it has to be double dash. Thanks Raymond, I tried that before, it did not work. Do you have a solution for the blank spaces in the path?
I have to agree with @spinnaker, it is hard to understand what you are trying .

You know that the declaration is int system(const char*command)

You can't change the pointer but you can change the contents of where it is pointing.

I don't know what you mean...anything that I can think of that will work with system("whatever command"), will work by building that string in a.
When you say, "char a[5000] ="explorer /start,C:\\Windows\\notepad.exe";" it works AND it is \0 terminated, you need to build that - explorer /start,C:\\Windows\\notepad.exe - exactly and terminate it with a \0 in the array and don't add the \n that fgetc is adding (it must, right, because you are scanning for it.

That is a problem with code that you posted as I am seeing it - have you moved on to something else?
 

spinnaker

Joined Oct 29, 2009
7,830
I have to agree with @spinnaker, it is hard to understand what you are trying .

You know that the declaration is int system(const char*command)

You can't change the pointer but you can change the contents of where it is pointing.

I don't know what you mean...anything that I can think of that will work with system("whatever command"), will work by building that string in a.
When you say, "char a[5000] ="explorer /start,C:\\Windows\\notepad.exe";" it works AND it is \0 terminated, you need to build that - explorer /start,C:\\Windows\\notepad.exe - exactly and terminate it with a \0 in the array and don't add the \n that fgetc is adding (it must, right, because you are scanning for it.

That is a problem with code that you posted as I am seeing it - have you moved on to something else?
Apparently the TS solved his problem and is now on to a different one. Never said the game was changed till after the fact. The new problem is the classic space in the command line. Which I do not think is a pobelm any longer with Windows 10 so this must be a pre Windows 10 OS, unless the system function is the one that does not like spaces. The command line just simply needs to be surrounded by quotes when there are spaces.
 
Apparently the TS solved his problem and is now on to a different one. Never said the game was changed till after the fact. The new problem is the classic space in the command line. Which I do not think is a pobelm any longer with Windows 10 so this must be a pre Windows 10 OS, unless the system function is the one that does not like spaces. The command line just simply needs to be surrounded by quotes when there are spaces.
ugh

When you run up against something like this, it is a good idea to print out what you are actually reading from fgetc and what you are putting into the a[] array. Byte by byte going past the length that you think it is. This can save you a lot of trouble.
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
I tried null terminating the array, ti does not work, if its hard coded it works, if its read from a file it does not work. That problem was solved by using "strcpy()" which for some reason is ok. Now the problem is the spaces in the path, the idea is to read 1 path and start it, read a second and start it and so on. So the path has to be "Path". This is all, it worked now. New code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

char main()
{
  char a[500]="explorer /start,";/*comand text example: system("explorer /start,C:\\Windows\\notepad.exe");*/
  char b[5000];

  unsigned int i=0,z=0;  /* Counter. */
  char c;/* Chars. */
  FILE *ifp; char InputFileName[]="Files.t"; char *InMode="r";/*Files.*/

  ifp = fopen(InputFileName, InMode);

  if (ifp == NULL)/* This checks the filename for existence/permissions and displays the down message if there is an error with them. */
  {
  printf("The dialog was canceled by the user, the file doesn't exists or doesn't have the right permissions!");
  return -9998;
  }

  for(i=0; (fgets (b, 5000, ifp))!=NULL; i++)
  {
  strcpy(a, b);
  /* Main block. */
  printf("%s", b);/* For testing only. */
  system(a);
  /*return fgetc(ifp);*/
  }
}
 
I tried null terminating the array, ti does not work, if its hard coded it works, if its read from a file it does not work. That problem was solved by using "strcpy()" which for some reason is ok. Now the problem is the spaces in the path, the idea is to read 1 path and start it, read a second and start it and so on. So the path has to be "Path". This is all, it worked now. New code:
Ok great, looks like you have it all sorted out. Now, if you want, you could print out the bytes in a[] before strcpy and after right before you do the system().
 

djsfantasi

Joined Apr 11, 2010
9,156
AFAIK, all versions of Windows still can refer to files in an 8.3 format. File attributes contain the original name AND an 8.3 version of the name.

I don’t remember all of the rules for generating the 8.3 file name. But I’ve run into similar compatibility issues with the “Program Files” directory name. Try replacing “Program Files” with “Progra~1”.

“Progra~1” is the 8.3 file name for the directory.

Hey, Mikey likes it!
Try it. You’ll like it, too
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
Ok, next problem, now the programs get started 1 by 1, I have to close the previous one for the new one to open. Thanks for the "Progra~1" it works better.
 

djsfantasi

Joined Apr 11, 2010
9,156
I need to check in this, but maybe you can. As I remember, there are optional parameters for the “start” command (or maybe for the “command” command).

These parameters specify what the command processor does. Whether it returns immediately, closes the command processor when the program completes, etc...

This reference may help. It does mention that if one is running a program with START or CMD, you only need to supply the program name. Is this a possibility?
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
I don't think the program name is enough. You neeed the path and the name if its not in the environmental variables. I will check the reference, thanks for the help!
 
That part is already in the code, you can see it at line 27. You don't read the previous posts :D.
No, that part is not in the previous code and yes, I did read the previous posts, including line 27 in the code. If you had understood my post, what I said was to print it out byte by byte, before and after as a debugging aid because printf is not going to, necessarily, tell you any non-printable characters.

But, you are working it out, so that's good.
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
I tried printing byte by byte, I wrote in previous posts that I get the accurate result from "printf()" but the program does not start.

For the "explorer" it does not work without it, the programs get started after the previous one is closed.
 

402DF855

Joined Feb 9, 2013
271
Cmd.exe's start command has an interesting idiosyncrasy I discovered when trying to replicate your problem behavior. If the first character of the start command string is a double quote, it assumes you've specified a title for the new window. While start /? alludes to this behavior, it wasn't obvious and googling ultimately lead me to this information. So

start x.exe
start "x.exe"

are not equivalent. The second incantation should be:

start "" "x.exe"
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
Thanks for the suggestion. The same problem, the programs get started only 1 by one. I am thinking the "system()" function has some build in code to wait until the previous program is closed.
 
Top