Reputation: 590
Say I want to print using
printf(format, "YYYYMMDD");
How would format look if I wanted my output to look like "MM-DD-YYYY"?
Thanks.
Let me add my full code so there's no confusion.
I have a sruct defined as
typedef struct{
char *dt;
float op, hi, lo, cl, vl;
}STOCKDATA;
Then call a function:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "../include/struct.h"
#include "../include/function.h"
#include "../include/utility.h"
#define linemax 514
void stockData(char *ticker)
{
extern STOCKDATA *stockdata;
extern int nrows;
//count number of lines in file..............................
char *filename;
mkstring("../data/", ticker, filename);
nrows=linesInFile(filename);
//allocate stockdata.........................................
stockdata=malloc_stockdata(0,nrows-1);
//open file to read into stockdata...........................
FILE *fp;
fp = fopen(filename, "r");
if(fp==NULL)
{
printf("%s%s\n", "Can't open data file ", filename);
exit(1);
}
char delimiters[] = " ,";
char line[linemax];
char *tmp;
fgets(line, linemax, fp); //skip header
int count=0; //count number of lines
while( fgets(line, linemax, fp) !=NULL)
{
tmp=strtok(line, delimiters); //ticker (skipped)
stockdata[count].dt= strtok(NULL, delimiters); //date
//printf("%s\n", stockdata[count].dt);
stockdata[count].op=atof(strtok(NULL, delimiters)); //open
stockdata[count].hi=atof(strtok(NULL, delimiters)); //high
stockdata[count].lo=atof(strtok(NULL, delimiters)); //low
stockdata[count].cl=atof(strtok(NULL, delimiters)); //close
stockdata[count].vl=atof(strtok(NULL, delimiters)); //volume
count++;
}
fclose(fp);
}
Then call the function:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../include/struct.h"
#include "../include/function.h"
#include "../include/utility.h"
#define cutoff 10
void writeData(char *ticker)
{
extern STOCKDATA *stockdata;
extern int nrows;
//open output file........................................
char *filename;
mkstring("../output/", ticker, filename);
FILE *fp;
fp = fopen(filename, "w");
if(fp==NULL)
{
printf("%s%s\n", "Can't open data file ", filename);
exit(1);
}
int k;
char *format="%-15s%-10.2f%-10.2f%-10.2f%-10.2f%-10.0f\n";
for (k=nrows-cutoff; k<nrows; k++)
{
fprintf(fp, format, stockdata[k].dt,
stockdata[k].op,
stockdata[k].hi,
stockdata[k].lo,
stockdata[k].cl,
stockdata[k].vl );
}
fclose(fp);
}
Now, where I define 'format' in the last function call, I'm trying to make it print stockdata[k].dt in a MM-DD-YYYY format. The string in the data file is in YYYYMMDD.
Upvotes: 1
Views: 5508
Reputation: 215193
What is the format of your source data?
If it's really a string "YYYYMMDD"
, just use %.4s
and %.2s
with the appropriate offsets added to the pointer to get at the parts you want.
If it's a number of the form YYYYMMDD
(decimal), use x/10000
, x/10%100
, and x%100
to get at the parts, and %.4d
and %.2d
for the formats.
If it's an actual time, use strftime
.
Upvotes: 0
Reputation: 7105
You want to look into strptime
and strftime
. First, you need to parse the date you have (using strptime
) like so:
struct tm timeStruct;
strptime(dateString, "%Y%m%d", &timeStruct);
Then convert it to the new format using strftime
:
strftime(outputString, sizeof(outputString), "%m-%d-%Y", &timeStruct);
Now outputString
contains the date in the format you want.
Upvotes: 5