# Problem with a code and passing an array to a function

#### ArakelTheDragon

Joined Nov 18, 2016
1,350
Hi guys!
I have the following code, when I add the text "explorer /start,C:\\Windows\\notepad.exe" to "system()" it works, when I use "char a[5000] ="explorer /start,C:\\Windows\\notepad.exe";" it works, when I read it from the file, add it to the array and pass it to "system()" it does not work.

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

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

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;
}
/* Main block. */
for(i=0; (c=fgetc(ifp))!= EOF; i++)/* "fgetc()" has automatic incrementation. */
{
a[i]=c;
printf("%c", a[i]);/* For testing only. */
if(a[i]=='\n')
{
system(a);
a[i+1]='\0';
i=0;
}
/*return fgetc(ifp);*/
}
}

#### spinnaker

Joined Oct 29, 2009
7,835
And you expect us to know what system does how?? And "does not work"?

#### ArakelTheDragon

Joined Nov 18, 2016
1,350
its a built in standard C function. It starts any program or file.

And the problem is why when I read characters in the array, they don't have the same effect as when I add them manually to the array.

Last edited by a moderator:

#### spinnaker

Joined Oct 29, 2009
7,835
its a built in standard C function. It starts any program or file.

Standard where???

#### spinnaker

Joined Oct 29, 2009
7,835

#### ArakelTheDragon

Joined Nov 18, 2016
1,350
Yes I found that also, but it does not answer my question, despite that using "strcpy()" bypasses the defect.

The "printf()" function returns this "explorer /start,C:\\Windows\\notepad.exe", the quotes are also in the file.

Last edited by a moderator:

#### spinnaker

Joined Oct 29, 2009
7,835
It would need to return explorer /start,C:\Windows\notepad.exe

You don't need to escape the \ if you are entering from the keyboard or reading from a file

#### spinnaker

Joined Oct 29, 2009
7,835
I would think just C:\Windows\notepad.exe would work too. No reason to call explorer.

#### ArakelTheDragon

Joined Nov 18, 2016
1,350
Does not work, both options. The same problem. The quotes and the single dash does not work.

#### spinnaker

Joined Oct 29, 2009
7,835
Is a now printing as explorer /start,C:\Windows\notepad.exe

#### ArakelTheDragon

Joined Nov 18, 2016
1,350
I already tried these option before I posted, I was looking for hours.

#### spinnaker

Joined Oct 29, 2009
7,835
Also look at a with your debugger. Make certain it is null terminated and at the correct place.

#### spinnaker

Joined Oct 29, 2009
7,835

#### ArakelTheDragon

Joined Nov 18, 2016
1,350

This works, I know, but it does not answer my question.
Code:
  char a[500]="explorer /start,";/*comand text example: system("explorer /start,C:\\Windows\\notepad.exe");*/
char b[5000];

#### ArakelTheDragon

Joined Nov 18, 2016
1,350
New code. Works, now the problem is the blank spaces in "Program files".

#### spinnaker

Joined Oct 29, 2009
7,835
This works, I know, but it does not answer my question.
Code:
  char a[500]="explorer /start,";/*comand text example: system("explorer /start,C:\\Windows\\notepad.exe");*/
char b[5000];

As I mentioned above. Use your debugger and confirm what you think is in a is really in a. If the strcpy works abd it works if you hard code the sting then there has to be something wrong with what you are reading.

#### spinnaker

Joined Oct 29, 2009
7,835
New code. Works, now the problem is the blank spaces in "Program files".

That is the first you mentioned you were referencing Program Files. The answer would have been obvious.

#### Raymond Genovese

Joined Mar 5, 2016
1,658
Hi guys!
I have the following code, when I add the text "explorer /start,C:\\Windows\\notepad.exe" to "system()" it works, when I use "char a[5000] ="explorer /start,C:\\Windows\\notepad.exe";" it works, when I read it from the file, add it to the array and pass it to "system()" it does not work.
You have
if(a=='\n')
{
system(a);
a[i+1]='\0';

what happens when you try
if(a=='\n')
{
a[ i ]='\0'; ,-- edit it doesn't like the a[ i ] which is what I was trying to type
system(a);

IOW get rid of the /n and terminate the char array before using system

NOTE: I have not tested this out but it looks like what may be going on.

Last edited: