Click here to Skip to main content
15,032,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello there, im relatively new to programming and ive got a question. Prompt the user to choose whether to execute task A or task B. If the user selects task A, the C program will capture the current date/time ,list of users logged in the system, and the list of files details in current directory. These information will be saved in a text file named file1.txt Else if the user selects task B, the C program will display the list of files in current directory and get the user to enter the name of the file to be deleted. The C program will use the system() function call to execute the shell command to delete the file. Note that system() function can only takes in one parameter. Therefore, you ll need to use string buffer sprintf or snprintf or alternative way such as strcpy() function. this is my assignment and i have done the code but it is not working as expected. please help. when i run it i can see the user and file list but it wont be saved to the text file and the rm function is not working as well. The only thing works is the date and time. im studying linux programming.


What I have tried:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main()

{

char opt;

printf("Enter an Option A/B: ");

scanf("%c", &opt);

if (opt == 'A')

{

int list;

time_t tm;

time(&tm);

int i,j;

i = system("w");

j = system("ls -l");

printf("The Date and Time are: %s", ctime (&tm));

printf("\n");

FILE *fptr;

fptr = fopen("file1.txt", "w");

fprintf(fptr, "%s", ctime (&tm));

fprintf(fptr, "%i", i);

fprintf(fptr, "%j", j);

fclose(fptr);

}

else

{

char del[30];

printf("Enter the file to delete: ");

scanf("%c", del);

system("rm" 'del');

}

return 0;

}
Posted
Updated 7-May-21 4:33am

The integers i and j do not capture the output of the system commands. You need to redirect the output to the file as directed:
C++
system("date > file1.txt");     // start redirecting the output into the file
system("w >> file1.txt");       // double >> means append this data
system("ls -l >> file1.txt");   //


In case B you need to input a string not a single character into del, and then create a single string of the command using sprintf.
C++
char rmcmd[64];
scanf("%s", del);  // %s reads a complete string
sprintf(rmcmd, "rm %s", del); // combine the command and filename
system(rmcmd); // execute the command.
   
Comments
Profesor Jack 7-May-21 11:01am
   
Thank you so much. Case A works perfectly in case b, when i run, it shows: sh: 1: Syntax error: word unexpected (expecting ")"). im not sure why this shows because when i compiled the program, it didnt show any error
Richard MacCutchan 7-May-21 11:45am
   
I just ran that code and it works fine. Are you sure you copied it exactly? Also, can you show the actual execution including what you typed in as the file name.
Profesor Jack 7-May-21 15:11pm
   
Thank you for the reply i figured it out. turns out i didnt get any error as i was running linux on wsl and when i used ubuntu on hyper-v i got the error, i gave 64 as string length for del and when i reduced the value, it worked perfectly.
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
   
Comments
Profesor Jack 7-May-21 11:02am
   
Thank you i appreciate your help. im relatively new to this and thanks for letting me know
OriginalGriff 7-May-21 11:40am
   
You're welcome!
I didn't tried your program, but:
C++
system("rm" 'del');

is not C.
You should see how to concatenate strings
C Language: strcat function (String Concatenation)[^]
-----
Advice: double line spacing do not help in any way, it just take more space for same thing.
-----
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
	char opt;
	printf("Enter an Option A/B: ");
	scanf("%c", &opt);
	if (opt == 'A')
	{
		int list;
		time_t tm;
		time(&tm);
		int i,j;
		i = system("w");
		j = system("ls -l");
		printf("The Date and Time are: %s", ctime (&tm));
		printf("\n");
		FILE *fptr;
		fptr = fopen("file1.txt", "w");
		fprintf(fptr, "%s", ctime (&tm));
		fprintf(fptr, "%i", i);
		fprintf(fptr, "%j", j);
		fclose(fptr);
	}
	else
	{
		char del[30];
		printf("Enter the file to delete: ");
		scanf("%c", del);
		system("rm" 'del');
	}
	return 0;
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
   
Comments
Profesor Jack 7-May-21 11:03am
   
Thank you for the help, ill keep this in mind when i write programs. appreciate the insight

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900