Problem with a code and passing an array to a function

Discussion in 'Programmer's Corner' started by ArakelTheDragon, Mar 13, 2019 at 8:36 PM.

  1. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,581
    3,476

    Yes the other problem was the double \\ in the input file which I pointed out and then it seems like you completely changed the file name and never mentioned it.
     
  2. ArakelTheDragon

    Thread Starter Well-Known Member

    Nov 18, 2016
    1,201
    97
    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?
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,581
    3,476

    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'");
     
  4. Raymond Genovese

    Well-Known Member

    Mar 5, 2016
    1,366
    774
    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?
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,581
    3,476
    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.
     
  6. Raymond Genovese

    Well-Known Member

    Mar 5, 2016
    1,366
    774
    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.
     
  7. ArakelTheDragon

    Thread Starter Well-Known Member

    Nov 18, 2016
    1,201
    97
    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 (Text):
    1.  
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <math.h>
    5.  
    6. char main()
    7. {
    8.   char a[500]="explorer /start,";/*comand text example: system("explorer /start,C:\\Windows\\notepad.exe");*/
    9.   char b[5000];
    10.  
    11.   unsigned int i=0,z=0;  /* Counter. */
    12.   char c;/* Chars. */
    13.   FILE *ifp; char InputFileName[]="Files.t"; char *InMode="r";/*Files.*/
    14.  
    15.   ifp = fopen(InputFileName, InMode);
    16.  
    17.   if (ifp == NULL)/* This checks the filename for existence/permissions and displays the down message if there is an error with them. */
    18.   {
    19.   printf("The dialog was canceled by the user, the file doesn't exists or doesn't have the right permissions!");
    20.   return -9998;
    21.   }
    22.  
    23.   for(i=0; (fgets (b, 5000, ifp))!=NULL; i++)
    24.   {
    25.   strcpy(a, b);
    26.   /* Main block. */
    27.   printf("%s", b);/* For testing only. */
    28.   system(a);
    29.   /*return fgetc(ifp);*/
    30.   }
    31. }
    32.  
     
  8. Raymond Genovese

    Well-Known Member

    Mar 5, 2016
    1,366
    774
    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().
     
  9. ArakelTheDragon

    Thread Starter Well-Known Member

    Nov 18, 2016
    1,201
    97
    That part is already in the code, you can see it at line 27. You don't read the previous posts :D.
     
  10. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    4,043
    1,479
    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
     
    ArakelTheDragon and spinnaker like this.
  11. ArakelTheDragon

    Thread Starter Well-Known Member

    Nov 18, 2016
    1,201
    97
    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.
     
  12. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    4,043
    1,479
    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?
     
  13. ArakelTheDragon

    Thread Starter Well-Known Member

    Nov 18, 2016
    1,201
    97
    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!
     
  14. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    4,043
    1,479
    Ok, you’re right. I mistakenly assumed the full path WAS specified as the program name.
     
  15. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,581
    3,476

    Try not starting explorer as I mentioned about a dozen posts ago. Just start your program with system.
     
    djsfantasi likes this.
  16. Raymond Genovese

    Well-Known Member

    Mar 5, 2016
    1,366
    774
    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.
     
  17. ArakelTheDragon

    Thread Starter Well-Known Member

    Nov 18, 2016
    1,201
    97
    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.
     
  18. 402DF855

    Active Member

    Feb 9, 2013
    36
    6
    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"
     
  19. ArakelTheDragon

    Thread Starter Well-Known Member

    Nov 18, 2016
    1,201
    97
    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.
     
  20. 402DF855

    Active Member

    Feb 9, 2013
    36
    6
    My test code issues each command without waiting. Did you get rid of the "explorer," part? It's not needed IMO.
     
Loading...