Can anyone tell me how to execute DOS commands using C? I tried the following but it isn't working.
#include%26lt;stdlib.h%26gt;
#include%26lt;stdio.h%26gt;
int main(void)
{
printf("Going to execute a C command");
system("dir");
return 0;
}
Iam just getting the printf statement executed. The dir command isn't working plz help!
C help needed?
I dont code much in windows, but what little I have seen, system command work just as well there.
I tried your code in Linux (with ls replaceing dir) and I got the expected result. So the program works for sure.
tell you what, check if adding a \n at the end of the print line changes things.
I have a feeling, you are running into buffer related issues.
The other thing that you can do is to change the command from dir to something like "mkdir a"
Reply:Try including Dos.h in your program. Write #include %26lt;dos.h%26gt;.
Have Phun Coding :)
garden flowers
Tuesday, July 14, 2009
How to allow unspecified number of parameters in C function?
For example, in C you have the function "printf" which takes a char* string with text, formatters, etc. and then you pass in a comma delimited list of variables for each parameter you provided in the first string. How do you define such a function? How do you make reference to these parameters? I assume there must be some base pointer which can be used to access the various entries?
How to allow unspecified number of parameters in C function?
Use "..."
I don't really get it, but there's some documentation here: http://courses.cs.vt.edu/~cs1704/notes/A...
Reply:Use ellipses in function definition:
void Foo( ... )
then use the following to get arguments:
va_list marker;
int first = va_arg( marker, int );
char* pSecond = va_arg( marker, char* );
...
va_end( marker );
How to allow unspecified number of parameters in C function?
Use "..."
I don't really get it, but there's some documentation here: http://courses.cs.vt.edu/~cs1704/notes/A...
Reply:Use ellipses in function definition:
void Foo( ... )
then use the following to get arguments:
va_list marker;
int first = va_arg( marker, int );
char* pSecond = va_arg( marker, char* );
...
va_end( marker );
Hey, please help me with some questions about C-Program!?
1) What are the header files in the C-Program?
2) What does "printf" do?
3) What does "scanf" do?
4) What are the data types in C-Languages?
This is for an assignment...
If possible, kindly include a site for my further info. Thanks a lot!
Hey, please help me with some questions about C-Program!?
1. header files are those files that hold the declarations of some of the functions and variables(and hence called "Pre-defined functions"). Those functions are put under different headers based on their function. Since those ".h" files are included usually at the top of a program file, they are referred to as "header files"
2. printf, a function under stdio.h(standard I/O header), prints a string- a raw string or values of variables - represented as format specifiers. it returns the total number of characters printed on to the standard console(Usually, the monitor)
3. scanf, a counterpart of printf, scans (or reads) values mentioned using format specifiers, from the stand console.(E.g. Key board). this too retrurns an integer, the total count of read characters.
4. Basic data types include:
int, char,float and double.
void can also be referred to as one of the data types.
Derived data types include:
Arrays, pointers and functions
"Let Us C" is widely acclaimed as the best guide for learning C.
Thank you,
Regards.
Reply:The headers file are files that contain prototype of many built in function in c program.
printf- is a function that allows u to display any kind of text in ur program.
the format is printf("your text");
as a result u'll get what ever u write in between the commas(in this case "your text") will be displayed in screen as u run the program.
But c program can't perform this function without a prototype(it tells c program how to perform). These prototypes are written in header files. So if u want to use printf function u will need to include the header files in ur program.
scanf- this function is used for take a input from ur keyboard and store it against a variable (u will have to define the variable name and data type first in ur program)
the format is scanf("%d", %26amp;name);
where name is the name of the variable and %d is a string that depends on the type of variable u are using.
data types are types of variables u can use on c program u will find details here
http://www.cplusplus.com/doc/tutorial/va...
try the whole website for more info
Reply:I think you can easily do what the rest of us do, like google your questions. You are getting us to do the work for you, but there is a positive to it, that if we do not know the subject we get to learn it along with you.
http://snap.nlc.dcccd.edu/learn/nlc/prin...
http://www.devx.com/tips/Tip/14539
http://www.unet.univie.ac.at/aix/libs/ba...
http://groups.google.com.au/groups?q=hea...
http://groups.google.com.au/groups/searc...
I wonder though if it isn't yahoo staff who prepare these questions to get us involved, for it is common sense what one needs to do....but whatever...
calling cards
2) What does "printf" do?
3) What does "scanf" do?
4) What are the data types in C-Languages?
This is for an assignment...
If possible, kindly include a site for my further info. Thanks a lot!
Hey, please help me with some questions about C-Program!?
1. header files are those files that hold the declarations of some of the functions and variables(and hence called "Pre-defined functions"). Those functions are put under different headers based on their function. Since those ".h" files are included usually at the top of a program file, they are referred to as "header files"
2. printf, a function under stdio.h(standard I/O header), prints a string- a raw string or values of variables - represented as format specifiers. it returns the total number of characters printed on to the standard console(Usually, the monitor)
3. scanf, a counterpart of printf, scans (or reads) values mentioned using format specifiers, from the stand console.(E.g. Key board). this too retrurns an integer, the total count of read characters.
4. Basic data types include:
int, char,float and double.
void can also be referred to as one of the data types.
Derived data types include:
Arrays, pointers and functions
"Let Us C" is widely acclaimed as the best guide for learning C.
Thank you,
Regards.
Reply:The headers file are files that contain prototype of many built in function in c program.
printf- is a function that allows u to display any kind of text in ur program.
the format is printf("your text");
as a result u'll get what ever u write in between the commas(in this case "your text") will be displayed in screen as u run the program.
But c program can't perform this function without a prototype(it tells c program how to perform). These prototypes are written in header files. So if u want to use printf function u will need to include the header files in ur program.
scanf- this function is used for take a input from ur keyboard and store it against a variable (u will have to define the variable name and data type first in ur program)
the format is scanf("%d", %26amp;name);
where name is the name of the variable and %d is a string that depends on the type of variable u are using.
data types are types of variables u can use on c program u will find details here
http://www.cplusplus.com/doc/tutorial/va...
try the whole website for more info
Reply:I think you can easily do what the rest of us do, like google your questions. You are getting us to do the work for you, but there is a positive to it, that if we do not know the subject we get to learn it along with you.
http://snap.nlc.dcccd.edu/learn/nlc/prin...
http://www.devx.com/tips/Tip/14539
http://www.unet.univie.ac.at/aix/libs/ba...
http://groups.google.com.au/groups?q=hea...
http://groups.google.com.au/groups/searc...
I wonder though if it isn't yahoo staff who prepare these questions to get us involved, for it is common sense what one needs to do....but whatever...
calling cards
The difference between stream output and printf is :?
a. type and formatting info is not used with stream I/O.
b. stream I/O can be directed to any output device
c. stream I/O is faster
d. printf cannot be used in C++
The difference between stream output and printf is :?
b
b. stream I/O can be directed to any output device
c. stream I/O is faster
d. printf cannot be used in C++
The difference between stream output and printf is :?
b
Studying for final, need help with c?
#include %26lt;stdio.h%26gt;
#define N 100
int main()
{
---int c, i = 0;
---char s[N];
---while((c = getchar()) != '\n' %26amp;%26amp; i %26lt; N)
------s[i++] = c;
---s[i] = '\0';
---printf("%s\n", s);
---return 0;
}
there are two bugs in this program, what are they?(hint: look for errors at the boundaries)
okay ive compiled this program and everything, i ant seem to figure out what the errors should be, i copied/pasted this directly from an old exam. Any help please
Studying for final, need help with c?
the while loop limits i to 99. however it will be incremented to 100 in s[i++]=c; s[N] is s[100], which i think in C means it can have element from s[0] to s[99]. So your array will be out of bounds when i=100.
also it seems like s[i]='\0'; overwrites the last entered character.
Reply:there is no bug in the program
the program prints what ever the input you give
Reply:while((c = getchar()) != '\n' %26amp;%26amp; i %26lt; N-2)
------s[i++] = c;
/*
i points to the last array element now, no "out-of-bound" errors.
*/
#define N 100
int main()
{
---int c, i = 0;
---char s[N];
---while((c = getchar()) != '\n' %26amp;%26amp; i %26lt; N)
------s[i++] = c;
---s[i] = '\0';
---printf("%s\n", s);
---return 0;
}
there are two bugs in this program, what are they?(hint: look for errors at the boundaries)
okay ive compiled this program and everything, i ant seem to figure out what the errors should be, i copied/pasted this directly from an old exam. Any help please
Studying for final, need help with c?
the while loop limits i to 99. however it will be incremented to 100 in s[i++]=c; s[N] is s[100], which i think in C means it can have element from s[0] to s[99]. So your array will be out of bounds when i=100.
also it seems like s[i]='\0'; overwrites the last entered character.
Reply:there is no bug in the program
the program prints what ever the input you give
Reply:while((c = getchar()) != '\n' %26amp;%26amp; i %26lt; N-2)
------s[i++] = c;
/*
i points to the last array element now, no "out-of-bound" errors.
*/
Solve this...any c++ programmers out there..?
a simple c++ program--%26gt;
main()
{ static int a[]={97,98,99,100,
101,102,103,104};
int*ptr=a+1;
print(++ptr,ptr--,ptr,
ptr++,++ptr);
}
print(int *a,int *b,int *c,int *d,int *e)
{
printf("\n%d %d %d %d %d",*a,*b,*c,*d,*e);
}
help me with its o/p....i m not getting it..
its showing 100 100 100 99 99
but i thought 99 99 98 98 99
Solve this...any c++ programmers out there..?
This is due to the fact that the compiler execute printf from right to left internally but prits the output Left to Right. So if you look at the printf this way you will understand the output. i.e
1st execution will be ++ptr
2nd execution will be ptr++ and so on......
Reply:The print( ) statement with all the incremented and decremented ptr arguments can give different results on different C compilers, because there is no rule for the compiler about the order in which the arguments should be evaluated.
b, c, d and e are not declared, so the program should not compile anyway, and they are not initialised, so if it somehow compiles, the results of their print( ) statements could be anything.
main()
{ static int a[]={97,98,99,100,
101,102,103,104};
int*ptr=a+1;
print(++ptr,ptr--,ptr,
ptr++,++ptr);
}
print(int *a,int *b,int *c,int *d,int *e)
{
printf("\n%d %d %d %d %d",*a,*b,*c,*d,*e);
}
help me with its o/p....i m not getting it..
its showing 100 100 100 99 99
but i thought 99 99 98 98 99
Solve this...any c++ programmers out there..?
This is due to the fact that the compiler execute printf from right to left internally but prits the output Left to Right. So if you look at the printf this way you will understand the output. i.e
1st execution will be ++ptr
2nd execution will be ptr++ and so on......
Reply:The print( ) statement with all the incremented and decremented ptr arguments can give different results on different C compilers, because there is no rule for the compiler about the order in which the arguments should be evaluated.
b, c, d and e are not declared, so the program should not compile anyway, and they are not initialised, so if it somehow compiles, the results of their print( ) statements could be anything.
Can you help me debug this program? c++ updating a record, its error is in the search function?
#include "stdafx.h"
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;ctype.h%26gt;
#include %26lt;conio.h%26gt;
#include %26lt;string.h%26gt;
void searchNupdate();
void dispRec();
void displayRecord();
void displayTitle();
void updateRecord();
void search(int, int, int);
struct book
{
char bookID[6];
char title[31];
char author[31];
char copyright[5];
}bookrec;
FILE *bookfile;
int main()
{
bookfile= fopen("BOOK.DAT", "rb");
if(bookfile==NULL)
{
printf("File does not exist, press any key..");
getch();
}
else
{
fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);
if(feof(bookfile))
{
printf("File is empty, press any key..");
getch();
}
else
searchNupdate();
}
fclose (bookfile);
return 0;
}
void searchNupdate(void)
{
char rep;
int found=0;
int target=0;
int size;
displayTitle();
do
{
printf("Input book ID to be updated:");
scanf("%d", %26amp;target);
search(target,%26amp;found,size);
if(!found)
{
printf("Record is not found\n");
}
else
{
displayRecord();
}
do
{
printf("Update (Y/N)? :");
scanf("%c", %26amp;rep);
}
while(tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');
if(rep=='y')
{
updateRecord();
}
do
{
printf("Update another (Y/N)? :");
fflush(stdin);
scanf("%c", %26amp;rep);
}while(tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');
}
while(rep=='y');
fclose(bookfile);
}
void displayTitle(void)
{
printf("\n\t\t==UPDATE A FILE==\n");
printf("\t\t==BOOK==\n");
printf("UPDATE RECORDS:\n\n");
}
void dispRec(void)
{
printf("%s%s%s%s\n",bookrec.bookID,bo...
}
void displayRecord(void)
{
int lineCounter=0;
while(!feof(bookfile))
{
displayTitle();
while(lineCounter%26lt;10 %26amp;%26amp; !feof(bookfile))
{
dispRec();
lineCounter++;
fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);
}
if(!feof(bookfile))
{
printf("Press any key to continue..");
getch();
}
else
printf("End of file, press any key..");
getch();
}
}
void updateRecord(void)
{
char nBookid[6];
char nTitle[31];
char nAuthor[31];
char nCopyright[5];
int field;
char rep;
do
{
printf("Which field to update[1-4]? :");
scanf("%d", %26amp;field);
while(field%26gt;4)
if(field==1)
{
printf("Input new book ID:");
gets(nBookid);
}
else if(field==2)
{
printf("Input new book title:");
gets(nTitle);
}
else if(field==3)
{
printf("Input new book author:");
gets(nAuthor);
}
else
{
printf("Input new copyright:");
gets(nCopyright);
}
do
{
printf("Update another field(Y/N)? :");
scanf("%c", %26amp;rep);
}
while(tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');
} while(rep=='y');
fseek(bookfile, 1, SEEK_SET);
fwrite(%26amp;bookrec, sizeof(bookrec), 1, (bookfile));
fclose(bookfile);
}
void search(int target[6], int found, int size)
{
int i;
int count=0;
int field=0;
bookfile= fopen("BOOK.DAT", "r+");
fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);
while(!feof(bookfile) %26amp;%26amp; found!=0)
{
if(target[i]==field)
{
found=1;
}
else
{
count++;
fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);
}
}
size= count * sizeof(%26amp;bookrec);
}
Can you help me debug this program? c++ updating a record, its error is in the search function?
i will try to help, email me and tell me whether or not you are trying to reverse engineer an already created program, if you are, i will not help, but if so, ill tell you what you need to do
Reply:what is that all about i did not understand any of the language
Reply:Search has conflicting parameter types.
It is declared as
void search(int, int, int);
but as implemented with:
void search(int target[6], int found, int size);
as you can see they are not the same. But I have no idea what some of the parameters are. The size paramater is useless, you don't even use it inside or outside of the function (if you plan to use it out of the function, change it to a reference to an int).
Your search function has way too many things wrong with it to work properly. Your variable i is uninitaliazed, so target[i] will just return junk. field is always 0, so target[i] is always compared with 0 (unless this is what you intended). found isnt a reference so it isnt useful outside of the function (as any changes to it applicable inside the function). bookfile is closed with fclose() (again unless you intended this). Sorry I can't be constructive in providing a solution, but I hate trying to decipher other people's code. But I hope these bring some problems out to light.
Also, the line -
while (tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');
will always end up in a infinite loop if rep is 'y','Y','n',or 'N'. This is because rep never changes in the loop and so the statement is either always true or false. Try something like this.
while (tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n')
{
scanf("%c", %26amp;rep);
}
pokemon cards
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;ctype.h%26gt;
#include %26lt;conio.h%26gt;
#include %26lt;string.h%26gt;
void searchNupdate();
void dispRec();
void displayRecord();
void displayTitle();
void updateRecord();
void search(int, int, int);
struct book
{
char bookID[6];
char title[31];
char author[31];
char copyright[5];
}bookrec;
FILE *bookfile;
int main()
{
bookfile= fopen("BOOK.DAT", "rb");
if(bookfile==NULL)
{
printf("File does not exist, press any key..");
getch();
}
else
{
fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);
if(feof(bookfile))
{
printf("File is empty, press any key..");
getch();
}
else
searchNupdate();
}
fclose (bookfile);
return 0;
}
void searchNupdate(void)
{
char rep;
int found=0;
int target=0;
int size;
displayTitle();
do
{
printf("Input book ID to be updated:");
scanf("%d", %26amp;target);
search(target,%26amp;found,size);
if(!found)
{
printf("Record is not found\n");
}
else
{
displayRecord();
}
do
{
printf("Update (Y/N)? :");
scanf("%c", %26amp;rep);
}
while(tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');
if(rep=='y')
{
updateRecord();
}
do
{
printf("Update another (Y/N)? :");
fflush(stdin);
scanf("%c", %26amp;rep);
}while(tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');
}
while(rep=='y');
fclose(bookfile);
}
void displayTitle(void)
{
printf("\n\t\t==UPDATE A FILE==\n");
printf("\t\t==BOOK==\n");
printf("UPDATE RECORDS:\n\n");
}
void dispRec(void)
{
printf("%s%s%s%s\n",bookrec.bookID,bo...
}
void displayRecord(void)
{
int lineCounter=0;
while(!feof(bookfile))
{
displayTitle();
while(lineCounter%26lt;10 %26amp;%26amp; !feof(bookfile))
{
dispRec();
lineCounter++;
fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);
}
if(!feof(bookfile))
{
printf("Press any key to continue..");
getch();
}
else
printf("End of file, press any key..");
getch();
}
}
void updateRecord(void)
{
char nBookid[6];
char nTitle[31];
char nAuthor[31];
char nCopyright[5];
int field;
char rep;
do
{
printf("Which field to update[1-4]? :");
scanf("%d", %26amp;field);
while(field%26gt;4)
if(field==1)
{
printf("Input new book ID:");
gets(nBookid);
}
else if(field==2)
{
printf("Input new book title:");
gets(nTitle);
}
else if(field==3)
{
printf("Input new book author:");
gets(nAuthor);
}
else
{
printf("Input new copyright:");
gets(nCopyright);
}
do
{
printf("Update another field(Y/N)? :");
scanf("%c", %26amp;rep);
}
while(tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');
} while(rep=='y');
fseek(bookfile, 1, SEEK_SET);
fwrite(%26amp;bookrec, sizeof(bookrec), 1, (bookfile));
fclose(bookfile);
}
void search(int target[6], int found, int size)
{
int i;
int count=0;
int field=0;
bookfile= fopen("BOOK.DAT", "r+");
fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);
while(!feof(bookfile) %26amp;%26amp; found!=0)
{
if(target[i]==field)
{
found=1;
}
else
{
count++;
fread(%26amp;bookrec, sizeof(bookrec), 1, bookfile);
}
}
size= count * sizeof(%26amp;bookrec);
}
Can you help me debug this program? c++ updating a record, its error is in the search function?
i will try to help, email me and tell me whether or not you are trying to reverse engineer an already created program, if you are, i will not help, but if so, ill tell you what you need to do
Reply:what is that all about i did not understand any of the language
Reply:Search has conflicting parameter types.
It is declared as
void search(int, int, int);
but as implemented with:
void search(int target[6], int found, int size);
as you can see they are not the same. But I have no idea what some of the parameters are. The size paramater is useless, you don't even use it inside or outside of the function (if you plan to use it out of the function, change it to a reference to an int).
Your search function has way too many things wrong with it to work properly. Your variable i is uninitaliazed, so target[i] will just return junk. field is always 0, so target[i] is always compared with 0 (unless this is what you intended). found isnt a reference so it isnt useful outside of the function (as any changes to it applicable inside the function). bookfile is closed with fclose() (again unless you intended this). Sorry I can't be constructive in providing a solution, but I hate trying to decipher other people's code. But I hope these bring some problems out to light.
Also, the line -
while (tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n');
will always end up in a infinite loop if rep is 'y','Y','n',or 'N'. This is because rep never changes in the loop and so the statement is either always true or false. Try something like this.
while (tolower(rep)!='y' %26amp;%26amp; tolower(rep)!='n')
{
scanf("%c", %26amp;rep);
}
pokemon cards
Subscribe to:
Posts (Atom)