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

Thread Tools Search this Thread
Top Forums Programming Storing input + some text into a variable (C Programming)
# 1  
Old 08-07-2011
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.
# 2  
Old 08-08-2011
How about showing what is inside your getNAME func...
# 3  
Old 08-08-2011
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.. Smilie

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

		return (globalCountryDataArray [idx].Capital);

# 4  
Old 08-08-2011
How about you show all your code so we don't have to play 20 questions?

Your use of write() actually looks correct.
# 5  
Old 08-08-2011
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);

# 6  
Old 08-08-2011
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.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 08-09-2011
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

   int ret;
   char country[300];
   printf("2) To end program, type 'end'\n\n\n");
   printf("Please enter country name > ");
   if (strcmp(country, "end") == 0)
   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 */


Last edited by aLHaNz; 08-09-2011 at 12:09 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

Storing output into a variable

My script below seems to be choking because I need the the output of the find command to be stored as a variable that can then be called by used lower in the script. #!/bin/bash cd "/resumes_to_be_completed" var1=find . -mmin -1 -type f \( -name "*.doc" -o -name "*.docx" \)... (1 Reply)
Discussion started by: binary-ninja
1 Replies

3. UNIX for Dummies Questions & Answers

Storing all the PID's in a variable.

Hi, ps -ef|awk '{print $2}' i want to store the result of the above command in a variable. I never worked with arrays in shell scripting. i tried the below code: set a=`ps -ef|awk '{print $2}'` But echo $a returns null. I want to store the content in a variable and retrieve it... (2 Replies)
Discussion started by: pandeesh
2 Replies

4. Shell Programming and Scripting

Storing o/p of a command to a variable

Hi, I have a ftp script there I want to store the o/p of the below command: sftp -b <batch file> user@password cat <batch file> get /remote/file/path/remote_file_name.csv*.gz /local/path Now the problem is that when I fire this command. Then it gives o/p as: File... (7 Replies)
Discussion started by: dips_ag
7 Replies

5. Shell Programming and Scripting

Problem in storing value to variable

#bash curVer=`cat /var/sadm/clsversion | cut -f 2 -d "_"` echo "CurVer:$curVer" ls |grep -v tar| grep -v sh| grep -v log|cut -f 1 -d "_" | sort -u >tmp1 for line in $(cat tmp1) do ver=`echo $line_$curVer` ls $line* |sort >tmp2 grep -n ${ver} tmp2 >/dev/null ... (2 Replies)
Discussion started by: rajamohan
2 Replies

6. Shell Programming and Scripting

Storing a field within a variable

Hi, I need to figure out a way to to capture the contents of a field that is separated by a pipe sign. Example Data: -100F| some other description -10C| some description | some description As you can see, the length of the field on the left of the pipe can be any length and... (3 Replies)
Discussion started by: doza22
3 Replies

7. Shell Programming and Scripting

About storing the value of wc -l into a variable and then using this value in while

Hi all, I m new to this forum. I ma facing onei issue. I have something like this: length= wc -l < b2| awk '{print $1}' where b2 is filename having detauls like: cat b2 abc1 abc4 xyc3 sbdghf4 but when I do echo "$length" it displays nothing Also I am using awk to overcome... (4 Replies)
Discussion started by: student2009
4 Replies

8. Shell Programming and Scripting

Storing value in a variable

Hi Everyone, I have a code which requires to be stored in different variables and I am achiving it like this. HOST=`echo $RMP | cut -f2 -d:` NAME=`echo $RMP | cut -f3 -d:` DIR=`echo $RMP | cut -f4 -d:` TYPE=`echo $RMP | cut -f5 -d:` Is there any other way of storing value... (2 Replies)
Discussion started by: gehlnar
2 Replies

9. Shell Programming and Scripting

storing a command in a variable

how would i go about storing this command in a variable echo "$LINE" | awk -F"|" '{print $1"|"$2"|"$3"}' i have tried FOO = ${command up there} but receive the error FOO: not found aswell as a couple of other attempt but no luck (2 Replies)
Discussion started by: nookie
2 Replies

10. Shell Programming and Scripting

Storing a variable?

I'm writing a bash shell script to backup several mysql databases. This script will run on a daily basis and send a copy to a remote FTP repository. The filenames are in the format DATE.backup.sql. How do I store the DATE variable so I can delete/move/etc the file on the FTP server the next time... (4 Replies)
Discussion started by: hoover90
4 Replies
Login or Register to Ask a Question