Re: reading a file




robannexs@xxxxxxxxx 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 */
...

.



Relevant Pages

  • Re: reading a file
    ... Although it will make some regulars groan, you could use strtok() to ... break the line into substrings. ... strtokmodifies its input string (replacing ... statsLine array; they are not distinct string instances themselves. ...
    (comp.lang.c)
  • Re: My own fuction: Access Violation error...
    ... I saw the warning about strtok having security issues, but I cannot see where this could ... You may consider this code for splitting a string into substrings (and I ...
    (microsoft.public.vc.mfc)
  • Re: Less is not More
    ... not produce an increase in the string's KC toward its maximum upper ... You are implying that larger substrings have constant or ... digits of computable constant X" with n as an argument, ... 24 ways I can concatenate them once each into a single string, ...
    (talk.origins)
  • Re: (?{..}) and lexical scoping issues.
    ... > @_ is most definitely a package variable. ... > (You are counting how many substrings of each string are also substrings ... > all substrings of a string. ... it will be hard to beat with a Perl program. ...
    (comp.lang.perl.misc)
  • Re: Does strtok require a non-null token?
    ... In other words, if the string has 2 colons in a row, it ... Just one more reason to avoid strtok(). ... That is, for the majority of the reasons I need to tokenized a string, this default behaviour is exactly what I want. ... For those requirements that strtokdoes not fit we have our own internal tokenizing routines. ...
    (comp.lang.c)