Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys,

I am a learner in C, I need to know how to take input from user and compare them using system calls.

I am not allowed to use any library functions as they provide an error while compiling with shellforge,

Can someone help me with the same.

What I have tried:

I am a very new learner so any help will be appreciated.

I am aware of write function but cannot work around the program.
Posted
Updated 6-Aug-18 1:20am

1 solution

If you are aware of the write() function you should be also aware of the read() function. To read from the keyboard pass zero as first parameter (file descriptor stdin).

Use a while loop to read single characters and store them in an array until a new line ('\n') occurs (user pressed the ENTER key). Store the number of characters read in a variable and/or terminate the string with a NULL byte.

Compare the two arrays. The first check is comparing the lengths which has been stored in variables after reading the user input. If they do not match, the comparison has failed. Otherwise iterate over the arrays using a for loop comparing each character until there is a mismatch (comparison failed) or the loop finishs (comparision succeeded).

Finaly use open() to create the file to write the output.

If you need a description for the system functions, you can use the Linux man pages like read(2) - Linux manual page[^]. They can be simply found by searching the web for "man 2 <function_name>". The "2" is the man page section for system calls. All functions from this section should be useable with ShellForge.
 
Share this answer
 
Comments
Member 13933893 6-Aug-18 8:05am    
#include <stdio.h>


int main()
{
int fd;
char username[10];
int ret;

fd = open("test.text", O_CREAT | O_RDWR,0666);
if(fptr == -1)
{
printf("Error!");
exit(1);
}

printf("Enter your username");
ret = read(0, username, 10);
write(fd,username,strlen(username));



@User-2223753 | Jochen, this is what I have till now, please can you guide me further.
Let me know if I am on the right track and how to exactly proceed with comparing two passwords. if any sample code could be provided.
Jochen Arndt 6-Aug-18 8:18am    
printf() and strlen() are C library functions. You have to use write(1, ...) instead of printf. Just search for "man printf" (or enter it on a Linux shell if present) and you will notice that they belong to section 3 which contains the C library functions.

You must also use a loop to read character by character from the keyboard as explained in my solution (which avoids also using strlen() when counting the characters in the loop).

Your code will wait until 10 characters has been entered and does not append a NULL byte so that strlen() and printf() would fail anyway.
Member 13933893 6-Aug-18 8:38am    
Having gone through your suggestions and details.

This is what I have got till now :
#include <stdio.h>


int main()
{
int fd;
char username[10];
char passwordone[15];
char passwordtwo[15];

int ret;
fd = open("test.text", O_CREAT | O_RDWR,0666);
if(fptr == -1)
{
printf("Error!");
exit(1);
}

write(1,"Enter the username",18);
ret = read(0, username, 10);
write(fd,username,10));

while (1)
{
write(1,"Enter the password",18);
read(0, passwordone, 15);

write(1,"Enter the password again",25);
read(0, passwordtwo, 15);

if ( strcmp(passwordone, passwordtwo) == 0)
break;

write(1,"The passwords do not match",28);
}

write(1,"The Passwords match",20);
write(fd, passwordone, 15);

return 0;

}


Could you let me know if this is correct , if not could you help me and point out where the mistakes are .
Highly appreciate your help
@User-2223753 | Jochen
Jochen Arndt 6-Aug-18 8:51am    
You have to read single characters in a while loop like
char kbinput[KBINPUT_SIZE];
char c;
int count = 0;
do
{
 read(0, &c, 1);
 if (c == '\n')
  break;
 kbinput[count++] = c;
}
while (count < KBINPUT_SIZE - 1);
kbinput[count] = 0;


Also: strcmp() is again a C library function.
Have you tried "man strcmp" as suggested?
Member 13933893 6-Aug-18 9:10am    
Would request your further assistance, the loop is basically reading input from the keyboard and storing into character C until the user presses Enter key.

I am not able to get the second part as to what that is doing .

Also, could you please help me implement the check in my program .
This is what I have done and I do not know if its correct or not .

#include <stdio.h>


int main()
{
int fd;
char username[10];
char kbinput[KBINPUT_SIZE];
char c;
int count = 0;

int ret;
fd = open("test.text", O_CREAT | O_RDWR,0666);
if(fptr == -1)
{
printf("Error!");
exit(1);
}

write(1,"Enter the username",18);
ret = read(0, username, 10);
write(fd,username,10));


do
{
read(0, &c, 1);
if (c == '\n')
break;
kbinput[count++] = c;
}
while (count < KBINPUT_SIZE - 1);
kbinput[count] = 0;
write(1,"The Passwords match",20);
write(fd, passwordone, 15);

return 0;

}


@User-2223753 | Jochen

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