Reputation: 378
I want to generate an output like this "firstname surname: day.month.year " but what i get is some mixed up order. I am new to C and I don't know what's going on here.
So this is what i get
This is my code
char string[imax];
fgets (string, imax, team1); //wo, max count, aus welchem file
int i=1, k=0;
char delimiter[] = " ";
char *day, *month, *year, *firstname, *surname;
char *stats[5];
while(fgets(string,imax,team1) != 0)
{
/*stats[0] = strtok(string,delimiter);
while(i <=4)
{
stats[i] = strtok(NULL,delimiter);
i++;
}*/
day = strtok(string,delimiter);
month = strtok(NULL,delimiter);
year = strtok(NULL,delimiter);
firstname = strtok(NULL,delimiter);
surname = strtok(NULL,delimiter);
printf("%s ", firstname);
printf("%s:", surname);
printf("%s.", day);
printf("%s.", month);
printf("%s. ", year);
}
EDIT: I get the same order as the order in the file i am reading from
Upvotes: 1
Views: 295
Reputation: 9894
First, you need to add a newline after printing the year. Either change
printf("%s. ", year);
to
printf("%s.\n ", year);
or add
fputc( '\n', stdout );
The second thing is, that after fgets()
the newline of the source text file is part of string
and therefore part of surname
. You can handle this by adding '\n'
to delimiter
(and if it's a Windows text file opened on another system (e.g. UNIX), '\r'
too)
Upvotes: 3