Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripts 1

Status
Not open for further replies.

funstur

Technical User
Feb 15, 2001
66
GB
I have just started using C in NT, & would be grateful for any advice as regards user input. I am required to create a program that allows user to create directories & sub-directories, along with an options to discontinue the process or run other programs.

Many thanks Funstur
 
Check out this code It will give you the hints req.

# include <stdio.h>
# include <string.h>
# include <dos.h>

int main()
{
char name[8], command[20]=&quot;md &quot;;

printf(&quot;Enter absolute path for Directory to be created :&quot;);
scanf(&quot;%s&quot;,name);
strcat(command,name);
system(command);
return 1;
}


Regards,
SwapSawe.
 
># include <stdio.h>
># include <string.h>
># include <dos.h>

Don't need this for any of the stuff below. Maybe you were thinking it was needed for system(), which is prototyped in <stdlib.h>

>int main()
>{
>char name[8], command[20]=&quot;md &quot;;

>printf(&quot;Enter absolute path for Directory to be created :&quot;);

fflush(stdout);

Gotta make sure the user will see our prompt right away.

>scanf(&quot;%s&quot;,name);

This is equivalent to using gets() because the %s specifier by itself doesn't limit the number of bytes that can be written to name.

If you must use scanf(), put a limit:

scanf(&quot;%7s&quot;,name);

>strcat(command,name);

This is safe given the correction to scanf(), but without it you're asking for more trouble.

>system(command);
>return 1;

C only guarantees that the values of 0, EXIT_SUCCESS and EXIT_FAILURE are portable. Usually, but not necessarily, EXIT_FAILURE is 1.

>}

To the OP:

Post the code you've attempted and we can help. For your directory functions, it's highly compiler-dependent as to which functions will be provided. Take a look at your compiler's documentation.

The example Swap provided will work, but system() calls are time-consuming, particularly if you use them several times as your program specs suggest. There is almost always a compiler-specific extension that's a better alternative than system().

Russ
bobbitts@hotmail.com
 
Thanx Russ for poiting out my error u r always there when I commit a mistake and thats why I have started liking you.
:-Q
SwapSawe.
 
Hi rbobbitt & swapsawe

This is an element of code where I am having problems.

looping:; /* Pointer for looping the program, line 30*/

printf(&quot;Do you want to create vfx shot directory y/n...&quot;);
scanf(&quot;%c&quot;, &b);

if(&b !=&quot;y&quot;)
goto exit;
else {
printf(&quot;Please enter the vfx code (2 letters e.g. tr, pl) \n&quot;);
printf(&quot;followed by a space & the vfx number (3 Numbers e.g. 030): &quot;);
fflush(stdout); /* DO I NEED THIS HERE? */
scanf(&quot;%c %d&quot;, &c, &d);
strcat(command, name, &c)&quot;_shp_&quot;(&d); /* This generates file *_shp_* line 49 I NEED HELP HERE*/
system(command);
goto looping; /* loop for pointer the program */
}

error message from complier is as follows

G:\Programs\C\mset3dx.c(49) : warning C4020: 'strcat' : too many actual parameters
G:\Programs\C\mset3dx.c(49) : error C2143: syntax error : missing ';' before 'string'
G:\Programs\C\mset3dx.c(49) : error C2064: term does not evaluate to a function
Error executing cl.exe.

mset3dx.obj - 2 error(s), 2 warning(s)

many thanks for you help!! Funstur

I acn send you the file code via email if you need more info?
 
>looping:; /* Pointer for looping the program, line 30*/

You don't need the semi-colon here.

> printf(&quot;Do you want to create vfx shot directory y/n...&quot;);

fflush(stdout);

Since output is probably line-buffered by default, this ensures that your user sees your prompt before he/she can enter input.

> scanf(&quot;%c&quot;, &b);
>
> if(&b !=&quot;y&quot;)

I'm guessing that b is declared like this:

char b;

If that's the case, you'll want to change this to:

if (b!='y')

The quotes are only used to enclose string literals, single quotes are for character constants.

You may also want to allow for both lowercase and uppercase:

if (b!='y' && b!='Y')

> goto exit;
> else {
> printf(&quot;Please enter the vfx code (2 letters e.g. tr, pl) \n&quot;);
> printf(&quot;followed by a space & the vfx number (3 Numbers e.g. 030): &quot;);
> fflush(stdout); /* DO I NEED THIS HERE? */

Yes, see above.

> scanf(&quot;%c %d&quot;, &c, &d);
> strcat(command, name, &c)&quot;_shp_&quot;(&d); /* This generates file *_shp_* line 49 I NEED HELP HERE*/

strcat() accepts two arguments, both of which are pointers to char, here you're giving it 3 arguments and after the right parentheses comes a quote which violates the C grammar.

It looks like you're trying to build a string out of the stuff you've collected so far. This is a job for sprintf():

sprintf(command,&quot;%s%c_shp_%d&quot;,name,c,d);

This assumes that name is either an array of char or a pointer to char, c is type char and d is type int or short int.

You'll want to check the return value to make sure that the conversion worked and you'll also want to ensure that command is big enough to hold any possible string that will result from this combination.

> system(command);
> goto looping; /* loop for pointer the program */
> }

You should probably avoid goto unless there's a really compelling reason to use it. i.e. There's no other way to do something without going to great pains and the goto version is clearer and easier to maintain.

Also, this is a question that really deserves its own thread. People that are willing and able to help might not see it, thinking it's a part of this thread which they may have already read and passed up.

HTH,

Russ
bobbitts@hotmail.com
 
Geez, sorry about that last comment wrt starting your own thread. I must *really* be tired. For some reason it didn't occur to me that you were the original poster. What's worse, I'm the one that asked you to post your code!

:~/

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top