I'm reading a 2 to 4 gb .txt file and then I manipulate some of the data and want to save my struct vector as a tab delimited .txt file. I read some of the other questions but still not clear to me how I'm going to do it in my program.
So my question is: How to save the Input
vector results as a tab delimited .txt file?
Below is my code:
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
using namespace std;
struct Input_Spec {
std::string Data;
std::string Ativo;
int Buy_Sell;
double Sequencia;
double Id;
int Event;
std::string Hr_Priority;
double Priority;
double Price;
double Qtd_Total;
double Qtd_Traded;
std::string Data_Order;
std::string Data_Time_Order;
std::string State_Of_Order;
std::string Condition_Of_Order;
double Broker;
};
void split(const std::string &s, char delim, std::string elems[])
{
std::stringstream ss(s);
std::string item;
long i = 0;
while (std::getline(ss, item, delim))
{
elems[i++] = item;
}
}
int main()
{
ifstream infile1("C:\\Teste\\Teste.txt");
ofstream output("output.txt");
string word;
string columns[16];
string line;
int row=0;
long c=0;
long filescol=0;
for (int i = 0; std::getline(infile1, word); ++i)
{
row++;
}
//cout<<row;
infile1.close();
ifstream infile("C:\\Teste\\Teste.txt");
vector<Input_Spec> Input(row);
while( getline(infile, line))
{
split(line,';', columns);
if (columns[0]!="")
{
Input[filescol].Data =columns[0];
Input[filescol].Ativo =columns[1];
Input[filescol].Buy_Sell = stoi(columns[2]);
Input[filescol].Sequencia = stod(columns[3]);
Input[filescol].Id = stod(columns[4]);
Input[filescol].Event = stoi(columns[5]);
Input[filescol].Hr_Priority = columns[6];
Input[filescol].Priority = stod(columns[7]);
Input[filescol].Price = stod(columns[8]);
Input[filescol].Qtd_Total = stod(columns[9]);
Input[filescol].Qtd_Traded = stod(columns[10]);
Input[filescol].Data_Order = columns[11];
Input[filescol].Data_Time_Order = columns[12];
Input[filescol].State_Of_Order = columns[13];
Input[filescol].Condition_Of_Order = columns[14];
Input[filescol].Broker = stod(columns[15]);
filescol++;
c++;
}
if (c>(999))
{
break;
infile.close();
return 0;
}
}
infile.close();
return 0;
}
Here's a fragment:
ofstream output ("output.txt");
output << Input[filescol].Data << '\t';
output << Input[filescol].Ativo << '\t';
output << Input[filescol].Buy_Sell << '\t';
//...
output << Input[filescol].Broker << '\n';
Is this what you are talking about?
At first, please let me give you some hints on your code: You don't need to count all lines of the input file and you don't need to reserve memory for all items in your vector
beforehand. You can use the push_back()
function to add items to the vector
. This saves you one iteration on the input file.
I put this improvement into the main()
function below. I also expanded a little bit on the answer by @ThomasMatthews, so that you can see how to loop over the vector
in order to save the data in it:
int main()
{
ifstream infile("C:\\Teste\\Teste.txt");
ofstream output("C:\\Teste\\output.txt");
string line;
string columns[16];
vector<Input_Spec> Input;
Input_Spec oneInput;
while (getline(infile, line))
{
split(line, ';', columns);
if (!columns[0].empty())
{
oneInput.Data = columns[0];
oneInput.Ativo = columns[1];
oneInput.Buy_Sell = stoi(columns[2]);
oneInput.Sequencia = stod(columns[3]);
oneInput.Id = stod(columns[4]);
oneInput.Event = stoi(columns[5]);
oneInput.Hr_Priority = columns[6];
oneInput.Priority = stod(columns[7]);
oneInput.Price = stod(columns[8]);
oneInput.Qtd_Total = stod(columns[9]);
oneInput.Qtd_Traded = stod(columns[10]);
oneInput.Data_Order = columns[11];
oneInput.Data_Time_Order = columns[12];
oneInput.State_Of_Order = columns[13];
oneInput.Condition_Of_Order = columns[14];
oneInput.Broker = stod(columns[15]);
Input.push_back(oneInput);
}
}
// ----------------------
// Modify your data here.
// ----------------------
for(vector<Input_Spec>::const_iterator it = Input.begin(); it != Input.end(); it++)
{
output << it->Data << '\t';
output << it->Ativo << '\t';
output << it->Buy_Sell << '\t';
output << it->Sequencia << '\t';
output << it->Id << '\t';
output << it->Event << '\t';
output << it->Hr_Priority << '\t';
output << it->Priority << '\t';
output << it->Price << '\t';
output << it->Qtd_Total << '\t';
output << it->Qtd_Traded << '\t';
output << it->Data_Order << '\t';
output << it->Data_Time_Order << '\t';
output << it->State_Of_Order << '\t';
output << it->Condition_Of_Order << '\t';
output << it->Broker << '\n';
}
output.close();
infile.close();
return 0;
}
I guess that more improvements could be done to your code, but this should give you a good start.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.