An idea.
Code:
/* test.txt
Date: Feb 20, 2004
Requestor: Paul
Email: [email protected]
Priority: Medium
Needed: now
Status: ok
Description:
Test here.
.
Date: Feb 20, 2004
Requestor: Paul
Email: [email protected]
Priority: Medium
Needed: now
Status: ok
Description:
Test here.
And here.
And here.
.
*/
#include <stdio.h>
struct record
{
char date [ BUFSIZ ],
requestor [ BUFSIZ ],
email [ BUFSIZ ],
priority [ BUFSIZ ],
needed [ BUFSIZ ],
status [ BUFSIZ ],
description [ BUFSIZ ];
};
int foo(FILE *file, char *dst, size_t size)
{
size_t i;
for ( i = 0; i < size; ++i )
{
int ch = fgetc(file);
if ( ch == EOF )
{
return 0;
}
dst[i] = ch;
if ( i > 2 && dst[i - 3] == '.' && dst[i - 2] == '\n' &&
dst[i - 1] == '.' && dst[ i ] == '\n' )
{
break;
}
}
dst[i - 1] = '\0';
return 1;
}
int main(void)
{
const char filename[] = "test.txt";
FILE *file = fopen(filename, "r");
if ( file )
{
struct record record;
const char format[] = "Date: %[^\n]%*c"
"Requestor: %[^\n]%*c"
"Email: %[^\n]%*c"
"Priority: %[^\n]%*c"
"Needed: %[^\n]%*c"
"Status: %[^\n]%*c"
"Description: ";
while ( fscanf(file, format,
record.date,
record.requestor,
record.email,
record.priority,
record.needed,
record.status) == 6 )
{
printf("Date: %s\n", record.date);
printf("Requestor: %s\n", record.requestor);
printf("Email: %s\n", record.email);
printf("Priority: %s\n", record.priority);
printf("Needed: %s\n", record.needed);
printf("Status: %s\n", record.status);
/*
* fscanf did some rough stuff, but now we've got some work to do.
*/
if ( foo(file, record.description, sizeof record.description) )
{
printf("Description: \n%s\n", record.description);
}
}
fclose(file);
}
return 0;
}
/* my output
Date: Feb 20, 2004
Requestor: Paul
Email: [email protected]
Priority: Medium
Needed: now
Status: ok
Description:
Test here.
Date: Feb 20, 2004
Requestor: Paul
Email: [email protected]
Priority: Medium
Needed: now
Status: ok
Description:
Test here.
And here.
And here.
*/