Clearing Data in an Array


 
Thread Tools Search this Thread
Top Forums Programming Clearing Data in an Array
# 1  
Old 05-15-2007
Clearing Data in an Array

How would I clear all data from an array? Here is what I'm doing:
Code:
while (1) {
	if ((numbytes = recv(sock2, buf, 100, 0)) == -1) {
		perror("recv");
	}
	printf("%s : %s", inet_ntoa(their_addr.sin_addr), buf);
}

buf[] still has extra data in it, so when I print it, things from previous data is put in there.

Of course, that is only the one part of the program, but it was unnecessary to put the entire program in the post
# 2  
Old 05-15-2007
From this code it looks like you don't understand null terminated strings.

Code:
while (1) {
	if ((numbytes = recv(sock2, buf, sizeof(buf), 0)) ==-1) {
		perror("recv");
                          break;
	}
	if (!numbytes) {
		printf("EOF\n");
                          break;
	}
	printf("%s : ", inet_ntoa(their_addr.sin_addr));
	fwrite(buf,numbytes,1,stdout);
             printf("\n");
}

memset will clear an array of characters to any value you want, eg

Code:
memset(buf,0,sizeof(buf));

# 3  
Old 05-16-2007
Thanks, memset() works perfectly. I just set all values to '\0'.
# 4  
Old 05-16-2007
1. Is 'buf' one hundred bytes long?

2. If so what happens when you receive one hundred bytes?

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read file and get the data then put it in array

Hi, I have a file called "readfile" it contains below parameters #cat readfile word=/abc=225,/abc/cba=150 three=12 four=45 five=/xyz/yza likewise multiple line. From the above file, I have to read "word" output should be like, /abc /abc/cba these values need to be put in... (3 Replies)
Discussion started by: munna_dude
3 Replies

2. Programming

C Data Structure to represent a Sparse Array

Which data structure will be most appropriate to represent a sparse array? (1 Reply)
Discussion started by: rupeshkp728
1 Replies

3. Shell Programming and Scripting

reading data from a file to an array

I need some help with this code below, i doesnt know why it will run twice with my function, but my function only got if else, any other way that can read line and put into array? while read line; do read -A array <<<$line n=${#array} for ((i=1;i<$n;i++)); do print... (1 Reply)
Discussion started by: gavin_L
1 Replies

4. Homework & Coursework Questions

awk - filtering data by if --> into an array

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: my data in csv-format ... ... 13/08/2012,16:30,303.30,5.10,3,2,2,1,9360.0,322... (13 Replies)
Discussion started by: IMPe
13 Replies

5. Shell Programming and Scripting

awk - filtering data by if --> into an array

Hi my data is in csv-format ... ... 13/08/2012,16:30,303.30,5.10,3,2,2,1,9360.0,322 13/08/2012,16:40,305.50,5.00,3,2,2,1,12360.0,322 13/08/2012,16:50,319.90,3.80,3,2,1,0,2280.0 13/08/2012,17:00,326.10,3.50,3,2,1,1,4380.0,321 13/08/2012,17:10,333.00,3.80,3,3,1,0,2280.0... (1 Reply)
Discussion started by: IMPe
1 Replies

6. Shell Programming and Scripting

Storing data in perl 2D array

Respected All, Kindly help me out. I have got file listings in a directory like this: -rw-r--r-- 1 root root 115149 2011-11-17 07:15 file1.stat.log -rw-r--r-- 1 root root 115149 2011-11-18 08:15 file2.stat.log -rw-r--r-- 1 root root 115149 2011-11-19 09:15 file3.stat.log -rw-r--r-- 1... (2 Replies)
Discussion started by: teknokid1
2 Replies

7. Shell Programming and Scripting

problem to read data in array

My input is a long list of data start with "#": #read_1 123456898787987 #read_2 54645646540646406 #read_3 4654564654316 . . I got a bit confusing about how to set all in an array first. And then when I run a program name "statistic_program", it will read the array in scalar and do... (24 Replies)
Discussion started by: patrick87
24 Replies

8. Shell Programming and Scripting

Unable to set a data to array

Hi All, Iam trying to set the value to the array... Still its not happening Following is the code: #!/usr/bin/ksh filenames="x"; filenames="y"; echo $filenames; echo $filenames; O/P: x x Iam expecting (2 Replies)
Discussion started by: kiranlalka
2 Replies

9. Shell Programming and Scripting

sorting data using array in ksh

plz help me..........i have a ksh script that sorts data in ascending order. the 1st half is correct,but for the line no 31 its showing problem 1 #!/bin/ksh 2 3 4 5 echo "Enter the array length" 6 read num 7 8 9 echo "enter the... (4 Replies)
Discussion started by: ali560045
4 Replies

10. Shell Programming and Scripting

getting data list into a hash array

Begginer alert!! I have just started learning perl and have got stuck. I need to initialize a hash array with values from a list. I've been told to do this using a split command, but its just not happening. Here is the script and output: SCRIPT: #!/bin/perl system("clear"); ... (5 Replies)
Discussion started by: topcat8
5 Replies
Login or Register to Ask a Question