Thread: parsing through a text file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Registered User
    Join Date
    May 2012
    Posts
    13
    I don't think I'm properly grasping why this isn't working.
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <iomanip>

    using namespace std;

    int main()
    {

    string line;
    ifstream myfile;
    ofstream myfile2, myfile3;
    myfile.open("input.txt");
    myfile2.open("output.txt");
    myfile3.open("logfile.txt");
    myfile2<<"column1\t\t"<<"column2\t\t"<<column3<<endl;

    while(getline(myfile,line)
    {
    stringstream ss(line);
    ss.clear();

    int x[20],y,z,hours,minutes,index;
    char colon;
    double seconds;
    string junk;
    string word1;

    if(ss>> hours>>colon>>minutes>>colon>>seconds && colon==':')
    {
    myfile3<<"success reading time code\n";
    myfile<<hours<<":"<<minutes<<":"<<seconds<<endl;
    ss<<"";
    ss.clear();

    }

    else if(ss>>index>>colon>>hex>>x[0]>>hex>>x[1]>>hex>>x[2]>>hex>>x[3]>>hex>>x[4]>>hex>>x[5]>>hex>>x[6]>>hex>>x[7]>>hex>>x[8]>>hex>>x[9]>>hex>>x[10]>>hex>>x[11]>>hex>>x[12]>>hex>>x[13]>>hex>>x[14]>>hex>>x[15]>>hex>>x[16]>>hex>>x[17]>>hex>>x[18]>>hex>>x[19] && colon==":")
    {
    myfile3<<"success reading hex code line\n";
    myfile2<<x[0]<<endl;
    ss<<"";
    ss.clear();

    }

    else{
    myfile3<<"error"<<endl;
    ss<<"";
    ss.clear();
    }


    }

    myfile.close();
    myfile2.close();
    myfile3.close();
    return0

    }


    I've added what I have found in a few guides, and put ss<<""; and ss.clear(); into every entry. And it still isn't working. Am I just not understanding the way this program is being read?
    Last edited by oopsyourhead; 05-15-2012 at 01:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parsing specific text from xml file
    By rabers in forum C Programming
    Replies: 5
    Last Post: 03-07-2011, 02:14 AM
  2. Text file parsing
    By papagaio in forum C Programming
    Replies: 7
    Last Post: 10-01-2009, 04:47 PM
  3. Help parsing text file
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 07-16-2008, 10:21 AM
  4. Problem parsing comments and such in text file
    By zaxxon in forum C Programming
    Replies: 3
    Last Post: 08-09-2004, 12:14 AM
  5. Text file parsing
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2002, 01:17 AM