15,897,334 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by Member 13933893 (Top 17 by date)
Member 13933893
8-Aug-18 8:34am
View
@Richard-MacCutchan.
Thank you for your guidance. I was able to get the program running now .
Would you be able to help me or guide me how compare 2 strings without using strcmp function?
Member 13933893
8-Aug-18 7:32am
View
@Richard-MacCutchan. Thank you for the correction.
I have corrected my code and now it looks as follows:
#define O_RDWR 0x0002
#define O_CREAT 0x0100
#define USERNAME_SIZE 10
int main()
{
int fd;
fd = open("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,count);
close(fd);
return 0;
}
-----However on running it, I am getting an error saying There was an error opening the file ( I assume the file is not getting created. )What exactly should I do in this case?
If the file is present the code runs fine.
Member 13933893
8-Aug-18 6:37am
View
@Richard-MacCutchan, I am trying to compile this in shellforge which does not allow using external libraries of C.
I am new to C system calls and hence was not aware of how to do so.
I believe the gets requires an external library if I am not wrong.
I am not sure if the program written is correct or no, thats my concern.
I want to read username and password and store it into a file.
Could you guide me if what I have done will work to write to the file or if gets_s can be used without a library file .
Member 13933893
8-Aug-18 3:09am
View
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
Member 13933893
7-Aug-18 23:14pm
View
@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;
}
Member 13933893
7-Aug-18 6:50am
View
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);
Member 13933893
7-Aug-18 6:33am
View
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
Member 13933893
7-Aug-18 5:32am
View
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
Member 13933893
6-Aug-18 22:59pm
View
#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:57pm
View
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 10:11am
View
@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.
Member 13933893
6-Aug-18 9:31am
View
@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;
}
Member 13933893
6-Aug-18 9:10am
View
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
Member 13933893
6-Aug-18 8:38am
View
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
Member 13933893
6-Aug-18 8:05am
View
#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.
Member 13933893
2-Aug-18 3:50am
View
Hello CPallini,
Thank you for the same. However I have some few doubts.
While I compile the same , I get the warning that "gets" is a dangerous function and to switch it to "fgets" wherever gets is used.
However on doing so and recompiling, I am receiving further errors now .
I am not sure if the arguements that is only the variable passed is sufficient.
What other parameters need to be passed on ?
Reference of the error :
test.c:22:3: error: too few arguments to function ‘fgets’
fgets(username);
^~~~~
In file included from test.c:1:0:
/usr/include/stdio.h:564:14: note: declared here
extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
^~~~~
test.c:29:5: error: too few arguments to function ‘fgets’
fgets(passwordone);
^~~~~
In file included from test.c:1:0:
/usr/include/stdio.h:564:14: note: declared here
extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
Member 13933893
1-Aug-18 22:23pm
View
Hi CPallini and Nelek.
Thank you for your valuable suggestions .
I am writing this program as a beginner and highly appreciate all your inputs .
After having gone through your suggestion and implementing the same , I am able to write the password only when they match. However I am not able to understand the concept of the while loop . If one of you guys coudl help me understand the same and give me a small example of an implementation it would be great.
This is what I have got till now
#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int main()
{
char username[1000];
char passwordone[1000];
char passwordtwo[1000];
FILE *fptr;
fptr = fopen("program.txt", "w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter your username\n");
gets(username);
fprintf(fptr,"%s", username);
printf("Enter your password\n");
gets(passwordone);
printf("Enter your password again\n");
gets(passwordtwo);
if (strcmp(passwordone,passwordtwo)==0)
{
printf("The Passwords match");
fprintf(fptr,"%s", passwordone);
}
else
{
printf("The passwords do not match");
}
fclose(fptr);
return 0;
}
Show More