the file looks like:

11/29/2010 S 2907 1
11/29/2010 S 9673 4
11/30/2010 A 4321 30
11/31/2010 S 9673 12 
12/01/2010 N 5789 wind_chimes 13.54 17.83
12/02/2010 S 5140 5

How can I extract out "2907" from file using >>?

i think its myfile >> then something.

#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

struct item_format 
{
string ID;
string item_name;
int current_stock;
int ordered_stock;
float retail_price;
float selling_price;
}item_info[30], greeting_cards, note_cards, calendars, pens, candles, wind_chimes;

int main () {

int i;
string line;
string data[6];
int datacounter = 0;
string substr = "";


ifstream lastfile;
lastfile.open("lastweek.txt"); 

ifstream myfile;
myfile.open ("transaction.txt");

ofstream thisfile;
thisfile.open ("thisweek.txt");


for(int index = 0; index < 5; index++) {  //gets info into array

lastfile >> item_info[index].current_stock; // c=0
lastfile >> item_info[index].ID;  //c=1
lastfile >> item_info[index].item_name;  //c=2
lastfile >> item_info[index].ordered_stock;  //c=3
lastfile >> item_info[index].retail_price;  //c=4
lastfile >> item_info[index].selling_price;  //c=5
}
cout << item_info[0].current_stock<<endl;
cout << item_info[0].ID<<endl;
cout << item_info[0].item_name<<endl;
cout << item_info[0].ordered_stock<<endl;
cout << item_info[0].retail_price<<endl;
cout << item_info[0].selling_price<<endl;

//getline(myfile,line);
//cout << line <<endl;

  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      //cout << line << endl;
    }

  }

  else cout << "Unable to open file"; 

  int d,m,y,id,n,counter;
  char type,junk,S;
  string t="";

  while(!myfile.eof)
  {
       myfile>>t>>t>>t;
           //junk>>d>>d>>junk>>y>>y>>y>>y>>S>>id>>id>>id>>id>>n;
       cout<<t;

  }


return 0;
}

You're on the right track. In the snippet below, the stringstream acts just like your file stream.

#include <iostream>
#include <string>
#include <sstream>

int main(int, char *[])
{
  std::string line = "11/29/2010 S 2907 1";

  int myInt;
  char myChar;
  std::string myString;
  
  std::stringstream ss;
  ss << line;
  ss >> myInt >> myChar >> myInt >> myChar >> myInt >> myString >> myInt;

  std::cout << myInt << std::endl;
  return 0;
}

Please try to simplify your problem down to something as close to this length as possible. It makes it easier for you to figure out what is going on and easier for us to help you :)

David

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.