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

Click For Summary
The discussion focuses on how to read data from a text file in C++. A user seeks assistance in scanning each line of the file to extract specific information, sharing their initial code that reads lines but struggles with parsing. Suggestions include using whitespace to split lines into separate strings and utilizing tools like Excel's "Text to Columns" feature for easier data manipulation. Additional code snippets are provided to load the file into memory or a vector, facilitating further processing. The conversation concludes with acknowledgment of the effectiveness of the provided solutions.
Nate Duong
Messages
125
Reaction score
4
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:
#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]
 

Attachments

Last edited:
Technology news on Phys.org
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.
 
  • Like
Likes Nate Duong
mfb said:
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.
@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.
 
How about excel "Text to columns" on the "Data" tab (for selected rows, of course) :smile: ?
 
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.
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;
}
 
  • Like
Likes Nate Duong
newjerseyrunner said:
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.
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;
}
Thank you very much, it's really worked.
 
  • Like
Likes newjerseyrunner
No problem, as you go along in your career, you'll end up with modules you just bring with you everywhere.
 
  • Like
Likes BvU

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
6
Views
1K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
9K
  • · Replies 75 ·
3
Replies
75
Views
6K