sscanf

How do I accept an md5sum via command line in C?

typedef union { uint ui[4]; } md5hash; void main(void) { int opt; while ((opt = getopt(argc, argv, "c:t:s:h:")) != -1) { switch (opt) { case 'h': hash = optarg; break; default: /* '?' */ exit(EXIT_FAILURE); } } md5hash hash; sscanf(hash, "%x%x...

How to portably convert a string into an uncommon integer type?

Some background: If I wanted to use for, for instance, scanf() to convert a string into a standard integer type, like uint16_t, I’d use SCNu16 from <inttypes.h>, like this: #include <stdio.h> #include <inttypes.h> uint16_t x; char *xs = "17"; sscanf(xs, "%" SCNu16, &x); But a more uncommon integer type like pid_t does not have any suc...

A Beginner's scanf_s() Disability

int main(void) { char tmp, arr[100]; int i, k; printf("Enter a string: "); scanf_s("%s", arr); for ( k = 0, i = (strlen(arr) - 1); k < (int) (strlen(arr) / 2); --i, ++k) { tmp = arr[k]; arr[k] = arr[i]; arr[i] = tmp; } puts(arr); return 0; } I know that there is so...

What should I use instead of sscanf?

I have a problem that sscanf solves (extracting things from a string). I don't like sscanf though since it's not type-safe and is old and horrible. I want to be clever and use some more modern parts of the C++ standard library. What should I use instead? ...

sscanf and strings

Hi I'm parsing some CSV data in C for the purposes of a Ruby extension. In order to pull out the data from each row I'm using sscanf as follows: char* line = RSTRING_PTR(arg); double price; double volume_remaining; unsigned int type_id, range, order_id, volume_entered, minimum_volume, duration, station_id, region_id, solar_syst...

How to retrieve the telephone number from an AT CMGL response?

I have an application written in C that reads text messages from a modem using AT commands. A typical AT response from the modem looks like this: +CMGL: 1,"REC READ","+31612123738",,"08/12/22,11:37:52+04" The code is currently set up to only retrieve the id from this line, which is the first number, and it does so using the following ...

Converting hex string stored as chars to decimal in C

I'm given a string hex_txt containing a 4 digit hex number in the way outlined in the code, split up in two array entries. I need to convert it to decimal. The following is the way I'm doing it. unsigned char hex_txt[] = "\xAB\xCD"; unsigned char hex_num[5]; unsigned int dec_num; sprintf(hex_num, "%.2x%.2x", (int)hex_txt[0], (int)hex_t...

using sscanf(), read string to array of int?

i have this string: 12 4 the quick 99 -1 fox dog \ what i want in my program: myArray[] = {12, 4, 99, -1}; how i do a multiple number scanning? ...

How do I parse out the fields in a comma separated string using sscanf while supporting empty fields?

I have a comma separated string which might contain empty fields. For example: 1,2,,4 Using a basic sscanf(string,"%[^,],%[^,],%[^,],%[^,],%[^,]", &val1, &val2, &val3, &val4); I get all the values prior to the empty field, and unexpected results from the empty field onwards. When I remove the expression for the empty field from th...

Parse a char from an xml in C language?

Hi, I'm passing as input to my program: "<param value=s/>" I use this code: char character[1]; sscanf(data, "<param value=%c/>", &character); printf("%c", character); However the output seems to be "s/>" instead of only "s" char. what's wrong here? ...

Extracting timings from a multidimensional array and writing to a file in c

Hello, I'm having trouble extracting the timings from a .srt (subtitle) file and writing it to another file called output.srt. When i run the following i get some funky stuff written onto the output file. // where hr=hours,mn=minutes,sc=seconds,ms=mili seconds #include <stdio.h> #define LINES 50 #define CHARAC 80 int main(void){ FI...

Is sscanf thread-safe on iPhone OS 3.1.2 ?

I was just spending my whole day debugging a random bug when i finally realized the Problem was sscanf being called from multiple threads. I confirmed by running the following code which works as expected on Snow Leopard but produces very strange results on my iphone with os 3.1.2. It also works fine in the Simulator. On the iPhone the...

Escape the dot in PHP sscanf?

Hi, This doesn't work: list($value) = sscanf('foo.bar','%s.bar'); echo $value; //foo.bar While this does: list($value) = sscanf('foo bar','%s bar'); echo $value; //foo Any suggestions are really appreciated. Thanks. ...

how to parse URLs in c using sscanf() ?

Hi, this is my c code that reads a list of URLs from a file , and tries to separate the various parts of the URL.. This is just rough parsing , i'm not bothered about special cases.. I guess there is some fault with the sscanf() statement , when i run this , i get " segmentation FAULT" .. and moreover , the full url is being assigned t...

sscanf in Python

I'm looking for an equivalent to sscanf() in Python. I want to parse /proc/net/* files, in C I could do something like this: int matches = sscanf( buffer, "%*d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %*X %*X:%*X %*X:%*X %*X %*d %*d %ld %*512s\n", local_addr, &local_port, rem_addr, &rem_port, &inode); I thought at ...

Quick question regarding this issue, Why doesnt it print out the second value(converted second value) on the string?

Quick question, What have I done wrong here. The purpose of this code is to get the input into a string, the input being "12 34", with a space in between the "12" and "32" and to convert and print the two separate numbers from an integer variable known as number. Why doesn't the second call to the function copyTemp, not produce the value...

Convert function to read from string instead of file in C

I've been tasked with updating a function which currently reads in a configuration file from disk and populates a structure: static int LoadFromFile(FILE *Stream, ConfigStructure *cs) { int tempInt; ... if ( fscanf( Stream, "Version: %d\n",&tempInt) != 1 ) { printf("Unable to read version number\n"); return 0; } c...

sscanf wrapping function to advance string pointer in C

I have a function that makes a series of calls to sscanf() and then, after each, updates the string pointer to point to the first character not consumed by sscanf() like so: if(sscanf(str, "%d%n", &fooInt, &length) != 1) { // error handling } str+=length; In order to clean it up and avoid duplicating this several times over, i'd l...

Can't separate string using sscanf?

Hi, I have a text file of associated numbers i.e; 1 2 2 3 2 1 3 4 3 Each line is a seperate piece of information, as such I am trying to read it in one line at a time and then seperate it into the 3 numbers but sscanf isn't doing what I expect it to. char s[5]; char e[5]; char line[100]; int d; fgets(line, sizeof(line), inFile); ssc...

sscanf + c99 not working on some platforms ?

When I compile a simple Hello World! program that uses the sscanf function on my local Debian lenny x64, it works. But when I upload the same program to the server running CentOS x86, it will not work. If I do not use sscanf, then the program works on both computers. gcc -std=c99 -O2 -pipe -m32 If I compile it with sscanf but without -...