Problem with a code and passing an array to a function

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
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);*/
  }
}
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
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:

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
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,830
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
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,362
Try this instead of reading it

strcpy(a,"explorer /start,C:\\Windows\\notepad.exe");
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];
 

spinnaker

Joined Oct 29, 2009
7,830
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.
 
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:
Top