printf

How can I create columnar output in Perl?

Hello! #!/usr/bin/env perl use warnings; use strict; my $text = 'hello ' x 30; printf "%-20s : %s\n", 'very important text', $text; the output of this script looks more ore less like this: very important text : hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hel...

How to use sprintf instead of hardcoded values

I am developing a firewall for Linux as my project. I am able to capture packets and to block them. I am using IPTABLES. How can I use variables with sprintf instead of hardcoded values? sprintf(comm, "iptables -A INPUT -s $str -j DROP") // inplace of: sprintf(comm, "iptables -A INPUT -s 192.168.0.43 -j DROP") ...

Extra leading zeros when printing float using printf?

I'd like to be able to write a time string that looks like this: 1:04:02.1 hours using printf. When I try to write something like this: printf("%d:%02d:%02.1f hours\n", 1, 4, 2.123456); I get: 1:04:2.1 hours Is it possible to add leading zeros to a float formatting? ...

User defined conversion operator as argument for printf

I have a class that defined a user defined operator for a TCHAR*, like so CMyClass::operator const TCHAR*() const { // returns text as const TCHAR* } I want to be able to do something like CMyClass myClass; _tprintf(_T("%s"), myClass); or even _tprintf(_T("%s"), CMyClass(value)); But when trying, printf always prints (null) ...

C++ printf std::vector

How I can do something like this in C++: void my_print(format_string) { vector<string> data; //Fills vector printf(format_string, data); } my_print("%1$s - %2$s - %3$s"); my_print("%3$s - %2$s); I have not explained well before. The format string is entered by the application user. In C# this works: void my_print(format_s...

Does Cocoa’s printf-style formatting not work as expected when using %@ conversion specifier and field width?

My understanding of printf-like format strings is that you can prefix any conversion specifier with a minimum field width. This does not seem to work for Cocoa’s %@ specifier. Example: NSLog(@"'%5@'", @"foo"); NSLog(@"'%5s'", [@"foo" UTF8String]); Output: … 'foo' … ' foo' Is this the intended behavior? ...

Recovering graceflly from a failed vsnprintf on msvc2008

I'm looking for a way to use some variant of vsnprintf() with a buffer that can possibly be longer than the input buffer without triggering an error to the user. So far I've found that vsnprintf() and its variants silently truncate the string when the buffer is too small but they don't return the actual length of the string so I can't t...

C++ printf: newline (\n) from commandline argument

How print format string passed as argument ? example.cpp: #include <iostream> int main(int ac, char* av[]) { printf(av[1],"anything"); return 0; } try: example.exe "print this\non newline" output is: print this\non newline instead I want: print this on newline ...

c language: printf help

Hi again, here is my coding which gives me the error 'warning: unknown conversion type character 0x20 in format' int subtotal; long long a,b,c,d,e,f,g,h,i,j,k,l,m; subtotal = (1*(a+c+e+g+i+k))+(3*(b+d+f+h+j+l)); printf(" = %d % 10 = %d; (10 - %d) % 10 = %lld\n", subtotal,subtotal%10,subtotal%10,m); any idea why this is wrong? ...

[C] this code, its work fine and return what i want, but its hangs before print it ??

I make this program :: #include<stdio.h> char *raw_input(char *msg); main() { char *s; *s = *raw_input("Message Here Is: "); printf("Return Done.."); printf(s); } char *raw_input(char *msg){ char *d; printf("%s", msg); scanf("%s",&d); return d; } What this do is, it print my message and scan for input from the user, then pr...

printf anomaly after "fork()"

OS: Linux, Language: pure C I'm moving forward in learning C progpramming in general, and C programming under UNIX in a special case :D So, I detected a strange (as for me) behaviour of the printf() function after using a fork() call. Let's take a look at simple test program: #include <stdio.h> #include <system.h> int main() { int...

Java printf using variable field size?

I'm just trying to convert some C code over to Java and I'm having a little trouble with String.printf. In C, to get a specific width based on a variable, I can use: printf("Current index = %*d\n", sz, index); and it will format the integer to be a specific size based on sz. Trying: System.out.println(String.format("Current index =...

How can I temporarily redirect printf output to a c-string?

I'm writing an assignment which involves adding some functionality to PostgreSQL on a Solaris box. As part of the assignment, we need to print some information on the client side (i.e.: using elog.) PostgreSQL already has lots of helper methods which print out the required information, however, the helper methods are packed with hundred...

Without width printing format in C

Hi, in C if I have a printf statement containing say "%.2f", it says that the precision is 2 digits after the decimal place. I haven't explicitly specify the width. I have two questions: Is this good programming practice?; Is without specifying the width means that the width of the field will get adjusted automatically when printing th...

Quick C question - how to hide leading zero in printf

Hey, if I had this code: printf( "%8.2f" , .23 ); It outputs 0.23. How do I get it to simply output .23? Thanks. ...

How to use a variable in an awk expression

I'm trying to make this command: sed bla bla filename | awk '{printf "%s %s_entry_%.3f %.3f %.3f %.3f",$1,$3,$4,$5,$6,$7}' But the thing is, i want the %.3f part to be variable. So in one case it could be %.3f and in another it could be %.3f %.3f %.3f. So i'll just use a static one in my example code for clarity. So if i want 4 of the...

C stdout printf problem

I have a weird issue with printing data out. I use printf to print a char* string and then after that print another one. However part of the first string doesn't get printed and when I print the second string the missing part of the first one is pretended to that one. What is happening here? I'm writting a simple libpcap implimentatio...

snprintf vs strcpy(etc) in c

For doing string concatenation I've been doing basic strcpy,strncpy of char* buffers. Then I learned about the snprintf and friends. Should I stick with my strcpy,strcpy + \0 terminiation. Or should I just use snprintf in the future? thanks ...

How to ANSI-C cast from unsigned int * to char *?

I want these two print functions to do the same thing: unsigned int Arraye[] = {0xffff,0xefef,65,66,67,68,69,0}; char Arrage[] = {0xffff,0xefef,65,66,67,68,69,0}; printf("%s", (char*)(2+ Arraye)); printf("%s", (char*)(2+ Arrage)); where Array is an unsigned int. Normally, I would change the type but, the proble...

Python3k ctypes printf

printf returns 1 instead of Hello World! which is the desired result. I googled it and think its because of the changes in the way sequences are treated. How do i modify the code to print "Hello World!"? www.mail-archive.com/[email protected]/msg15119.html import ctypes msvcrt=ctypes.cdll.msvcrt string=b"Hello World!" msvcrt.prin...