removing the null terminator at the end of string
strncat(char str1, char str2, strlen(str2)-1) will give string str1 with no terminator at the end of string
I am born with potential, I am born with goodness, I am born with ideas and dreams, I am born with greatness, I have wings, I have two wings, I am meant for creativity because I have wings, I will fly, I will fly, I will fly !!! -- DR. A. P. J. Abdul Kalam
#include <unistd.h>
size_t write(int fildes, const void *buf, size_t nbytes);
Field | Description |
---|---|
int fildes | The file descriptor of where to write the output. You can either use a file descriptor obtained from the open system call, or you can use 0, 1, or 2, to refer to standard input, standard output, or standard error, respectively. |
const void *buf | A null terminated character string of the content to write. |
size_t nbytes | The number of bytes to write. If smaller than the provided buffer, the output is truncated. |
return value | Returns the number of bytes that were written. If value is negative, then the system call returned an error. |
#include <unistd.h> int main(void) { if (write(1, "This will be output to standard out\n", 36) != 36) { write(2, "There was an error writing to standard out\n", 44); return -1; } return 0; }
#include <unistd.h> #include <fcntl.h> int main(void) { int filedesc = open("testfile.txt", O_WRONLY | O_APPEND); if (filedesc < 0) { return -1; } if (write(filedesc, "This will be output to testfile.txt\n", 36) != 36) { write(2, "There was an error writing to testfile.txt\n", 43); return -1; } return 0; }
#includeThe available access modes areint open( char *filename, int access, int permission );
O_RDONLY O_WRONLY O_RDWR O_APPEND O_BINARY O_TEXTThe permissions are
S_IWRITE S_IREAD S_IWRITE | S_IREADThe open() function returns an integer value, which is used to refer to the file. If un- successful, it returns -1, and sets the global variable errno to indicate the error type.
#includeThe read() function attempts to read nbytes from the file associated with handle, and places the characters read into buffer. If the file is opened using O_TEXT, it removes carriage returns and detects the end of the file.int read( int handle, void *buffer, int nbyte );
#includeThe write() function attempts to write nbytes from buffer to the file associated with handle. On text files, it expands each LF to a CR/LF.int write( int handle, void *buffer, int nbyte );
#includeThe close() function closes the file associated with handle. The function returns 0 if successful, -1 to indicate an error, with errno set appropriately.int close( int handle );
in C, Assigning output of system() to a variable
[Log in to get rid of this advertisement]
I'm new to C and Linux and I am
confused on how things work. I tried to search but I don't really know
if I'm searching for the right thing because lots of other things come
up.
Currently, I figured out that I can use shell commands inside C by using the method system(). They display out on the screen fine, but my problem is that I need to assign them to a variable, and not have them display on the screen (yet). I'm planning to put the output of "ls -l" to a variable. I'm thinking of an array of strings (I think there are no strings, but an array char, so I guess this is an array of array of chars?) which will contain every line of the "ls -l" command except for the first line. (I think I need it in an array because I'm supposed to scroll through it in a menu-like style where I'm following example 18 on this link: http://www.tldp.org/HOWTO/NCURSES-Pr...tml#MENUBASICS but I think it's another question for another time, hopefully I can figure it out and not have to though, hahah) What I have is this, but it doesn't work.
Code:
#include
Code:
system.c: In function 'main': system.c:5: error: invalid initializer Last edited by Miaire; 01-28-2005 at 08:29 AM. |
|
01-28-2005, 08:27 AM | #2 |
Hivemind
Member
Registered: Sep 2004
Posts: 273
Rep:
|
Two things. If a function returns a
pointer-to-char (dynamically allocated or static) and you want to assign
the return value to a variable you do:
Code:
char *ptr = some_func_returning_pointer_to_char();
Code:
$ man 3 system The system() call is part of standard C (and C++ of course) but how it operates and what you can do with the processes it spawns is platform-dependent. |
|
01-28-2005, 09:29 AM | #3 |
deiussum
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
man popen
I think this does what you are looking for. |
|
01-28-2005, 02:39 PM | #4 |
jim mcnamara
Member
Registered: May 2002
Posts: 964
Rep:
|
popen example:
Code:
#include |
|
01-30-2005, 01:40 PM | #5 |
Miaire
LQ Newbie
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Fedora Core 3
Posts: 11
Original Poster
Rep:
|
Thanks for the answers, I have gotten
what I needed. Figured out char arrays (strings) and casting char to
int in the process.
This is what I came up with:
Code:
#include |
int system ( const char * command );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|