473,657 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3287
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(statsLin e, ",");
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 bytesSecondsStr ing[MAX_SIZE];
...
strcpy(bytesSec ondsString, copyRate);
You can't nest calls to strtok(); that is, you can't do something like
this:

char *bytesCopied = strtok(statsLin e, ","); /* get first substring
in statsLine */
char *bytes = strtok(totalByt es, " "); /* 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(statsLin e, ",");
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 bytesSecondsStr ing[MAX_SIZE];
...
strcpy(bytesSec ondsString, copyRate);
You can't nest calls to strtok(); that is, you can't do something like
this:

char *bytesCopied = strtok(statsLin e, ","); /* get first substring
in statsLine */
char *bytes = strtok(totalByt es, " "); /* 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*******@fals e.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.ne t
Oct 16 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3056
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
7042
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 file... Thank you to all of you who can help me with this one...
19
10306
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 text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
4
9823
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 = tmpfile(); /* write into tmpFile */ ...
6
6340
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 browsing through this group and other sources on the web, it seems that there are many ways to do it. Some suggest that simply fseek'ing to 8K bytes before the end of file, and going backwards is the way. In this case, am I guaranteed best results...
1
2011
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: mdyn"comment~""comment~"\n Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried saving a record for 6/26/2004 and 7/26/2004 and it read it in as the day, year, and number of...
7
6055
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
5
14981
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 and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
6
3524
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
2
2834
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 wireless where I'm working could just be ungodly slow-- which it is.) Is reading a file much more resource/processor intensive than, say, including a .php file? What about the act of creating a simpleXML object? What about the act of checking the...
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.