Sponsored Content
Top Forums Programming appending space in binary data in c Post 302106817 by hegemaro on Monday 12th of February 2007 08:30:52 PM
Old 02-12-2007
Presumably you have a buffer filled with data and you know the length. Given those two bits of information, you simply set the "length" byte to space and increment length. For example:


Code:
#include <stdio.h>

int main ( void )
{
    char        Buffer [1024] = "ABC\0DEF\n";
    int         Length = 8;

    Buffer [Length++] = ' ';

    fwrite (Buffer, 1, Length, stdout);
/*  or
     write (1, Buffer, Length);
*/

}

For the same reason you can not use strcat, you can not use the standard printf(3C) functions with a "%s" tag. It would stop at the NUL byte '\0' which is at the 4th byte (or offset 3).

I hope this helps.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

help on appending data to existing data

I need to know how to record the hostname, date/time and all of the process and send it all to one file. I know that the commands I need are hostname, date and ps but I don't know how to do them all and send them all to the same file. Please help! (1 Reply)
Discussion started by: precious51980
1 Replies

2. UNIX for Advanced & Expert Users

Convert Binary data to ascii data

Friends, I've tried on solaris, but I could n't get ascii data dd if=binaryinputfile bs=1 skip=3800 count=4 | od -t u4 output : INDBU3:/usr/users/FTAMUSER/kk $ dd if=SMP20041006173649188151 bs=1 skip=3800 count=4 | od -t u4 4+0 records in 4+0 records out 0000000 0000000000 0000004... (4 Replies)
Discussion started by: krishna
4 Replies

3. Shell Programming and Scripting

Appending data at the first and last line of a file

Hi, Am trying to write a shell script which will append a header and a footer to an existing file. Header will contain details like the current date while the footer will contain the no: of records listed in the file. I know we can use the CAT command, but i have no clue abt the syntax to... (4 Replies)
Discussion started by: brainstormer
4 Replies

4. Shell Programming and Scripting

appending space to variable

Hi I need to write a script where there the user enters 3 input parameter variable number the program should ask the user left or right if it is left , the number specified that many spaces should be added to the value in front of the value and saved in the samee variable itself and if it is... (5 Replies)
Discussion started by: viv1
5 Replies

5. Shell Programming and Scripting

appending data to file

Hi. I wrote a very simple script and it doesn't work :( It is supposed to go to a certain directory, execute some command and append the output to the file "expo.dat" what it does is that it writes to the file only one entery. I dont know if Im using the write synthax for "append". Here is... (3 Replies)
Discussion started by: Enigma08
3 Replies

6. Shell Programming and Scripting

Appending data into a variable

Hi, I would like to know if it's possible to append data into a variable, rather than into a file. Although I can write information into a temporary file in /tmp, I'd rather if possible write into a variable, as I don't like the idea that should my script fail, I'll be polluting the server with... (5 Replies)
Discussion started by: michaeltravisuk
5 Replies

7. Shell Programming and Scripting

Blank Space is not appending in each row of CSV File - Shell Script

I am calling SQL script in my UNIX Shell script and trying to create the CSV file and my last column value of each row is 23 blank spaces. In my SQL script,the last column is like below. RPAD(' ',23,' ') -- Padding 23 blank Spaces The CSV file is generated but the sapce(23 spaces) is... (2 Replies)
Discussion started by: praka
2 Replies

8. Shell Programming and Scripting

appending previous data.

I have on file abc.txt abc.txt: 20090807 Now I want to delete empty lines which has tap/whit spaces from abc.txt .and store the date value in the file into variable.some processs will update the this file with some date . if the process updtes thiis file with empty string , write the... (3 Replies)
Discussion started by: Gopal_Engg
3 Replies

9. Shell Programming and Scripting

Appending timestamp to a file. Why do I need this space ?

Version Info $ cat /etc/redhat-release Red Hat Enterprise Linux Server release 5.4 (Tikanga) $ $ echo $0 -ksh I was trying to append date to the file name. The following syntax has worked $ touch HELLO-`date '+%d-%b-%Y'`.txt $ ls -alrt HELL* -rw-r--r-- 1 rlapp oinstall 0 Feb 20... (2 Replies)
Discussion started by: John K
2 Replies

10. Shell Programming and Scripting

Help with data appending to a file

Hi I have a file called text.txt contains x y z when i run a command i will get output like below x 20 z 30 i want to insert x, z value in text.txt file and should be like this x 20 y 0 z 30 can anyone help me please? (1 Reply)
Discussion started by: siva kumar
1 Replies
Tcl_DString(3)						      Tcl Library Procedures						    Tcl_DString(3)

__________________________________________________________________________________________________________________________________________________

NAME
Tcl_DStringInit, Tcl_DStringAppend, Tcl_DStringAppendElement, Tcl_DStringStartSublist, Tcl_DStringEndSublist, Tcl_DStringLength, Tcl_DStringValue, Tcl_DStringSetLength, Tcl_DStringTrunc, Tcl_DStringFree, Tcl_DStringResult, Tcl_DStringGetResult - manipulate dynamic strings SYNOPSIS
#include <tcl.h> Tcl_DStringInit(dsPtr) char * Tcl_DStringAppend(dsPtr, bytes, length) char * Tcl_DStringAppendElement(dsPtr, element) Tcl_DStringStartSublist(dsPtr) Tcl_DStringEndSublist(dsPtr) int Tcl_DStringLength(dsPtr) char * Tcl_DStringValue(dsPtr) Tcl_DStringSetLength(dsPtr, newLength) Tcl_DStringTrunc(dsPtr, newLength) Tcl_DStringFree(dsPtr) Tcl_DStringResult(interp, dsPtr) Tcl_DStringGetResult(interp, dsPtr) ARGUMENTS
Tcl_DString *dsPtr (in/out) Pointer to structure that is used to manage a dynamic string. const char *bytes (in) Pointer to characters to append to dynamic string. const char *element (in) Pointer to characters to append as list element to dynamic string. int length (in) Number of bytes from bytes to add to dynamic string. If -1, add all characters up to null terminating character. int newLength (in) New length for dynamic string, not including null terminating character. Tcl_Interp *interp (in/out) Interpreter whose result is to be set from or moved to the dynamic string. _________________________________________________________________ DESCRIPTION
Dynamic strings provide a mechanism for building up arbitrarily long strings by gradually appending information. If the dynamic string is short then there will be no memory allocation overhead; as the string gets larger, additional space will be allocated as needed. Tcl_DStringInit initializes a dynamic string to zero length. The Tcl_DString structure must have been allocated by the caller. No assump- tions are made about the current state of the structure; anything already in it is discarded. If the structure has been used previously, Tcl_DStringFree should be called first to free up any memory allocated for the old string. Tcl_DStringAppend adds new information to a dynamic string, allocating more memory for the string if needed. If length is less than zero then everything in bytes is appended to the dynamic string; otherwise length specifies the number of bytes to append. Tcl_DStringAppend returns a pointer to the characters of the new string. The string can also be retrieved from the string field of the Tcl_DString struc- ture. Tcl_DStringAppendElement is similar to Tcl_DStringAppend except that it does not take a length argument (it appends all of element) and it converts the string to a proper list element before appending. Tcl_DStringAppendElement adds a separator space before the new list element unless the new list element is the first in a list or sub-list (i.e. either the current string is empty, or it contains the single charac- ter "{", or the last two characters of the current string are " {"). Tcl_DStringAppendElement returns a pointer to the characters of the new string. Tcl_DStringStartSublist and Tcl_DStringEndSublist can be used to create nested lists. To append a list element that is itself a sublist, first call Tcl_DStringStartSublist, then call Tcl_DStringAppendElement for each of the elements in the sublist, then call Tcl_DStringEnd- Sublist to end the sublist. Tcl_DStringStartSublist appends a space character if needed, followed by an open brace; Tcl_DStringEndSublist appends a close brace. Lists can be nested to any depth. Tcl_DStringLength is a macro that returns the current length of a dynamic string (not including the terminating null character). Tcl_DStringValue is a macro that returns a pointer to the current contents of a dynamic string. Tcl_DStringSetLength changes the length of a dynamic string. If newLength is less than the string's current length, then the string is truncated. If newLength is greater than the string's current length, then the string will become longer and new space will be allocated for the string if needed. However, Tcl_DStringSetLength will not initialize the new space except to provide a terminating null character; it is up to the caller to fill in the new space. Tcl_DStringSetLength does not free up the string's storage space even if the string is truncated to zero length, so Tcl_DStringFree will still need to be called. Tcl_DStringTrunc changes the length of a dynamic string. This procedure is now deprecated. Tcl_DStringSetLength should be used instead. Tcl_DStringFree should be called when you are finished using the string. It frees up any memory that was allocated for the string and reinitializes the string's value to an empty string. Tcl_DStringResult sets the result of interp to the value of the dynamic string given by dsPtr. It does this by moving a pointer from dsPtr to the interpreter's result. This saves the cost of allocating new memory and copying the string. Tcl_DStringResult also reinitializes the dynamic string to an empty string. Tcl_DStringGetResult does the opposite of Tcl_DStringResult. It sets the value of dsPtr to the result of interp and it clears interp's result. If possible it does this by moving a pointer rather than by copying the string. KEYWORDS
append, dynamic string, free, result Tcl 7.4 Tcl_DString(3)
All times are GMT -4. The time now is 12:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy