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;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please use code tags when posting code.

Also, please try to simplify your code before showing us. Surely it doesn't take 125 lines to demonstrate this error. I bet you can get it down to 20 :)

Hi friends,

I would like to know if there are problems of memory or something like this in my function. When I use small length of each line seems to be fine, but when I increase the number of each record, I find problems.

Someone can Help me?

Thanks!

Sads

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;
}

What kind of problems? Hardcode some input, show us the current output, and tell us the expected output.

Hi David,

As example, I'm trying to use the file1.txt as input:

150 0101010101010101010101
151 0101010101010100110111
154 4534543020000111111111

But, If I increase the second piece of each line "0101010101010101010101" (length = 22) to 50.000, the output is " ". Nothing happens ...

Probably is problem to use memory.

Can you help me?

Thanks!

Another problem is this message:

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Hello,

I compiled the code using icc and I found this error message:

Funcao getANIPED()
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr

How can I fix the problem?

Hi friends,

Problem solved!

I just tested if the string lineAllCode.size() > 0 before getline. Now is fine!

Cheers,

Sads

This question has already been answered. Start a new discussion instead.