Hi friends,
I'm newer in C++ and I'm trying write a code using ifstream, vector and substr.
A) I have 2 problems:
A.1) When I read files with small number of lines OK, but if I increase the number of lines, PROBLEM.
As example:
file1.txt (Work well with 9 lines, but NOT with 50.000 lines):
1 9 0 1
2 0 0 1
3 0 0 2
4 0 0 1
5 0 0 1
6 0 0 2
7 0 0 2
8 0 0 2
9 0 0 2
A.2) When I was using files with small length in each line was OK, but in same cases when the length is big, I found some problems.
As example:
File2.txt (Work well with the second SUBSET "0101010101" been small, but NOT with length > 5.000)
150 0101010101
151 0101010101
154 4534543020
B) The second problem is that the system is not stopping to answer the question:
cout << "\nEnter the name of second text file: ";
The system prints the results of first question (First file1.txt) but not give the opportunity to fill ""File2.txt". How can I force this stop?
Thanks!
Sads
CODE:
//***********************************************************************************
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
void printV(vector<string>& a);
void getFileP(ifstream &myfileP, vector<string> &vetorIDP, vector<string> &vetorIDF, vector<string> &vetorIDM, vector<string> &vetorIDOpc, string &lineIDP);
void getSC(int numberSC,ifstream &myfile, vector<string> &vS,string &line, int &dimCodAni);
int main()
{
vector<string> vetorAllCode;
string lineAllCode;
int numberSC= 0;
int dimCodAni = 0;
string lineIDP;
vector<string> vetorIDP;
vector<string> vetorIDF;
vector<string> vetorIDM;
vector<string> vetorIDOpc;
char nomeArqP[256];
ifstream myfileP;
cout << "\nEnter the name of first file: ";
cin.get (nomeArqP,256);
myfileP.open (nomeArqP); // open file
getFileP(myfileP,vetorIDP,vetorIDF,vetorIDM,vetorIDOpc,lineIDP);
cout << "\nPrinting ID : ";
printV(vetorIDP);
cout << "\nPrinting F : ";
printV(vetorIDF);
cout << "\nPrinting M : ";
printV(vetorIDM);
cout << "\nPrinting Opc : ";
printV(vetorIDOpc);
myfileP.close();
char nomeArq[256];
ifstream myfile;
ifstream myfileAni;
cout << "\nEnter the name of second text file: ";
cin.get (nomeArq,256);
myfile.open (nomeArq);
myfileAni.open(nomeArq);
cout << "\nEnter the number of SINGLE CODES: " << endl;
cin >> numberSC;
cout << "\nEnter the length of ID: " << endl;
cin >> dimCodAni;
getSC(numberSC,myfile,vetorAllCode,lineAllCode,dimCodAni);
printV(vetorAllCode);
myfile.close();
myfileAni.close();
return 0;
return 0;
}
//************************************************************************
//************************ INT MAIN **************************************
void printV(vector<string>& a)
{
std::cout << "\nPrinting VECTOR:" << endl;
for(int i=0; i<a.size(); ++i)
std::cout << a[i] << "\n";
}
void getFileP(ifstream &myfileIDP, vector<string> &vetorIDP, vector<string> &vetorIDF, vector<string> &vetorIDM, vector<string> &vetorIDOpc, string &lineIDP)
{
string substLineIDP = "";
string substLineIDPOpc = "";
string substLineIDPF = "";
string substLineIDPM = "";
int countLineIDP = 0;
cout << "\nFunction getIDP()" << endl;
if ( myfileIDP.is_open() )
{
while ( ! myfileIDP.eof() )
{
getline(myfileIDP,lineIDP);
substLineIDP = lineIDP.substr(0,17);
vetorIDP.push_back(substLineIDP);
substLineIDPF = lineIDP.substr(18,17);
vetorIDF.push_back(substLineIDPF);
substLineIDPM = lineIDP.substr(35,13);
vetorIDM.push_back(substLineIDPM);
substLineIDPOpc = lineIDP.substr(48,1);
vetorIDM.push_back(substLineIDPM);
countLineIDP++;
}
}
else
{
cout << "Unable to open file." << endl;
}
}
void getSC( int numberCODE,ifstream &myfile, vector<string> &vetorAllCode, string &lineAllCode, int &dimCodAni)
{
string substLine;
int countLineSC = 0;
cout << "The length of SingleCODES is " << numberCODE << " ... " << endl;
if ( myfile.is_open() )
{
while ( ! myfile.eof() )
{
getline(myfile,lineAllCode);
substLine = lineAllCode.substr(dimCodAni,numberCODE);
vetorAllCode.push_back(substLine);
countLineSC++;
}
}
else
{
cout << "Unable to open file." << endl;
}
cout << "\nLine Counts: " << countLineSC << endl;
cout << "\nNumber of SingleCODES: " << substLine.length() << endl;
}