How can i make sscanf() skip all whitespace characters?I read from a file and i dont know where are the characters.
eg.->@Solomon Islands ,i need "Solomon Islands" in one string
e.g->AGAF (AFT) – Afutara Airport – Afutara ,i need "Afutara Airport" in one string.

How can i make sscanf() skip all whitespace characters?

It's awkward for string data. You might be better off manually extracting the data you want.

eg.->@Solomon Islands ,i need "Solomon Islands" in one string

The boundary characters matter. I assume the string starts with '@', but if it ends with any of the internal whitespace you want to keep, you're going to get more than you want. For the example, let's assume there's a newline after "Islands" to keep it simple:

/*
  Breakdown:

  @: Read and ignore a literal '@' character
  %[^\n]: Read everything up to a newline character into dst
*/
sscanf(src, "@%[^\n]", dst);

e.g->AGAF (AFT) – Afutara Airport – Afutara ,i need "Afutara Airport" in one string.

/*
  Breakdown:

  %*[^-]: Read and ignore everything up to a '-' character
  -: Read and ignore the literal '-' still in the stream
  space: Read and ignore leading whitespace
  %[^\n]: Read everything up to a '-' character into dst
*/
sscanf(src, "%*[^-]- %[^-]", dst);

Note that this will leave trailing whitespace in dst.

If i want to read this line-->AGAR (RNA) – Ulawa Airport – Arona, Ulawa Island
and store:
AGAR in var1
RNA in var2
Ulawa Airport in var3
Arona, Ulawa Island in var4

this command is ok?

sscanf(buffer, "%s (%s) %*[^-]- %[^-] %s", var1,var2,var3,var4);

I dont think so..Anybody that can plsease help me.Thanks in advance.

If i want to read this line-->AGAR (RNA) – Ulawa Airport – Arona, Ulawa Island
and store:
AGAR in var1
RNA in var2
Ulawa Airport in var3
Arona, Ulawa Island in var4

this command is ok?

sscanf(buffer, "%s (%s) %*[^-]- %[^-] %s", var1,var2,var3,var4);

I dont think so..Anybody that can plsease help me.Thanks in advance.

The use of sscanf() in that way can be very convoluted.

sscanf(buffer, "%s (%[^)]) - %[^-]- %[^0]", var1,var2,var3,var4);

or

sscanf(buffer, "%[A-Z] (%[A-Z]) - %[a-zA-Z ]- %[A-Za-z, ]", var1, var2, var3, var4);

Still the red portion is there to make a point. Since you want var3 to hold a space in between words, there's a space trailing as well. >>Ulawa Airport <<

Still not working...Help if possible pls.Thanks.

"Not working" is not helpful. I notice that you're using '–' in the example string and '-' in the format string. While they look very similar, they're two different characters. To avoid that confusion, let's consider them both in the format string. The following should work almost perfectly:

#include <stdio.h>

int main(void)
{
    const char *p = "AGAR (RNA) – Ulawa Airport – Arona, Ulawa Island";
    char var1[BUFSIZ] = "";
    char var2[BUFSIZ] = "";
    char var3[BUFSIZ] = "";
    char var4[BUFSIZ] = "";

    sscanf(p, "%s ( %[^)]) %*[–-] %[^–-]%*[–-] %[^\n]", var1, var2, var3, var4);
    printf(">%s<\n>%s<\n>%s<\n>%s<\n", var1, var2, var3, var4);

    return 0;
}

I say almost because var3 should have trailing whitespace that still needs to be trimmed.

%[a-zA-Z ]

Note that ranges like this are not portable.

"Not working" is not helpful. I notice that you're using '–' in the example string and '-' in the format string. While they look very similar, they're two different characters. To avoid that confusion, let's consider them both in the format string. The following should work almost perfectly:

#include <stdio.h>

int main(void)
{
    const char *p = "AGAR (RNA) – Ulawa Airport – Arona, Ulawa Island";
    char var1[BUFSIZ] = "";
    char var2[BUFSIZ] = "";
    char var3[BUFSIZ] = "";
    char var4[BUFSIZ] = "";

    sscanf(p, "%s ( %[^)]) %*[–-] %[^–-]%*[–-] %[^\n]", var1, var2, var3, var4);
    printf(">%s<\n>%s<\n>%s<\n>%s<\n", var1, var2, var3, var4);

    return 0;
}

I say almost because var3 should have trailing whitespace that still needs to be trimmed.

sscanf(p, "%s ( %[^)]) %*[–-] %[^–-]%*[–-] %[^\n]", var1, var2, var3, var4);

While most likely, I suspect, there's a newline break in the OP's, there's none in this example, creating the potential of reading more than it should.

sscanf(p, "%s ( %[^)]) %*[–-] %[^–-]%*[–-] %[^\n]", var1, var2, var3, var4);

While most likely, I suspect, there's a newline break in the OP's, there's none in this example, creating the potential of reading more than it should.

It reads to a newline or end-of-file. Until the OP deigns to tell us enough about the format to know what comes after the example, we can only assume. I made it clear already that boundaries matter, so the OP either needs to figure it out on his own, or post a complete format description.

But regardless of that particular detail, he can still take advantage of the code to solve his problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.