ro*******@gmail.com wrote:
hi all..
i've got a file of the following format
10000000 records in
10000000 records out
5120000000 bytes (5.1 GB) copied, 628.835 seconds, 8.1 MB/s
how am i suppose to get the parameter 8.1MB/s on the third line?
thanks in advance and best regards.
thanks.
I assume you're talking about parsing that text.
Although it will make some regulars groan, you could use strtok() to
break the line into substrings. For example:
#include <string.h>
int main(void)
{
char statsLine[] = "5120000000 (5.1 GB) copied, 628.835 seconds,
8.1 MB/s";
char *bytesCopied = strtok(statsLine, ",");
char *totalSeconds = strtok(NULL, ",");
char *copyRate = strtok(NULL, ",");
return 0;
}
After running, bytesCopied should point to the substring "5120000000
(5.1 GB) copied", totalSeconds should point to the substring "628.835
seconds", and copyRate should point to the substring "8.1 MB/s".
Many words of warning: strtok() modifies its input string (replacing
delimiters with nul characters), so you cannot pass it a string literal
or otherwise unwritable argument. If you intend to use that original
string elsewhere, you must preserve it somehow. Also, bytesCopied,
totalSeconds, and copyRate are all pointing to substrings in the
statsLine array; they are not distinct string instances themselves. If
you need that, you'll have to create separate string buffers and copy
the results of strtok() into them, e.g.:
char bytesSecondsString[MAX_SIZE];
...
strcpy(bytesSecondsString, copyRate);
You can't nest calls to strtok(); that is, you can't do something like
this:
char *bytesCopied = strtok(statsLine, ","); /* get first substring
in statsLine */
char *bytes = strtok(totalBytes, " "); /* get first substring in
totalBytes */
char *gBytes = strtok(NULL, " "); /* get next substring in
totalBytes */
char *totalSeconds = strtok( NULL, ","); /* get the next substring
in statsLine */
...