Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been on the forum now and I am looking for further help.

Jochen has been helping me out , however I am not following some stuff.

I would be glad if someone could just help me with letting me know if the code I have is correct or not

If its wrong, what is the exact modification to be made.

What I have tried:

#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,count);
	
	close(fd);
	
	return 0;
}
Posted
Updated 8-Aug-18 0:31am

1 solution

Why are you using the low level read to get user input? Use a friendly system call like gets_s, _getws_s[^] which reads a complete string.

Also, you have not explained what is wrong with your program.
 
Share this answer
 
Comments
Member 13933893 8-Aug-18 6:37am    
@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 .
Richard MacCutchan 8-Aug-18 7:10am    
I have no idea what shellforge is.

However, as far as I can see the only problem you have is in the following line:

fd = fd("user.dat", O_CREAT | O_RWDR, 0666);

You have typed fd( ... instead of open(. It should be:

fd = open("user.dat", O_CREAT | O_RWDR, 0666);
Member 13933893 8-Aug-18 7:32am    
@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.
Richard MacCutchan 8-Aug-18 7:45am    
Your error message does not provide enough information. When I run the code on my system it works whether the file is present or not, so I cannot guess what your failure is.
Member 13933893 8-Aug-18 8:34am    
@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?

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