Click here to Skip to main content
14,932,329 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.
   
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
Jochen Arndt 6-Aug-18 9:19am
   
You have to use the snippet from my previous post for every input string. That is: For username, password and passord2.
Member 13933893 6-Aug-18 9:31am
   
@User-2223753 | Jochen, I have done as suggested , changed a few variables , requesting you to go through and let me know if its alright. I am not sure where the comparison is happening .

A small request if you could modify my program and show me where I am going wrong .

#include <stdio.h>


int main()
{
int fd;
char username;
char kbinput[KBINPUT_SIZE];
char passwordone;
char passwordtwo;
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);
do
{
read(0, &username, 1);
if (username == '\n')
break;
kbinput[count++] = username;
}
while (count < KBINPUT_SIZE - 1);
kbinput[count] = 0;
write(fd,username,10));


write(1,"Enter the password",18);
do
{
read(0, &passwordone, 1);
if (passwordone == '\n')
break;
kbinput[count++] = passwordone;
}
while (count < KBINPUT_SIZE - 1);
kbinput[count] = 0;


write(1,"Enter the password again",25);
do
{
read(0, &passwordtwo, 1);
if (passwordtwo == '\n')
break;
kbinput[count++] = passwordtwo;
}
while (count < KBINPUT_SIZE - 1);
kbinput[count] = 0;


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

return 0;

}
Jochen Arndt 6-Aug-18 10:02am
   
Please take some time and think before writing code.

Would it make sense to store all strings entered by the user in the same single array kbinput?
Member 13933893 6-Aug-18 10:11am
   
@User-2223753 , Since I am very new to codign, thank you for all your inputs , as suggested, I have created 3 different arrays( kbinput, kbinputpassone and kbinputpasstwo) . However, I am still not sure where the password matching is happening before writing to the file .

Following is what I have done until now as per your suggestions :
---------

#include <stdio.h>


int main()
{
int fd;
char username;
char kbinput[KBINPUT_SIZE];
char kbinputpassone[KBINPUTPASSONE_SIZE];
char kbinputpasstwo[KBINPUTPASSTWO_SIZE];
char passwordone;
char passwordtwo;
int count = 0;

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

write(1,"Enter the username",18);
do
{
read(0, &username, 1);
if (username == '\n')
break;
kbinput[count++] = username;
}
while (count < KBINPUT_SIZE - 1);
kbinput[count] = 0;
write(fd,username,10);


write(1,"Enter the password",18);
do
{
read(0, &passwordone, 1);
if (passwordone == '\n')
break;
kbinputpassone[count++] = passwordone;
}
while (count < KBINPUTPASSONE_SIZE - 1);
kbinputpassone[count] = 0;


write(1,"Enter the password again",25);
do
{
read(0, &passwordtwo, 1);
if (passwordtwo == '\n')
break;
kbinputpasstwo[count++] = passwordtwo;
}
while (count < KBINPUTPASSTWO_SIZE - 1);
kbinputpasstwo[count] = 0;


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

return 0;

}

------------------------------------------
Would like to know where the comparison and check for matching the password is happening and if its not happening, could you please add that for me and correct my program. I will go through it and try to understand everything mentioned by you and use it for future.
Jochen Arndt 6-Aug-18 10:42am
   
Where it should happen is quite obvious:
After the data has been entered and before writing to the file.
Then re-read my solution on how to do this. It is basically implementing your own version of memcmp().
Member 13933893 6-Aug-18 22:57pm
   
As suggested , I have implemented a check to see if the array lengths match, requesting you to check if the same is correct or not and if possible guide further.

#include <stdio.h>


int main()
{
int fd;
char username;
char kbinput[KBINPUT_SIZE];
char kbinputpassone[KBINPUTPASSONE_SIZE];
char kbinputpasstwo[KBINPUTPASSTWO_SIZE];
char passwordone;
char passwordtwo;
int count = 0;
int result;
int ret;


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

write(1,"Enter the username",18);
do
{
read(0, &username, 1);
if (username == '\n')
break;
kbinput[count++] = username;
}
while (count < KBINPUT_SIZE - 1);
kbinput[count] = 0;
write(fd,username,10);


write(1,"Enter the password",18);
do
{
read(0, &passwordone, 1);
if (passwordone == '\n')
break;
kbinputpassone[count++] = passwordone;
}
while (count < KBINPUTPASSONE_SIZE - 1);
kbinputpassone[count] = 0;


write(1,"Enter the password again",25);
do
{
read(0, &passwordtwo, 1);
if (passwordtwo == '\n')
break;
kbinputpasstwo[count++] = passwordtwo;
}
while (count < KBINPUTPASSTWO_SIZE - 1);
kbinputpasstwo[count] = 0;


result = memcmp(passwordone,passwordtwo,10)
if (result == 0)
write(1,"Password length is equal",25);
else
write(1,"Password lengths do not match",30);


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

return 0;

}
Member 13933893 6-Aug-18 22:59pm
   
#include <stdio.h>


int main()
{
int fd;
char username;
char kbinput[KBINPUT_SIZE];
char kbinputpassone[KBINPUTPASSONE_SIZE];
char kbinputpasstwo[KBINPUTPASSTWO_SIZE];
char passwordone;
char passwordtwo;
int count = 0;
int result;
int ret;


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

write(1,"Enter the username",18);
do
{
read(0, &username, 1);
if (username == '\n')
break;
kbinput[count++] = username;
}
while (count < KBINPUT_SIZE - 1);
kbinput[count] = 0;
write(fd,username,10);


write(1,"Enter the password",18);
do
{
read(0, &passwordone, 1);
if (passwordone == '\n')
break;
kbinputpassone[count++] = passwordone;
}
while (count < KBINPUTPASSONE_SIZE - 1);
kbinputpassone[count] = 0;


write(1,"Enter the password again",25);
do
{
read(0, &passwordtwo, 1);
if (passwordtwo == '\n')
break;
kbinputpasstwo[count++] = passwordtwo;
}
while (count < KBINPUTPASSTWO_SIZE - 1);
kbinputpasstwo[count] = 0;


result = memcmp(passwordone,passwordtwo,10)
if (result == 0)
write(1,"Password length is equal",25);
else
write(1,"Password lengths do not match",30);


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

return 0;

}
Jochen Arndt 7-Aug-18 2:30am
   
Again: Take your time, read my posts and think about what to do.

"It is basically implementing your own version of memcmp()"
That means you have to write code doing the same as memcmp() because memcmp() is a C library function.

"The first check is comparing the lengths which has been stored in variables after reading the user input."

That means you need the length of password1 and password2 in two variables.
Member 13933893 7-Aug-18 5:32am
   
Hi Jochen , I am able to understand what you are trying to say but I am not sure of how to add it in my program , I have tried to search for references but I am having a lot of difficulty in achieiving this, hoping you could provide me further guidance and clarity on my code
Jochen Arndt 7-Aug-18 6:05am
   
It does not help you to learn if I gave you ready to use code when you do not understand what the code is actually doing.

I will repeat: Take your time and think about it.
What do you need to compare two strings?

Hint: Reading the documentation of memcmp() or strcmp() might help even when not allowed to use them.

You obviously need two variables containing the two strings. And you need the length for memcmp(). For strcmp() the strings must be NULL terminated because that determines the length internally (if you don't know about this, read about the strings in C).

Think about the code I gave you to read the user input. Do you understand what the code is doing?
I repeat:
It reads single characters entered by the user and stores them in an array (the input string). While doing that it counts the number of characters entered. It stops reading when the user has pressed the ENTER key wich is indicated by reading a new line character ('\n' resp. 0x0A). It finally appends a NULL byte to the array to create a valid C string.

So you have then an array (input string) and it's length.

Do this for all required user inputs storing the results in different variables. Use these variables to perform the additional tasks (here compare two strings).

When having a task to be implemented by a programming language, perform an analysis similar to the above before writing any line of code.

Then break it into subtasks (here: read input from keyboard, compare strings, write to file) and define how these parts communicate which each other (here: the variables required for input and output of the tasks).

When all the above is done, start implementing it by code.
Member 13933893 7-Aug-18 6:33am
   
char kbinput[KBINPUT_SIZE]; // The array in which the complete string is stored.
char c; // The single character it is reading
int count = 0;
do
{
read(0, &c, 1); // Reading 1 alphabet into variable c
if (c == '\n') // When you press enter, exit the loop.
break;
kbinput[count++] = c; // increments the array to the next position so as to take the next alphabet?
}
while (count < KBINPUT_SIZE - 1);
kbinput[count] = 0;

I do not understand the last 2 lines, can you please clarify that for me
Jochen Arndt 7-Aug-18 6:38am
   
C basics:
Arrays, array indexes, post increment operator
While loop (here: avoid out of bound array accessing)
Null terminated strings (char array)
Member 13933893 7-Aug-18 6:50am
   
I am tackling the problem piece by piece as recommended and sorry for all the questions. I have just made the code to open the file and read user input and write it to the file .

Could you tell me if this much is correct?

----

int main()
{

int fd;

fd = fd("user.dat", O_CREAT | O_RWDR, 0666);
if (fd == -1)
{
write(1,"There is an error opening the file",35);
}

write(1,"Enter the username",18);


char username[USERNAME_SIZE];
char c;
int count = 0;
do
{
read(0, &c, 1);
if (c == '\n')
break;
username[count++] = c;
}
while (count < USERNAME_SIZE - 1);
username[count] = 0;

write(fd,username,USERNAME_SIZE);
Jochen Arndt 7-Aug-18 14:19pm
   
What do you think is the purpose of the count variable?
It is the number of characters that has been entered and specifies which of the input array items are valid.

USERNAME_SIZE defines the size of the array and so the max. number of characters allowed to be entered and must be defined first with a #define statement.

So you have to use count when writing to file or screen. Otherwise you would have additional garbage characters in your file or on the screen.
Member 13933893 7-Aug-18 23:14pm
   
@User-2223753 | Jochen, thank you for the explanation. I have made the changes as you mentioned. Hoping that it's correct this time.
Please correct wherever wrong and I will try to implement the same for password 1 and password 2 and then later move onto comparison
---

#DEFINE USERNAME_SIZE=10

int main()
{

int fd;

fd = fd("user.dat", O_CREAT | O_RWDR, 0666);
if (fd == -1)
{
write(1,"There is an error opening the file",35);
}

write(1,"Enter the username",18);


char username[USERNAME_SIZE];
char c;
int count = 0;
do
{
read(0, &c, 1);
if (c == '\n')
break;
username[count++] = c;
}
while (count < USERNAME_SIZE - 1);
username[count] = 0;

write(fd,username,sizeof(count));

close(fd);

return 0;
}
Jochen Arndt 8-Aug-18 3:06am
   
Please read the documentation if use a keyword or function.

The count variable contains the number if characters entered. That is what you want. sizeof() is the fixed size in bytes a variable occupies in memory. For an int that is 4 bytes on most platforms.

Read also about the #define statement. It does not require a '=' and must be lower case:
#define USERNAME_SIZE 10

Such can be also detected by just compiling the code.
Member 13933893 8-Aug-18 3:09am
   
Okay so the correct way to write to the file will be :
write(fd,username,count);

fd being the file descriptor, username being the array, count has the number of alphabets that needs to be written?

I hope my understanding of this is correct.

@User-2223753

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