By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

reading a file

P: n/a
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.

Sep 28 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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?
If it always looks just like that, it should be easy to use
scanf() or sscanf() to read it. Something like:

double rate;
scanf(fp, "%*s bytes (%*f GB) copied, %*f seconds, %lf MB/s",
&rate);

If the units at the end might change, store them in a string and
multiply the rate accordingly.

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Sep 28 '06 #2

P: n/a
ro*******@gmail.com wrote:
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?
divide the file size by the time taken to copy it?

--
Nick Keighley

Sep 28 '06 #3

P: n/a
On Thu, 2006-28-09 at 01:12 -0700, 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?
<OT>
Do you need to use C for this? A quick awk script would work
perfectly:

awk -F"," {'print $3;'}

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Sep 28 '06 #4

P: n/a
ro*******@gmail.com wrote:
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?
If it has to be a C solution, then
- read lines of the file until one of them meets
NULL != strstr(line, "MB/s");
- then either scan for the given format using
if (1 == sscanf("%*[^,], %*[^,], %lf", &MegsPerSecond)) {
/* do something with MegsPerSecond */
}
- or find the last ',' using strrchr() and scan from beyond that
position for a number with strtod()
If the "MB/s" part is not fixed, you probably just want to search
for "/s" and extract the string after the last ','.
Reading arbitrary length lines with fgets() is rather hard.
Have a look at fggets() (and ggets()) from
http://cbfalconer.home.att.net/download/
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 28 '06 #5

P: n/a

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 */
...

Sep 28 '06 #6

P: n/a
John Bode wrote:
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 */
...
Or instead of using strtok(), loop through the string backwards and stop
at the second space character found. Untested partial code follows to
illustrate my point.

char *p = NULL;
char string = "5120000000 bytes (5.1 GB) copied, 628.835 seconds, 8.1 MB/s";
size_t i = strlen(string);
int found = 0;

for (; i 0; i--)
{
if (string[i] == ' ' && found == 0)
{
break;
}

found++;
}

p = &string[i];

printf("%s\n", p);
Sep 28 '06 #7

P: n/a
On Thu, 28 Sep 2006 13:39:00 GMT, Andrew Poelstra
<ap*******@false.sitewrote:
On Thu, 2006-28-09 at 01:12 -0700, ro*******@gmail.com wrote:
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?

<OT>
Do you need to use C for this? A quick awk script would work
perfectly:

awk -F"," {'print $3;'}
<OTAlmost. You need the whole script including braces inside the
single quotes or most (Unixoid) shells will treat it as a (uselessly
trivial) expansion list, and you probably need to select only line 3:
awk -F, 'NR==3{print $3}'

- David.Thompson1 at worldnet.att.net
Oct 16 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.