Results 1 to 4 of 4

Thread: แยกข้อมูลจาก string

  1. #1
    Junior Member
    Join Date
    Jun 2007
    Posts
    12


    อ่าคือ สมมติผมมี string ข้อความนึง


    1,Zinyx,5,6,7

    [/b]

    ที่ผมต้องการคือ อยากให้แยกมาเก็บใน Variables

    ผมใช้โค้ดแบบนี้


    sscanf(str,"%d,%s,%d,%d,%d",id,name,para1,para2,para3);

    [/b]
    แต่ไม่เวิค

    id = 1
    name = "Zinyx,5,6,7"
    มันเปนงี้อะครับ ขอคำแนะนำด้วยครับ

  2. #2


    ผมไม่ค่อยแน่ใจนะคับ ลองวิธีนี้ดูไหมคับ (พอดีผมไม่ได้ลง C++ ในเครื่อง)

    เท่าที่ผมดูจากเว็บต่างประเทศ ถ้าเป็นตัวเลข เขาจะใช้ Pointer ในการอ้างอิงนะคับ

    อันนี้จากเว็บญี่ปุ่น แต่ผมแปลงเป็นภาษาอังกฤษแทนนะคับ

    [code]
    #include
    [url="http://negimatfc.spaces.live.com"]Please Stop Global Warming[/url]
    [url="http://akiakira.elivebox.com"]Aki's tool[/url] [b]Update:[/b] [color=#444444]BASE 64 TOOL[/color]

    [b][url="http://akiakira.elivebox.com/akigraph"]akigraph[/url] [/b]- free graph opensource (php)

    [img]http://cftsubasa.tirkx.com/quiz/sakura.gif[/img]

  3. #3
    Junior Member
    Join Date
    Jun 2007
    Posts
    12


    ขอบคุณมากครับ แต่

    ไม่เป็นผลครับ ที่ได้ออกมาคือ

    Hello, Zinyx,5,6,7. Your id is 1 And para1 = 4072496, para2 = 4072528, para3 = 2
    009315348
    [/b]
    แต่ พอผมลองเปลี่ยนจาก , เป็น ' ' (สเปซ) ได้ผลครับ

    main()
    {
    char str[] = "1 Zinyx 5 6 7";
    int id, para1, para2, para3;
    char name[20];

    sscanf(str,"%d %s %d %d %d",&id,name,&para1,&para2,&para3);
    printf("Hello, %s. Your id is %d And para1 = %d, para2 = %d, para3 = %d",name,id,para1,para2,para3);
    }
    [/b]

    Output
    Hello, Zinyx. Your id is 1 And para1 = 5, para2 = 6, para3 = 7[/b]
    อื้ม ไม่ค่อยเข้าใจเหมือนกันครับ

  4. #4
    Junior Member
    Join Date
    Aug 2007
    Posts
    10


    sscanf มันจะตัดได้แต่ white space ครับ
    พอเจอ comma มันก็จะคิดว่าเป็น ส่วนหนึ่งของ string
    มันก็เลยอ่าน name ได้เป็น Zinyx,5,6,7 น่ะครับ

    อ้างอิงจาก
    http://www.cplusplus.com/reference/clibrar...dio/sscanf.html

    ถ้าอยากให้ตัด comma ได้ ลองใช้ strtok ดูนะครับ
    http://www.cplusplus.com/reference/clibrar...ing/strtok.html
    b = bit
    B = byte
    m = Milli
    K = Kilo
    M = Mega

    Mb = Mega Bit(s)
    MB = Mega Byte(s)

    8 bits = 1 Byte
    1024 Bytes = 1 KB
    1024 KB = 1 MB
    1024 MB = 1 GB
    1024 GB = 1 TB

Similar Threads

  1. การใช้switch() กับ string
    By ksolomon in forum C/C++,C#,VC++,MFC,Win32
    Replies: 4
    Last Post: 13-06-2008, 06:17 AM
  2. String สำหรับบอท RFI แสกนนะครับ
    By sniffer in forum Hacking, Exploit Articles/Tutorial/Techniques
    Replies: 0
    Last Post: 14-09-2007, 03:16 PM
  3. Occurence of character in string
    By asylu3 in forum Perl
    Replies: 0
    Last Post: 16-09-2004, 04:56 AM
  4. Using string in VC++
    By asylu3 in forum C/C++,C#,VC++,MFC,Win32
    Replies: 0
    Last Post: 07-01-2004, 07:45 PM
  5. Interger to string
    By asylu3 in forum C/C++,C#,VC++,MFC,Win32
    Replies: 0
    Last Post: 30-01-2003, 05:19 PM

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •