Parsing Integer and Float Values of a Text File with sscanf


I want to parse such a file with these fields into integer and float variables,I tried to do this using fscanf,strtok,sscanf. But none of them works!

Some lines of the file :

fed18 5.7 12.7 144997 8087 267345 100776 fedora18 24.9 25.3 253566 10501 126282 118157 fed18 5.9 12.7 145005 8094 267345 100785 fedora18 23.3 25.3 253576 10507 126282 118169 fed18 6.2 12.7 145013 8100 267345 100789

Running the following code returns wrong values! I don't know what's the problem as I search, everybody use such this code and it works properly for them!

while(fgets(str,512,fp)!= NULL)//read file line by line { char *tokenstring = str; uint64_t netrx,nettx,vbd_rd,vbd_wr; double cpu, mem; char a[10],b[10],c[10],d[10],e[10],f[10],g[10]; sscanf(tokenstring, "%s ,%[^' '],%[^' '],%[^' '],%[^' '],%[^' '],%[^' ']",g, a, b, c, d, e, f); cpu = atof(a); mem = atof(b); nettx = atoi(c); netrx = atoi(d); vbd_rd = atoi(e); vbd_wr = atoi(f); printf("%s %f %f %ld %ld %ld %ld\n",g,cpu,mem,netrx,nettx,vbd_rd,vbd_wr); } fclose(fp);

Here is the output:

fed18 38.000000 1.000000 0 0 0 0 fedora18 38.000000 1.000000 0 0 0 0 fed18 38.000000 1.000000 0 0 0 0 fedora18 38.000000 1.000000 0 0 0 0 fed18 38.000000 1.000000 0 0 0 0

I edited the original text file with a bash script and using awk ,.... The original lines were in this format:

fed18 --b--- 3616 6.3 1052640 12.7 1052672 12.7 3 1 125864 6023 1 0 254349 93082 7412662 4730752 0 fedora18 --b--- 4711 2.4 2101216 25.3 2101248 25.3 3 1 249151 8636 1 0 126083 113505 3306934 5992656 0

I selected some columns using a bash script. maybe this caused the problem!

I commented the lines of using function atoi or atof but still output wrong values.


If you always expect a single space between arguments you can simply your format string and obviate the need for atoi, atof:

while(fgets(str,512,fp)!= NULL)//read file line by line { char *tokenstring = str; uint64_t netrx,nettx,vbd_rd,vbd_wr; char g[10]; double cpu, mem; long int c, d, e, f; sscanf(tokenstring, "%s %lf %lf %lu %lu %lu %lu", g, &cpu, &mem, &nettx, &netrx, &vbd_rd, &vbd_wr); printf("%s %f %f %ld %ld %ld %ld\n",g,cpu,mem,netrx,nettx,vbd_rd,vbd_wr); } fclose(fp);


Your format string contains commas that don't exist in the input. That said, you should use %lf to parse floating point numbers into double and %lu to parse into uint64_t.

Note that you might run into trouble when the current locale isn't English because that influences which character C expects as a decimal point. Use setlocale(LC_NUMERIC, "C"); to fix that.


scanf is designed to parse numbers so there is no need to use atoi, so just use sscanf with proper parameters

int result = sscanf(tokenstring, "%s %lf %lf %lld %lld %lld %lld",g, &cpu, &mem, &netrx, &netrx, &vbd_rd, &vbd_wr); assert( result == 7 ) ;


  • fseek on a position beyond EOF does not trigger EOF using feof, how come?
  • Get claims from a WebAPI Controller - JWT Token,
  • In C, can you have a 128-bit bitfield [duplicate]
  • Infinity loop in while statement
  • How to Unit Test a SoapExtension derived class?
  • How can I distinguish between integer and character when using scanf()
  • How to use make_transform_iterator() with counting_iterator and execution_policy in Thrust?
  • Reloading table causes flickering
  • How to add a filter to Active Admin dashboard?
  • Open hyperlink on click on an ggplot/plotly chart
  • Read a file in “chunks” using PHP
  • Getting IIS6 to play nice with WordPress Pretty Permalinks
  • Accessing local repository in offline mode
  • MarkLogic Node.js Sort on “last-modified”
  • c++ search a vector for element first seen position
  • getelement by class name for clicking
  • Using android opencv apps without downloading opencv sdk manager
  • Efficient User-Agent Regex to find Safari in Python
  • Django Haystack Rebuild Index
  • jQuery: How to AJAXify WordPress Search?
  • How to install node-mysql?
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • Selenium to click on a javascript button corresponding to a text
  • D3 nodes and links from JSON with nested arrays of children
  • Google Custom Search with transparent background
  • Insert into database using onclick function
  • Using $this when not in object context
  • What is Eclipse's Declaration View used for?
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • AES padding and writing the ciphertext to a disk file
  • Perl system calls when running as another user using sudo
  • Can I make an Android app that runs a web view in Chrome 39?
  • Convert array of 8 bytes to signed long in C++
  • How to format a variable of double type
  • Linker errors when using intrinsic function via function pointer
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file