473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

reading a file

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
7 3251
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
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.