G
gtippery
Newbie-ish questions - I've been away from C for a _long_ time.
It seems to me that there ought to be easier (or at least shorter) ways
to do what this does. It does compile & run for me (with PowerC, a
16-bit DOS compiler); if there are nonstandard or "accidentally-works"
aspects, please let me know.
{This is the sort of situation where if I knew *what* to Google for, I
wouldn't *need* to... <grin>}
Code follows:
---------------------- snip ------------------------
#include <stdio.h>
#include <stdlib.h> /* for itoa */
#include <string.h> /* for memcpy(), strcpy(), strcat() */
#define NAMLEN 8
#define EXTLEN 3
#define MAX 512
#define MAXCOL 1
typedef struct {
char name[NAMLEN];
char ext[EXTLEN];
} NameExt;
NameExt NameArray[MAX+1];
int NameCnt=0;
main()
{
//...
char fmtstr[32], digits[12];
int i;
char NAM[NAMLEN+1], EXT[EXTLEN+1];
int colnum = 0;
//...
/* Fill the array with (test) names & extensions,
left-justified & space-filled (no (char)0's).
In the real app (...'d code), there's findfirst(),
findnext(), sorting, etc.*/
memcpy(&NameArray[0], "One 1 ", 11);
memcpy(&NameArray[1], "TwoTwoTw222", 11);
memcpy(&NameArray[2], "333 003", 11);
NameCnt = 3;
//...
/* Print names & extensions in multiple columns */
// Construct format string like "%8s%c%3s"
strcpy(fmtstr, "%");
strcat(fmtstr, itoa(NAMLEN, digits, 10));
strcat(fmtstr, "s%c%");
strcat(fmtstr, itoa(EXTLEN, digits, 10));
strcat(fmtstr, "s");
// Print according to the specified format
for(i=0; i<NameCnt; i++) {
memcpy(NAM, NameArray.name, NAMLEN);
NAM[NAMLEN] = (char)0;
memcpy(EXT, NameArray.ext, EXTLEN);
EXT[EXTLEN] = (char)0;
printf(fmtstr, NAM, '.', EXT);
// Handle columnizing here
if(colnum++ < MAXCOL) {
printf(" ");
} else {
printf("\n");
colnum = 0;
}
}
printf("\n");
//...
}
---------------------- snip ------------------------
Particularly, are there easier ways to:
(1) initialize the NameArray records;
(2) specify the format string, assuming I want to stick with constants
for the field widths; and
(3) printf the NameArray array-of-char fields without explicitly
converting them to strings?
Also, is there a way to get e.g.,
sizeof NameExt.Ext
(which doesn't work) at compile-time?
TIA!
It seems to me that there ought to be easier (or at least shorter) ways
to do what this does. It does compile & run for me (with PowerC, a
16-bit DOS compiler); if there are nonstandard or "accidentally-works"
aspects, please let me know.
{This is the sort of situation where if I knew *what* to Google for, I
wouldn't *need* to... <grin>}
Code follows:
---------------------- snip ------------------------
#include <stdio.h>
#include <stdlib.h> /* for itoa */
#include <string.h> /* for memcpy(), strcpy(), strcat() */
#define NAMLEN 8
#define EXTLEN 3
#define MAX 512
#define MAXCOL 1
typedef struct {
char name[NAMLEN];
char ext[EXTLEN];
} NameExt;
NameExt NameArray[MAX+1];
int NameCnt=0;
main()
{
//...
char fmtstr[32], digits[12];
int i;
char NAM[NAMLEN+1], EXT[EXTLEN+1];
int colnum = 0;
//...
/* Fill the array with (test) names & extensions,
left-justified & space-filled (no (char)0's).
In the real app (...'d code), there's findfirst(),
findnext(), sorting, etc.*/
memcpy(&NameArray[0], "One 1 ", 11);
memcpy(&NameArray[1], "TwoTwoTw222", 11);
memcpy(&NameArray[2], "333 003", 11);
NameCnt = 3;
//...
/* Print names & extensions in multiple columns */
// Construct format string like "%8s%c%3s"
strcpy(fmtstr, "%");
strcat(fmtstr, itoa(NAMLEN, digits, 10));
strcat(fmtstr, "s%c%");
strcat(fmtstr, itoa(EXTLEN, digits, 10));
strcat(fmtstr, "s");
// Print according to the specified format
for(i=0; i<NameCnt; i++) {
memcpy(NAM, NameArray.name, NAMLEN);
NAM[NAMLEN] = (char)0;
memcpy(EXT, NameArray.ext, EXTLEN);
EXT[EXTLEN] = (char)0;
printf(fmtstr, NAM, '.', EXT);
// Handle columnizing here
if(colnum++ < MAXCOL) {
printf(" ");
} else {
printf("\n");
colnum = 0;
}
}
printf("\n");
//...
}
---------------------- snip ------------------------
Particularly, are there easier ways to:
(1) initialize the NameArray records;
(2) specify the format string, assuming I want to stick with constants
for the field widths; and
(3) printf the NameArray array-of-char fields without explicitly
converting them to strings?
Also, is there a way to get e.g.,
sizeof NameExt.Ext
(which doesn't work) at compile-time?
TIA!