DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: file parsing [was:Urgent Help in Solving my problem!!!!]

  1. #1
    Join Date
    Feb 2006
    Posts
    8

    file parsing [was:Urgent Help in Solving my problem!!!!]

    Hi
    Can anyone help me to find the solution for my urgent problem?
    My problem is:

    I need to read file suppose "output.txt".
    the content in the file looks like this:
    1.00000- 5 0.00000+0 1.03912+4 0.00000+0 1.03912+4 1.48000+16457 3
    14454.4000 1.26000+1 18764.3000 1.13615+1 21379.6000 1.10428+16457 3
    23453.5000 1.08531+1 30000.0000 1.03848+1 35272.2000 1.01028+16457 3
    40000.0000 9.92998+0 45365.5000 9.75999+0 57500.0000 9.47555+06457 3
    65000.0000 9.27821+0 71033.3000 9.08284+0 85448.7000 8.76920+06457 3
    98085.2000 8.55090+0 113163.000 8.35730+0 137582.000 8.12755+06457 3

    I need to store every alternate element in a 2 different vector containing string.
    Please note that , I must consider the floating point "1.00000- 5" as a single string.
    Please provide me efficient solution.
    RAj

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    It would be quite simple if there was no spaces between - and 5..
    Does the format have to be like this?
    Maybe you could use whitespace as a delimiter and if the last digit of the prevoius string is "-" or "+" then you concatenate this string with the next string?

  3. #3
    Join Date
    Feb 2006
    Posts
    8
    Yeh,
    The problem is the format of the file.
    The format is always like this and I need to maintain the standard.
    Could you please ******** the later half of your answer with the sample code?
    I am finding difficulty on reading line and comparing.
    Raj

  4. #4
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    You could maybe do something like?
    Code:
    #include <fstream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string test;
    	fstream fin;
    	vector <string> nums;
    	fin.open("numbers.txt", ios::in);
    	while(!fin.eof())
    	{
    		getline(fin, test, ' ');
    		nums.push_back(test);
    	}
    	return 0;
    }
    This will put all the textfile into a vector using a ' ' as a delimiter. All you have to do then is find each string that ends with a + or - and combine it with the next string and alter the vector.. ;)

  5. #5
    Join Date
    Feb 2006
    Posts
    8
    Hi Code Nerd,
    I tried to run the code :
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    using namespace std;

    int main()
    {
    string value1;
    string value2;
    string final_string;
    fstream fin;
    vector <string> total;
    vector <string> last_value;
    //int len;
    //fin.open("test1.txt", ios::in);
    fin.open("test1.txt");
    if( !fin.is_open() ) {
    cout << "Error opening file" << endl;
    return 0;
    }
    else
    {
    int k=0;
    while(!fin.eof())
    {
    getline(fin,value1,'');
    total.push_back(value1);
    k++;
    }
    cout<<k<<endl;
    cout<<total.size()<<endl;
    for(int x = 0; x < total.size(); x++)
    //cout<<total.at(x)<<endl;
    //for(int i=0;i < str_Vector.size(); i++)
    {
    //std::string strd = str_Vector.at(x);
    //int y = i*7;
    //last_value.push_back(test(y));

    //if
    cout<<total.at(x)<<endl;
    }



    return 0;


    }
    if( fin.is_open() )
    fin.close();
    }

    but it gives me following error:
    "27 C:\Documents and Settings\Mahesh Neupane\My Documents\test_program1.cpp no matching function for call to `getline(std::fstream&, std::string&, int)' "
    Could you please tell me whats happening?
    RAJ

  6. #6
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Do you have a space between ' and '?

  7. #7
    Join Date
    Feb 2006
    Posts
    8
    Hi Code Nerd,
    I am able to run the code but I think it also considers space as a character hence produces vector of large number.
    Could you please debug the following code?

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    #include <stdlib.h>
    using namespace std;
    // using std::string;

    int main()
    {
    string value1,temp1;
    string temp2,temp;
    string value2;
    string final_string;
    fstream fin;
    vector <string> total;
    vector <string> energy;
    vector <string> cross;
    vector <string> last_value;
    vector<string> total_new;
    //int len;
    //fin.open("test1.txt", ios::in);
    fin.open("test1.txt");
    if( !fin.is_open() ) {
    cout << "Error opening file" << endl;
    return 0;
    }
    else
    {
    int k=0;
    while(!fin.eof())
    {
    getline(fin,value1,' ');
    total.push_back(value1);
    k++;
    }
    // cout<<k<<endl;
    cout<<total.size()<<endl;
    string c =' ';
    for(int z =0; z < total.size(); z++)
    { string t1 = total.at(z);
    if (t1 ==c)
    total.erase(z);
    else total_new.pushback(total.at(z));
    }
    int y=7;
    for(int x =0; x < total.size(); x++)
    { int i = 0;
    int m = 1;
    int count = 1;
    while (count != 6)
    {string temp1=total.at(x+i);
    energy.push_back(temp1);

    string temp2= total.at(x+i+1);
    cross.push_back(temp2);
    //To display 7th character
    //string temp3 = total.at(y);
    //last_value.push_back(temp3);
    cout<<"The Energy is: "<<temp1<<" \nCorresponding Crossection is: "<<temp2<<endl;
    //cout<<total.at(y)<<endl;
    // y= x+m*7+1;
    //m=m+1;
    //cout<<"\nThe "<< m<<" 7th Value is: \n"<<temp3;

    //int len=0;
    // for (int k = 0; k<temp.size();k++)
    //{len++;
    //}
    i= i+1;
    count = count +1;

    }
    // for (int j = 0; j<= len;j++)
    //{
    //char c = temp[j];
    //if (c== '+'|| c=='-')
    //temp1= total.at(x+1);
    //temp2 = temp+temp1;

    }
    //total.pushback(temp2);

    //To display 7th character
    //if (y < total.size())
    //{cout<<"this is "<<y<<" number"<<endl;
    //cout<<total.at(y)<<endl;
    //y= x+i*7;
    //}
    //}
    int count = 1;
    cout<<"the processed string "<< count << "the strings are"<< temp2<<endl;

    count++;
    return 0;


    }
    if( fin.is_open() )
    fin.close();
    }



    The required output should be in two vector Energy and Cross-section:
    Energy Cross-section
    1.0000-5 0.00000+0

    And my data files contains following text:(test1.txt)
    1.00000-5 0.00000+0 1.03912+4 0.00000+0 1.03912+4 1.48000+16457 3 1
    14454.4000 1.26000+1 18764.3000 1.13615+1 21379.6000 1.10428+16457 3 1
    23453.5000 1.08531+1 30000.0000 1.03848+1 35272.2000 1.01028+16457 3 1
    40000.0000 9.92998+0 45365.5000 9.75999+0 57500.0000 9.47555+06457 3 1
    65000.0000 9.27821+0 71033.3000 9.08284+0 85448.7000 8.76920+06457 3 1
    98085.2000 8.55090+0 113163.000 8.35730+0 137582.000 8.12755+06457 3 1


    I would appreciate this help from you.
    RAj

  8. #8
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    I dont quite understand what your problem is now?
    Your output looks correct to me?

    Can you please show me what the correct output should look like?

  9. #9
    Join Date
    Feb 2006
    Posts
    8

    The output should look like:(CODE NERD)

    Hi CodeNerd,
    If my input file contain the data like this:
    ///DATA in Text File: test1.txt///
    1.000000-5 0.000000+0 2.530000-2 0.000000+0 1.039120+4 0.000000+0
    1.039120+4 1.480000+1 1.445440+4 1.260000+1 1.876430+4 1.136150+1
    2.000000+4 1.120460+1 2.137960+4 1.104280+1 2.345350+4 1.085310+1
    2.936290+4 1.042280+1 3.000000+4 1.038480+1 3.527220+4 1.010280+1
    4.000000+4 9.929980+0 4.536550+4 9.759990+0 5.000000+4 9.647780+0
    5.485030+4 9.542180+0 5.545870+4 9.529670+0 5.750000+4 9.475550+0
    6.000000+4 9.412200+0 6.250000+4 9.351890+0 6.276480+4 9.345660+0
    6.441140+4 9.295700+0 6.500000+4 9.278210+0 6.677120+4 9.226720+0
    6.750000+4 9.201310+0 7.000000+4 9.116710+0 7.103330+4 9.082840+0
    7.500000+4 8.992060+0 7.756670+4 8.933330+0 8.000000+4 8.882660+0

    My out put should have :
    1. two vector that contains: A = [1.0000-5 2.53000-2 1.03912...]//alternate value
    B = [0.000000+0 0.000000+0 0.000000+0 1.480000+1....]
    "I already did this and able to read them in string vector"

    2. Now I should be able to read each string from the vector and convert them into the floating point and store them into two new vector : A_new= [ 0.000001 0.0025300 103912...] and similarly B_new.

    How can i do this efficiently?
    I am waiting for your response.
    RAJ

  10. #10
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Its going to be quite tricky because you will need to convert a part of your string to int to know how far to move the decimal place back.
    What I would do first was search each string for + or - with find()

    Code:
    size_type pos = str.find( '+', 0 );
    if( pos != string::npos )
            //found
    else //not found
    Then you will need get the value of the number after + or -. you can do this using at()

    Code:
    string dec_move = str.at(pos+1);
    This value is still stringf type however, so you will need to converty to int to enable you to know how many decimal places to move, to do this use atoi() (#include <cstdlib>)
    Code:
    int dec = atoi(dec_move);
    So now you have the decimal places and the which way ot manipulate your number you should be able to go ahead and do it.. :)

    Hope this is clear enough for you?

  11. #11
    Join Date
    Nov 2003
    Posts
    4,118
    atoi doesn't work with string objects. Either call dec_move.c_str(), which is a kludge of course, or simply use stringstream objects to convert between string to double:
    http://www.devx.com/DevX/LegacyLink/9399
    Danny Kalev

  12. #12
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Quote Originally Posted by Danny
    atoi doesn't work with string objects. Either call dec_move.c_str(), which is a kludge of course, or simply use stringstream objects to convert between string to double:
    http://www.devx.com/DevX/LegacyLink/9399
    Yes Sorry I forgot to add that, it takes a char * so you will need to convert it with c_str()..

    Thanks Danny :)

  13. #13
    Join Date
    Feb 2006
    Posts
    8
    Hi Danny and Code Nurd,
    It seems like atoi doesnot work well here:
    ////////////////My Code//////////////
    std::string temp1 = "1.00016+4";

    int len1 = temp1.length();
    cout<<"The length is:\n"<<len1<<endl;
    int pos = temp1.find('+',0);
    cout<<"the position of + is :\n"<<pos<<endl;
    // int pos3 = temp1.find('-',0);
    // if temp1[pos] =
    std::string s1 = temp1.substr(pos+1,len1);
    cout<<"The position after the decimal s1 is:\n"<<s1<<endl;
    std::string s2 = temp1.substr(0,pos);
    cout<<"The position before the decimal s2 is:\n"<<s2<<endl;
    int i,j;
    i = atoi(s2.c_str());
    cout<<"The changed s2\n"<<i<<endl;


    j = atoi(s1.c_str());
    cout<<"The changed S1\n"<<j<<endl;

    //////End of Code//////////

    It seems like the out put for the digit before decimal point after using:
    atoi(s2.c_str())
    produces one '1' as output instead of 1.00016 .
    Please tell me what is wrong in my code.
    Raj

  14. #14
    Join Date
    Dec 2003
    Posts
    3,366
    ATOI isnt smart enough for this "not quite right" scientific notation. C++ scientific notation is -1.000e-10 (I think, I dont use it a lot, but I am fairly sure this is it).
    You need to manually insert the e's or parse the string yourself (home rolled is probably faster, actually, atoi and atof are fairly "slow" if your files are *huge*).

  15. #15
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    It may not be the best way but after you find how many places the decimal needs to move you can use erase() and insert() from the string class to manipulate the decimal manually.. :)

    You can find info on them Here

Similar Threads

  1. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 07:48 PM
  2. Replies: 1
    Last Post: 03-15-2002, 01:55 PM
  3. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 05:34 PM
  4. file name==class name. why?
    By siva in forum Java
    Replies: 2
    Last Post: 01-03-2001, 11:40 AM
  5. Parsing a XML file to a flat file
    By Stoph in forum XML
    Replies: 3
    Last Post: 09-11-2000, 11:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center