0

thanks for the support on the previous post... i am now trying to store the vector string on another string and do a sscanf to check for a text eg. "TEXT" and if found append another text at the end of line....

any ideas???

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>

using namespace std;

int main() {
  vector<string> vs;
  vector<string>::iterator vsi;
  string agent;
  string buffer;
  string line;
  while (!(cin.eof())) {
    getline(cin, buffer);
    cout << buffer << endl;
    vs.push_back(buffer);
  };

  vsi = vs.begin();
  for (int count=1; vsi != vs.end(); vsi++,count++){
    cout << "string" << count <<"="<< *vsi << endl;
    line = *vsi;
    sscanf(line, "%s %[^\n]",text);
//dummy code
if text=="TEXT" do this:
    cout << "agent" << text << "endl";
  }
else: do nothing


  return 0;
}



[root@server dev]# g++ -o newcode newcode.cpp 
newcode.cpp: In function ‘int main()’:
newcode.cpp:24: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int sscanf(const char*, const char*, ...)’
[root@server dev]# 

UPDATE: i used line.c_str() on sscanf and gives out this error

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>

using namespace std;

int main() {
  vector<string> vs;
  vector<string>::iterator vsi;
  string agent;
  string buffer;
  string line;
  while (!(cin.eof())) {
    getline(cin, buffer);
    cout << buffer << endl;
    vs.push_back(buffer);
  };

  vsi = vs.begin();
  for (int count=1; vsi != vs.end(); vsi++,count++){
    cout << "string" << count <<"="<< *vsi << endl;
    line = *vsi;
    cout << "LINE:" << line.c_str() << endl;
    sscanf(line.c_str(), "%s %[^\n]",agent);
 /*   cout << "agent" << agent << "endl"; */
  }

  return 0;
}

[root@server dev]# g++ -o newcode newcode.cpp 
newcode.cpp: In function ‘int main()’:
newcode.cpp:25: warning: cannot pass objects of non-POD type ‘struct std::string’ through ‘...’; call will abort at runtime
[root@server dev]# 

what i want to do is when a string on a particular line is detected it will append a text to the end of line and stdout...

Community
  • 1
  • 1
krisdigitx
  • 7,068
  • 20
  • 61
  • 97
  • I see you have edited the question, can you put a comment with the line number that the error is occurring on. Something like `\\this is line 25` would be very helpful. – shuttle87 Oct 18 '12 at 22:06

3 Answers3

1

It's not entirely clear what you are trying to do but if you want to get the right type passed into sscanf you'll need to change some things around. sscanf only deals with c strings and you've tried to pass it in a c++ string object, to fix this you might want to use line.c_str() in sscanf to get it passed in the right format.

A better approach would be to use a c++ algorithm like string::find though as this will remove the need to use sscanf.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
0

I'm not sure of you want to do, but you should check out string::find : Cplusplus reference

lucasg
  • 10,734
  • 4
  • 35
  • 57
0

Instead of using sscanf try using stringstream which work nearly the same as cin/cout.

Hauleth
  • 22,873
  • 4
  • 61
  • 112