Linux -Blue forest free software | Return to home page | Site Map | Search WWW | Contact Us |
Your current position : Homepage > Free Software > Technological exchanges >Application Programming


    

Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006


C problems,

A string STR[]= "123||456||789||543||3453453||. . . . . . . . . . . . . . ||765 ";
| | How to split it and to the memory array ARRAY?
I used strtok () some problems,
#include "Stdio.h"
#include "String.h"
Main () (
Char str[]= "123||345||5676||5656||3||56||676";
Int i=0;
Int arr[50]={0};
Arr[0]=atoi (strtok (str, "| |"));
While (arr[i++]=atoi (strtok (NULL, "| |")))
          ;
Return 0
}

However, the last time to deal with mistakes, I forgot what is wrong, you help me find What is the problem? Thank you

C problems,

array = Split (/||/, str);

Then, using Perl bars.

C problems,

Char *p1; LEAVES OF 13 SPECIES OF LAURACEAE cycle to find the '|' */
Character location */ char record *p2; LEAVES OF 13 SPECIES OF LAURACEAE
P1 = str;
P2 = str;
While (p> '\0') (
If (p ====== '|') (
Strncpy (array, p2, P1);
P1 = p1 + 2;
P2 = =
     }
}
Wondered not, no compiler, you try!

C problems,

-->

Brother, in this 啊??? : Why use Perl? :

C problems,

However, the procedures Lane how to use perl?

C problems,

No matter what the outcome or wrong, I will not write procedures while (arr[i++]=atoi (strtok (NULL, "| |")));
Easily overflow attacks, Kazakhstan

I found two mistakes :
1. I++ be ++i
2. Strtok check whether the returned value is not NULL, NULL lead to a cycle atoi used for parameters of the natural segmentation fault.
Amended to read as follows :
[code]
#include "Stdio.h"
#include "String.h"
Main ()
{
Char 6-12, str[] = "123||345||5676||5656||3||56||676";
Int i = 0
Int arr[50] = (0)
P = strtok (str, "| |");
While (p> NULL) (
Arr[i] = atoi (p);
Printf ( "arr[%d]=%d\n", i, arr[i]);
If (++i ";= 50) (
Printf ( "out of range!\n");
Break;
    }
P = strtok (NULL, "| |");
  }
Return 0
}
[/code]

C problems,

Strtok NULL return is a mistake because in the end, no longer the atoi. Another is ++i not i++.
This :
Char *p;
While ((p = strtok (NULL, "| |")) & (arr[++i] = atoi (p)))
;
Sscanf also be used if the number of fixed without more :
Sscanf (str, "%d||%d||%d||%d||%d||%d||%d" &arr[0], &arr[1], &arr[2], &arr[3], &arr[4], &arr[5], &arr[6]);

C problems,

Thank you, have to get that :)

C problems,

While (arr[i++]=atoi (strtok (NULL, "| |"))) 4003rd fewer brackets "("



 Privacy Policy  Copyright © 1999-2000 LSLNET.COM. All rights reserved. Blue Forest website owners. E-mail : Webmaster@lslnet.com