Storing input + some text into a variable (C Programming)

Hey guys got a slight problem here, I kindda new to socket programming in C so I need some guide on how to store something like this in a variable.

            printf ("%s Name      : %s\n", id,getNAME(name));
           

name is declared as name[100].
The getName is a function.

So what I'm trying to say is that I want to store it inside a variable called

char new[100];

Then i can write it out to the client side

  n = write(clientFd,name,strlen(name));

Anyone can help me with this I kinda confused with it.

How about showing what is inside your getNAME func...

Ok.. so actually I was getting the information from a text file.
User enter the input from the client side, lets say Netherlands.
The server will read the information and returns back to the server showing the currency.
I know its not printf to print to the client side, but write instead, i'm not sure how though.. :wall:

char *capital = getCapital(country);
printf ("%s Capital       : %s\n", country, capital);
char* getCapital (const char* countryName)
{

	int idx = findCountryRecord (countryName);

	if (idx < 0)
	{
		printf ("Country '%s' not found!\n", countryName);

		return (NULL);	
	}

	else
		return (globalCountryDataArray [idx].Capital);
}

How about you show all your code so we don't have to play 20 questions?

Your use of write() actually looks correct.

Haha sorry man I would like to show but the server and client codes are way to long..

I just want to know how to write it in the client side using the write().
Using printf will just print the output at the server side.

printf ("%s Capital       : %s\n", country, capital);

Depends entirely what you're write() ing to. write() is not a function like printf which always goes to the same place, write() writes to what you tell it to. Without seeing your code I don't know what it should be writing to.

Also depends how the other end is reading it, whether it expects a newline, etc.

Your original code looked okay, assuming there actually were clientFd and name variables.

1 Like

I am able to type blank space and write everything into the client.
However one last problem.
When the user enter a country lets say China, It will returned everything I write but the terminal just hanged there and I can't type anything even there is a loop to ask user enter again.
I know there is something on at the readOutput function but I dunno where it go wrong

User enter input in the client and readOutput will show what the server has wrote

Client side

while(1){

   int ret;
   char country[300];
   printf("2) To end program, type 'end'\n\n\n");
   printf("Please enter country name > ");
   scanf("%[^\n]s",country);
   if (strcmp(country, "end") == 0)
   break;
   else
   write(clientFd,country,strlen(country));
   readOutput (clientFd); 
   }
   close (clientFd); /* Close the socket */
   exit (/* EXIT_SUCCESS */ 0); /* Done */

 }

function readOutput in client

readOutput (fd)

 int fd;

 {

   char str[200];
    while (readLine (fd, str)) /* Read lines until end-of-input */
     printf ("%s\n", str); /* Echo line from socket */	

 }

function readLine in client side

readLine (fd, str)

 int fd;

 char* str;



 /* Read a single NULL-terminated line */



 {

   int n;

   do /* Read characters until NULL or end-of-input */

     {

       n = read (fd,str, 1); /* Read one character */

     }

   while (n > 0 && *str++ != 0);

   return (n > 0); /* Return false if end-of-input */

 }

Never use scanf if you can help it. It will stop at the first character it considers invalid and leave it to be read next time -- so next time you scanf to read a line it instantly fails.

If you want to input individual lines, scanf is overkill anyway -- use the purpose-built line reading function, fgets(buffer, size, file); It adds a NULL to the end like most string functions. It also leaves in the newline, just to warn you.

And if you want to use scanf to extract parts of a line, you can do sscanf(buffer, "command string", &var1, &var2, ...) to scan the string you just got with fgets, no worries about scanf leaving junk in the buffer that way.

There's also a printf function which writes to a buffer, sprintf(buffer, "commandstring", arg, arg, ...); which you can use to conveniently format data before feeding it into write() if you want.

Your readline function makes an assumption, namely, that the stream contains NULLs. Since you haven't posted most of your code, I have no idea at all if these assumptions are true everywhere. But I'm guessing they're not.

write() is not a stdio function, it's a raw system call -- it doesn't add anything for you, it sends bytes as given without question. So write(fd, "abcd", strlen("abcd")) sends four bytes with no NULL terminator. strlen()+1 will send the NULL assuming there is one.

You do have the right idea in using a terminator, since a write on one end might not all arrive in one read on the other.