Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to get data from text file in C++?

  1. Jan 24, 2017 #1
    Hi Group,

    I am trying to get data from text file, I hope someone can suggest me how to do?

    I also have this code which can read data, but i do not know how to scan every single line to get any information I want.

    Please help, Thank you very much.

    Here is my code:
    Code (Text):

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <boost/lexical_cast.hpp> // convert number to string
    using namespace std;


    int main ()
    {
           string input  = "test1.txt";

           string line;

           ifstream fin(input.c_str());

           if (fin.is_open())
           {
    /*           while ( getline (fin, line) )
               {
                   cout << line << '\n';
               }*/
               while (! fin.eof() )
               {
                   getline (fin,line);
                   cout << line << endl;
               }

               fin.close();
           }

           else cout << "System can not read the input file, check it again.";
       }

       cout << "Done!" << endl;
        return 0;
    }
     

    and the file is attached:

    #cP2016 10 12 0 0 0.00000000 96 ORBIT IGb08 HLM IGS
    ## 1918 259200.00000000 900.00000000 57673 0.0000000000000
    + 32 G01G02G03G04G05G06G07G08G09G10G11G12G13G14G15G16G17
    + G18G19G20G21G22G23G24G25G26G27G28G29G30G31G32 0 0
    + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    ++ 2 2 2 0 2 2 2 2 2 2 2 3 2 2 2 2 2
    ++ 2 3 2 2 2 2 2 2 2 2 3 3 2 2 2 0 0
    ++ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    ++ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    ++ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    %c G cc GPS ccc cccc cccc cccc cccc ccccc ccccc ccccc ccccc
    %c cc cc ccc ccc cccc cccc cccc cccc ccccc ccccc ccccc ccccc
    %f 1.2500000 1.025000000 0.00000000000 0.000000000000000
    %f 0.0000000 0.000000000 0.00000000000 0.000000000000000
    %i 0 0 0 0 0 0 0 0 0
    %i 0 0 0 0 0 0 0 0 0
    /* RAPID ORBIT COMBINATION FROM WEIGHTED AVERAGE OF:
    /* cod emr esa gfz jpl ngs sio usn whu
    /* REFERENCED TO IGS TIME (IGST) AND TO WEIGHTED MEAN POLE:
    /* PCV:IGS08_1915 OL/AL:FES2004 NONE Y ORB:CMB CLK:CMB
    * 2016 10 12 0 0 0.00000000
    PG01 -13708.647442 10773.249185 19862.568314 36.889073 5 6 6 115
    PG02 13719.486776 13609.232181 -17921.238464 541.110472 10 9 8 125
    PG03 -23066.347494 13143.568797 580.431793 -92.017995 7 6 8 141[/code]
     

    Attached Files:

    Last edited: Jan 24, 2017
  2. jcsd
  3. Jan 24, 2017 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    You can put code in [code]...[/code] tags to keep indentation and other formatting.

    You want to work with "line"? You can look for whitespace in its characters and split by that. It is ugly, but possible and you can hide it in some functions that you don't have to touch again later.
    There are also tools that can do that for you and split it in separate strings.

    There are formats that would be easier to read in.
     
  4. Jan 24, 2017 #3
    @mfb : I am going to scan every single line, such as: PG01 -13708.647442 10773.249185 19862.568314 36.889073 5 6 6 115, They will separate by "space". Any tool can help me to get this done? Thank you.
     
  5. Jan 24, 2017 #4

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    How about excel "Text to columns" on the "Data" tab (for selected rows, of course) :smile: ?
     
  6. Jan 24, 2017 #5
    Looks like a CSV with spaces in place of commas.

    Here is code that will load a text file into either a buffer or a vector for you.
    Code (C):

    char * loadToMemory(const char * p_filename){
        FILE * f = fopen(p_filename, "rb");
        if (!f) return NULL;
        if (fseek(f, 0, SEEK_END)){
            fclose(f);
            return NULL;
        }
        long siz;
     
        siz = ftell(f);
        if (siz < 0){
            fclose(f);
            return NULL;
        }
        if (fseek(f, 0, SEEK_SET)){
            fclose(f);
            return NULL;
        }

        char * result = static_cast<char*>(malloc((siz + 1) * sizeof(char)));
        if (!result){  //Out of memory?
            fclose(f);
            return NULL;
        }
        if (static_cast<size_t>(siz) != fread(result, sizeof(char), siz, f)){
            free(result);
            fclose(f);
            return NULL;
        }
        fclose(f);
        result[siz] = '\0';
        return result;
    }

    bool loadToVector(const char * p_filename, std::vector<std::string> & p_vector) {
        if (char * c_result = loadToMemory(p_filename)){
            if (!*c_result) return false;  //Empty string
            char * stringStart = c_result;
            char * stringEnd = stringStart;
            while(*stringEnd){
                if (*stringEnd == '\n'){
                    p_vector.push_back(std::string(stringStart, stringEnd - stringStart));
                    stringStart = stringEnd + 1;
                }
                ++stringEnd;
            }
            p_vector.push_back(std::string(stringStart));
            free(c_result);
            return true;
        }
        return false;
    }
     
  7. Jan 26, 2017 #6
    Thank you very much, it's really worked.
     
  8. Jan 26, 2017 #7
    No problem, as you go along in your career, you'll end up with modules you just bring with you everywhere.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: How to get data from text file in C++?
  1. Input from text file (Replies: 13)

Loading...