Click here to Skip to main content
15,897,334 members

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;
}