Reuse format strings


 
Thread Tools Search this Thread
Top Forums Programming Reuse format strings
# 1  
Old 10-21-2014
Reuse format strings

I have a collection of format strings for sscanf, such as

Code:
"%02d%*1s%02d%*1s%02d"

to read in certain formatted strings, such as dates, times, etc.

I wonder if there is a way to use them in printf without some changes? The example above would not work - at least I can't think of any ways to use it in printf. Is there a way to reuse them?
# 2  
Old 10-21-2014
Can you provide a better example on how you plan to reuse them in a specific printf statement...
# 3  
Old 10-21-2014
As a general rule, no. The functions sscanf and scanf allow format strings that are called scansets. There are like the one you posted.

IF you wanted reuse for a subset of them(that are usable both ways) you can create a .h include file, example:
Code:
/* fmtstr.h */
#ifndef FMTSTR_H_INCLUDED
#define FMTSTR_H_INCLUDED 

char fmt1[]="%-4s";
char fmt2[]="%-5s";

#endif
/* end fmststr.h */

In your code include the fmtstr.h file like this:

Code:
#include "fmtstr.h"

When you compile be sure to add the directory that fmtstr.h lives in with a -I option.
This way you can compile almost anywhere you need to on the system as long as the directory & file exists and you can read it.

Code:
gcc myfile.c -I /home/migurus/myincludedir -o myfile

# 4  
Old 10-21-2014
Quote:
Originally Posted by shamrock
Can you provide a better example on how you plan to reuse them in a specific printf statement...
There is an array of format strings, the input / output routines make decision which one should be used and passes its decision as an index into that array, this index would be used later to actually use the string in the sscanf/printf

---------- Post updated at 02:20 PM ---------- Previous update was at 02:17 PM ----------

Quote:
Originally Posted by jim mcnamara
IF you wanted reuse for a subset of them(that are usable both ways) you can create a .h include file
Thanks, that idea looks interesting.
# 5  
Old 10-21-2014
You generally want to use the printf() format string rather than the scanf format string if you want to use the same format string for input and output. Try the following C code as an example:
Code:
#include <stdio.h>
char	*dtfmt = "%02d/%02d/%02d %02d:%02d:%02d\n";

int main() {
	int	D, M, Y, h, m, s;
	int	ret;
	char	in[80];

	printf("Enter date & time (MM/DD/YY hh:mm:ss): ");
	while(fgets(in, sizeof(in), stdin) != NULL) {
		ret = sscanf(in, dtfmt, &M, &D, &Y, &h, &m, &s);
		printf("Found Month %d, Day %d, Year %d\n", M, D, Y);
		printf("Found Hour %d, Minute %d, Second %d\n", h, m, s);
		printf(dtfmt, M, D, Y, h, m, s);
		printf("Enter another date & time (CTL-d): ");
	}
	printf("\n");
	return ret;
}

and see what you get if you give it the input:
Code:
10/21/14 13:07:33
1/2/14        1:2:5

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Change the format of the strings

I have Input file as below 1 a 1 b 1 c 2 d 2 e 2 f I want below output as below. 1 a,b,c 2 c,d,e Pls suggest how can i do it. (1 Reply)
Discussion started by: millan
1 Replies

3. Emergency UNIX and Linux Support

Reuse a LUN

I have a LUN (From HP-Storage VA7110) that is claimed on 2 servers, but is in used in one of the VG on Server-1 . Now I want to shut Server-1 and re-use that LUN on server-2 . Server-1 Path-1 : /dev/rdsk/c4t0d1 Path-2: /dev/rdsk/c6t0d1 Server-2 Path-1: /dev/rdsk/c5t0d1 Path-2:... (8 Replies)
Discussion started by: Shirishlnx
8 Replies

4. Shell Programming and Scripting

extract strings from file and display in csv format

Hello All, I have a file whose data looks something like this I want to extract just the id, name and city fields in a csv format and sort them by id. Output should look like this. 1,psi,zzz 2,beta,pqr 3,theta,xyz 4,alpha,abc 5,gamma,jkl (12 Replies)
Discussion started by: grajp002
12 Replies

5. Shell Programming and Scripting

Reuse Variable..

Hi. I have these two variables: My objective here is to reuse that $file_name variable again and again by resetting the $cv value. for example, if i reissue the cv="$(print 'CV01')" command, thus $file_name is now should be "CP99978_CV01.TXT", not "CP99978_CV01.TXT" anymore. How I'm... (7 Replies)
Discussion started by: aimy
7 Replies

6. Shell Programming and Scripting

SCCS is messing up my date format strings

Does anybody know how to keep SCCS from changing the wrong module keywords? I'm thinking of a don't translate after this line kind of operation. I see where you can use get with a -k but then no keywords get translated. It's either all or none. I use simply # %A% # %G% %T% in all my... (1 Reply)
Discussion started by: Back-N-Black
1 Replies

7. Linux

suggest some ideas for reuse

hi can you all help me to develop anything in unix that could be reused. any module or application could be helpful (0 Replies)
Discussion started by: infyanurag
0 Replies

8. HP-UX

Reuse disk from other HP-UX

Hello, I have 2 hp-ux both running 11.23, I have move one of a harddisk from "UNIX A" to "UNIX B", so how can I read back the data in "UNIX B"? Thanks (5 Replies)
Discussion started by: zetadhell
5 Replies

9. UNIX for Advanced & Expert Users

Password reuse utility

Does anyone know of a password reuse utility for Solaris 7 or 8? Security people are telling me that I need one. Thanks (1 Reply)
Discussion started by: rtoba
1 Replies

10. UNIX for Dummies Questions & Answers

How to reuse same major number

Hi, I am working on device drivers.Once If register a device i'll get one major no. If i unregister and register again i'll get a different major no.What i have to do to get same major no. each time :( (0 Replies)
Discussion started by: Agnello
0 Replies
Login or Register to Ask a Question