how to get the last line 10 char in a text file if the last line is null?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roberttonafelix
    New Member
    • Oct 2012
    • 16

    how to get the last line 10 char in a text file if the last line is null?

    Dear all,

    I can able to get the last line ten chars but i cant get if the last line is null.

    here is my code
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    using namespace std;
    int main(int argc, char *argv[])
    {
        ifstream SVfile("D:\\bys\\LOG.txt");
        string sLine;
        string SVcode;
        getline(SVfile, sLine);
        SVcode = sLine;
        char temp[1000000];
        ifstream file;
        stringstream ss;
        string value;
        string SVfilename = SVcode;
        string SVpath = "D:\\bys\\"+SVfilename;
        const char * f = SVpath.c_str();
        file.open(f);
        while(!file.eof())
        {
         file.getline(temp,1000000);                
        }
         ss << temp;
         ss >> value;
         string lastchar = value.substr(0,10);
    //     cout << lastchar << endl;
         ofstream myfile;
         string filename = lastchar+".txt";
         string filepath = "D:\\bys\\"+filename;
         const char * c = filepath.c_str();
         myfile.open (c);
         myfile << lastchar;
         cout<<lastchar<<endl;
         myfile.close();
         system("PAUSE");
         return EXIT_SUCCESS;
         
    }
    here with i have enclosed my file where i am pick my last ten chars kindly guide me...

    thanks and regards,

    Robert.J
    Attached Files
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    If the line is null, then there are no characters to get. Which raises the question, if the last line has 9 characters or less do you want to ignore this condition also?

    You may just need to do a value.size() and only work with lines where size is 10 or more.

    Comment

    Working...