?
Solved

sscanf spliting

Posted on 2005-05-12
8
Medium Priority
?
309 Views
Last Modified: 2010-04-17
I have a string like this

XXXXXXXXXXXXXXXX   XXXXXXXXXXXXXXXXXXX

I am trying to use sscanf to put them into 2 different chars

wonder if this can be done with sscanf
0
Comment
Question by:WackoMike
  • 5
  • 3
8 Comments
 

Author Comment

by:WackoMike
ID: 13991263
when I do it I get
Value1: 2C4Z2376VC6E4TH9MC72V74H4BGKT6JF Value2: MC72V74H4BGKT6JF
from
sscanf(buffer, "%s%s", value1,value2);
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13991801
use this:
sscanf(buffer, "%[^ ] %s",value1, value2);

There is a white space after ^ and another one after ]
0
 

Author Comment

by:WackoMike
ID: 13992112
yeah did that aswell I get the same results there are 3 spaces
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13992150
Sorry, I don't undestand what you mean. This works:

char *sample="C4Z2376VC6E4TH9   MC72V74H4BGKT6JF";
char value1[30], value2[30];

sscanf(sample, "%[^ ] %s",value1, value2);
printf ("value1=%s\nvalue2=%s", value1, value2);
0
 

Author Comment

by:WackoMike
ID: 13992158
char buffer[1000];
            int nLineCount = 0;
            char curdir[MAX_PATH];
            GetCurrentDirectory(MAX_PATH,curdir);
            char file[MAX_PATH];
            sprintf(file,"%s%s",curdir,"\\keys.txt");
            fstream infile;
            infile.open(file, ios::in);
            char Classic[16];
            char LoD[16];
            char Key;
            
            while(!infile.eof() && !infile.fail())
            {
                  infile.getline(buffer, sizeof(buffer));
                  sscanf(buffer, "%[^ ] %s",Classic, LoD);
                  
                  Output->WriteEx(ConsoleOutput::RED,"Classic: %s LoD: %s\n", Classic,LoD);
          
                  
            }

            infile.close();
0
 

Author Comment

by:WackoMike
ID: 13992160
gives me

Classic: C4Z2376VC6E4TH9MC72V74H4BGKT6JF LoD: MC72V74H4BGKT6JF
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 13992232
What a strange combination of C and STL libraries.

try larger buffers, appears that you are out-bounding:
char Classic[20];
char LoD[20];

Also you can use string streams:
// will need these
#include <string>
#include <sstream>

          stringstream str;
          string buffer;

          int nLineCount = 0;
          char curdir[MAX_PATH];
          GetCurrentDirectory(MAX_PATH,curdir);
               
          string file = curdir;
          file += "\\keys.txt";

          fstream infile;
          infile.open(file.c_str(), ios::in);

          string Classic, LoD;
          char Key;
         
          while(!infile.eof() && !infile.fail())
          {
               infile.getline(buffer);
               str.str(buffer);
               str >> Classic >> LoD;
               
               Output->WriteEx(ConsoleOutput::RED,"Classic: %s LoD: %s\n", Classic.c_str(), LoD.c_str());
          }

          infile.close();
0
 

Author Comment

by:WackoMike
ID: 13992294
char Classic[20];
char LoD[20];

fixed all my problems lol thanks =)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question