Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
Which format should I use: scanf \string,string,int,int/?

I have data in the following format \a,b,c,d/ where a,b are strings of letters and numbers; c, d are integers.

I tried using format \%s,%s,%d,%d/ format to scan it, but that causes a,b,c,d/ to be scanf'ed into the first string instead of only a.

Question:

Is there something I could type in the format in order to achieve desired result?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use the following format string to use commas as delimiters :

"\\%[^,],%[^,],%d,%d/"

The idea is to tell scanf to read anything that isn't a comma for each string, then read the delimiting comma and continue.

Here is a (bad and unsafe!) example:

char a[100], b[100];
int c=0, d=0;

scanf("\\%[^','],%[^','],%d,%d/", a, b, &c, &d);
printf("%s, %s, %d, %d\n", a, b, c, d);

In real code, you'll want to write something safer. You can for example use fgets to read a full line of input then reuse the same format string with sscanf to parse it.

over 4 years ago · Santiago Trujillo Denunciar

0

Read carefully the documentation of fscanf(3).

You might try something like

char str1[80];
char str2[80];
memset (str1, 0, sizeof(str1));
memset (str2, 0, sizeof(str2));
int  n3 = 0, n4 = 0;
int pos = -1;
if (scanf ("\\ %79[A-Za-z0-9], %79[A-Za-z0-9], %d, %d /%n",
           str1, str2, &n3, &n4, &pos) >= 4
    && pos > 0) {
   // be happy with your input
 }
 else {
   // input failure
 }

That won't work if you have a wider notion of letters, like French é or Russian Ы ; both are single letters existing in UTF-8 but represented in several bytes.

I added some spaces (mostly for readability) in the format string (but scanf is often skipping spaces anyway, e.g. for %d). If you don't accept spaces -like an input line such as \AB3T, C54x, 234, 65/ , read each line with getline(3) or fgets(3) and parse it manually (perhaps with the help of sscanf and strtol ...). Notice that %d is skipping spaces! I also am clearing the variables to get more deterministic behavior. Notice that %n gives you the amount of read characters (actually, bytes!) and that scanf returns the number of scanned items.

over 4 years ago · Santiago Trujillo Denunciar

0

My straightforward solution:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char a[10010];

    gets(a);
    int l = strlen(a);

    char storded_first[10010], storded_second[10010];
    char for_int_c[10010], for_int_d[10010];
    int c,d;

    char first_symbol, last_symbol;
    int i;
    int cnt = 0;
    int j=0;

    for(i=0; i<l; i++)
    {
        if(a[i]=='\\')
            first_symbol = a[i];
        else if(a[i]=='/')
            last_symbol = a[i];

        else if(a[i]==',')
        {
            cnt++;
            j=0;
        }

        else if(cnt==0)
        {
            storded_first[j]=a[i];
            j++;
        }
        else if(cnt==1)
        {
            storded_second[j]=a[i];
            j++;
        }
        else if(cnt==2)
        {
            for_int_c[j]=a[i];
            j++;
        }
        else if(cnt==3)
        {
            for_int_d[j]=a[i];
            j++;
        }
    }

    c = atoi(for_int_c);
    d = atoi(for_int_d);


    printf("%c%s, %s, %d, %d%c\n",first_symbol, storded_first, storded_second, c, d, last_symbol);

    return 0;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda