FAQ Database Discussion Community


how to parse three comma separated strings from terminal using fgets()/sscanf()

c,sscanf
After reading Parsing a comma separated file using C using fscanf() I made some progress but I'm still having difficulty parsing this. Input from the console is expected to be up to three strings, separated by commas e.g. "String1,String2,String3" or "String1,String2" or "String1" I can handle input validation after I...

Choose formats in sscanf in c

c,string,tokenize,sscanf
I am trying to parse a string Connected to a:b:c:d completed (reauth) id=5 using sscanf() in c language. My format string is Connected to %s completed %s id=%s. But In some cases my string is Connected to a:b:c:d completed id=5. I am not getting that reauth part. I am able...

How to read integers with sscanf separated by symbols in C?

c,sscanf
so I have a string with the following value [7, 8] Now I wan't to store each value (only the numbers) in a 2D array [1][2]. I'm pretty sure using sscanf is the best way to do this, but how do I tell sccanf to only read the integers? Thanks...

Sscanf - parsing with different delimiters

c,string,io,sscanf
I want parse my string using sscanf: char string[] = "/home/my/estf 122,323 452,323 662,343"; First element of string it's path and next are ints where comma or white characters are delimiters. This is my full code: #include <stdio.h> #include <stdlib.h> int main(void) { char string[] = "/home/my/estf 122,323 452,323 662,343";...

sscanf optional format specifier or parameter

c,sscanf
I have a string that I want to parse using sscanf with optional parameter in it char text1[] = "ATEC\rATECASA"; char text2[] = "ATEChehe\rATECASA"; char header[3]; char command[3]; char data[128]; int result = sscanf_s(text1, "%2s%2s%s\r", header, command, data); //actual output to data variable //output text1, data = ATECASA //output text2,...

How to read string until two consecutive spaces?

c,format,sscanf,c-strings
A well known function of the scanf() functions is that you can pass a format to scan input according to this format. For my case, I cannot seem to find a solution searching this and this documentation. I have a string (sInput) as the following: #something VAR1 this is a...

Extract numbers from string in MATLAB

matlab,sscanf
I'm working with sscanf to extract a number from a string. The strings are usually in the form of: '44 ppm' '10 gallons' '23.4 inches' but ocassionally they are in the form of: '<1 ppm' If I use the following code: x = sscanf('1 ppm','%f') I get an output of...

sscanf not working right?

c,sscanf
I am trying to parse a URL, and wrote this code: #include <stdio.h> int main() { char host[100]; char port[100]; char path[100]; char prot[100]; char* url = "https://google.com:8000/foobar"; sscanf(url, "%s://%s:%s/%s", prot, host, port, path); printf("Protocol: %s\n", prot); printf("Host: %s\n", host); printf("Port: %s\n", port); printf("Path: %s\n", path); return 0; } However,...

C sscanf (fscanf) behaving differently row from row

c,scanf,fscanf,sscanf
I've this snippet of code which should read text from input file and put it in a struct. void load(FILE *fin, struct camion payload[]){ int i=0; char except; char buf[1000]; while (fgets(buf,sizeof(buf),fin)){ except='A'; sscanf(buf,"%i-%i-%i %i %s %c",&payload[i].day,&payload[i]. month,&payload[i].year,&payload[i].nparcels,payload[i].origin,&except); if (except=='E') payload[i].except=1; i++; } } The fgets works as it should...

Get integer after a character from file

c,sscanf
I'm trying to read the number of a txt file like this: input=20 output=10 hidden=5 .... I tried with this code: char line[30]; char values[100][20]; int i = 0; FILE *fp; fp = fopen("myFile.txt", "r"); if(fp == NULL) { printf("cannot open file\n"); return 0; } while(fgets(line, sizeof(line), fp) != NULL)...

sscanf parse formated string

c,parsing,sscanf
I would like to read a string containing a undefined amount of suffixes, all separated by ; example 1: « .txt;.jpg;.png » example 2: « .txt;.ods;_music.mp3;.mjpeg;.ext1;.ext2 » I browsed the web and wrote that piece of code that doesn't work: char *suffix[MAX]; /* will containt pointers to the different suffixes...

sscanf failing on splitting string to double values

c++,c,sscanf
I do not know what mistake I am making here. But sscanf is not filling up the values in my double arrays (first indexes). here is code int main() { int n = 0; cout << "Enter the number of equations" << endl; cin >> n; string coeffData; string powerData;...

How to parse almost-defined string in C?

c,sscanf
I have a string to parse. This string has an almost-defined format with GPS coordinates. I want to get the coordinates. Sometimes I have this : 09:24:29 N 012:34:35 W, 09:22:18 N 012:33:55 W But sometimes I have extra whitespaces : 09:24:29 N 012:34:35 W , 09:22:18 N 012:33:55 W...

Why is sscanf not reading the first argument in the format string?

c,string,string-formatting,sscanf
In the following program, everything but the first value (the month) in the format string is being read into the SYSTEMTIME structure. Can anyone help me figure this one out? #include <Windows.h> #include <stdio.h> int main() { SYSTEMTIME st; char buf[50]; strcpy(buf, "6/23/2015 12:00:00"); sscanf(buf, "%d/%d/%d %d:%d:%d", &st.wMonth, &st.wDay, &st.wYear,...

Read a line in C and split it

c,fread,sscanf
I got a problem trying to split 'str' in 2 int vars and 1 char var. Here is a code: FILE *fp; int i,j; char str[8]; char valor; if(!(fp = fopen("miniflota_g.dat","rb"))){ printf("Error opening file."); getch(); return; } while(fread(str,sizeof(str),1,fp) == 1){ sscanf(str,"%d %d %c",i,j,valor); printf("%d %d %c",i,j,valor); } fclose(fp); And this...

Read more than one word from string with sscanf

c,string,sscanf
I am trying to read formatted content from a file. To do so, I read line by line using fgets() and sscanf(). The content of the file is supposed to be a table. One row would look like the following example: 456 2 39 chained_words 62.5 // comment with more...

Regex - the difference in \\n and \n

php,regex,datetime,syntax,sscanf
Sorry to add another "Regex explanation" question to the internet but I must know the reason for this. I have ran this regex through RegexBuddy and Regex101.com with no help. I came across the following regex ("%4d%[^\\n]") while debugging a time parsing function. Every now and then I would receive...

using sscanf to read fixed length strings

php,sscanf
I'm trying to sscanf to read a bunch of successive fixed length strings but it's not working very well. I can print a fixed length string thusly: sprintf('%.5s', 'aaaaabbbbb'); But if I try to use %.5s to read a fixed length string (eg. the first 5 bytes of an input...

Reading from txt file in C using scanf

c,file,scanf,sscanf
i have a txt file and i want to read from it. I know that i will have to read 20 lines(each line contain 3 number variables,for instance 10 5 6 and so on) #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int x,y,z; int counter = 0;...

How to read in a specific number with sscanf from lines which are similar?

c,sscanf
I'm trying to read in a specific number from a file with the sscanf()function in the C standard library. My example data comes from /proc/stat on a system running a Linux Kernel. Here is how it looks: cpu 90158 11772 50095 6885572 36975 0 207 0 0 0 cpu0 22942...