Re: reading a file



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



Relevant Pages

  • Re: *scanf in Harbison and Steele
    ... least that fgets takes a file pointer as an arg. ... fgets never discards a '\n'. ... string, you know that the input exceeded the string length, and ... which accesses any file, while ggets accesses stdin. ...
    (comp.lang.c)
  • Re: Is C99 the final C? (some suggestions)
    ... fgets was designed to read text lines into a string. ... that fgetsdoes not ignore embedded null characters in text files. ... and puts it in the buffer nominated. ...
    (comp.lang.c)
  • Re: Reading Simple Flatfiles with a COBOL Mindset
    ... >all 5 data fields in one file read using fgets. ... the extracting data from the string that's been read in quite similar. ... at a time with fgets and then extract it. ... The internal representation in a struct can be easily made close to the ...
    (comp.lang.c)
  • Re: fgets question
    ... documentation didn't say if fgets put \0 after a string literal. ... fgets() has nothing at all to do with string literals. ... within the bounds of the array, then it is not a string. ... strlenon an array of characters that is not a string, ...
    (comp.lang.c)
  • Re: Malcolms new book - Chapter 1 review
    ... My own take is that "safe" routines such as this ggets should not be ... In practise fgets() is too hard for the average programmer to use correctly, as has time after time been demonstrated here. ... passed a maliciously long line it will clog up the machine's memory, but it will not cause the program to calcualte a wrong answer. ... it would be completely unacceptable on a web server for receiving incoming requests. ...
    (comp.lang.c)