hai ,
I am using turbo c compiler,
the system() function always return -1.
i can't run any dos command using system() function
i can't use system() properly
please help me !!!
ex:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i ;
i=system("dir");
printf("%d",i);
getch();
}

Short answer: system("cmd /c dir") Long answer: system() expects an executable. dir is not, it is a builtin.

Your code runs fine on my Dev C++. What OS you are on? Try running on some other compiler.

commented: Turbo-C -- what OS do you think he's on? -4

Although one can run turboc on an OS like windows 7 too, using a software like Dosbox.
But I think it is more probable of him using XP or something, so may be my bad.

Some repairs.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int i ;
  i=system("dir");
  printf("%d",i);
  getchar();
  return 0;
}

This is correct.

@nezachem
system() expects either an executable or a shell builtin.

The system() function can fail for a number of reasons. Be sure to read the man page (which is correct for DOS also).

You can see if the shell is available by calling i=system(NULL); . If it is available, check to make sure your shell is cmd.exe or command.com.

All that said, I'm not sure this is what you really want. Are you trying to get the names of files in the current working directory? You may want to google around the "readdir" family of functions -- they are fairly portable even if you have to #include different headers on DOS/Windows.

Hope this helps.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<errno.h>
void main()
{
int i;
i=system("dir");
perror("");
printf("%d",i);
getch();
}

i just used perror() to find the error ,it shows that "memory allocation error, cannot start command "
why?

system() doesn't work on turbo c++. so use any other compiler dev c++ instead of turbo.

These are different compliler

ya these are diff.actually turbo c/c++ is no more upadated, as borland closed the project. so it don't support the ANSI C features, System() function is the part of ansi c. so use Dev C++, GCC or VS both of these supports ANSI C features.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
system("dir");
getch();
}

actually turbo c/c++ is no more upadated, as borland closed the project. so it don't support the ANSI C features, System() function is the part of ansi c.

Turbo C/C++ is an ancient compiler for an outdated operating system, but it still supports C89 well enough that the system() function is available[1]. I'd certainly agree that unless you're supporting legacy code you should use a modern compiler, but claiming that system() "doesn't work" in Turbo C++ is just plain misinformation.

[1] Unless you go out of your way to find something like version Turbo C 1.0, where it was released early enough before C89 that I can't confidently talk about conformance. But I'm not aware of any way to acquire Turbo C version 1.0, so at the very least we're talking about Turbo C v2.0 or Turbo C++, both of which are reasonably close to conformant with C89.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.