Originally Posted by
itCbitC
Hmm... so strtok() hasn't been covered in class either.
Walk along s3[] and replace all embedded spaces with tabs.
(assumes s3[] data fields are separated by a single space as there will be formatting issues if they aren't and would require more code to align)
Now fprintf() the three arrays s1[] s2[] and s3[] separated by tabs.
No strtok() hasn't been covered but that's a function i understand now because i looked it up in C reference on the net an it isn't that difficult
So i understand you split the 3rd string into tokens where there's a space character.
There are some other things i don't quite get tho.
I specified everything i don't get with /* ????? */ in my comments so maybe u could have a look at my code and explane?
Here's my code:
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) /* int main ?????????????? */
{
FILE *fpin, *fpout; /* declare input and output file pointer */
char flin[256], flout[256]; /* declare input and output file name string */
char *s, line[256], x[256]; /* declare pointer to ?????, and ????? */
char tijd[50], id[50], bytes[50]; /* declare time, id and bytes string */
printf("This program converts standard Vector CANoe log files to Ecxel readable files\n\nPlease specify source filename:"); /* print programheader with programfunction and ask for input filename */
scanf("%s",flin); /* read input filename */
/* does inputfile exist ? */
if ((fpin = fopen(flin, "r"))== NULL) /* if can not open inputfile for reading */
{
printf("Cannot open source file!\n"); /* show cannot open input */
return 1;
}
/* if inputfile does exist */
else
{
printf("Please specify destination filename:"); /* ask to specify destination filename */
scanf("%s",flout); /* read output filename */
fpout = fopen(flout, "w"); /* open outputfile for writing */
fprintf(fpout,"time\tid\tdlc\tbyte0\tbyte1\tbyte2\tbyte3\tbyte4\tbyte5\tbyte6\tbyte7\n"); /* print data header in outputfile */
}
while(fgets(line, 256, fpin)) /* while string from inputfile is ??????? */
{
if (sscanf(line, "%s%*s%s Tx %*s %[^\n]", tijd, id, bytes) == 3) /* ??????????? */
{
sprintf(x, "%s\t%s", tijd, id); /* print string time and id to file */
for ( s= strtok(bytes, " "); s; s = strtok(NULL, " ")) /* split bytes string into byteX when there's a space character */
sprintf(x, "%s\t%s", x, s); /* print ???? string to file */
fprintf(fpout, "%s\n", x); /* ??????????????? */
}
}
fclose(fpin); /* close inputfile */
fclose(fpout); /* close outputfile */
return 0;
}
I also would like to know how i can separate the first character i'm now putting in the bytes string to a new variable called dlc?
The output now is ok but the first characters that are put in the outputfile from the bytes string are actually the dlc data.
When i can ad this dlc variable in the code and put the first characters that are now in the bytes string in this dlc variable i have everything seperated like it should be.
then i can sprintf(x, "%s\t%s\t%s", tijd, id, dlc); and handle the bytes string in the same way i'm doing now.
Can you help me out with this?