how to read an extensive file without getting memory exception?











up vote
-1
down vote

favorite












I have a constructor for generate a Sparse Matrix after read a file.
The method works correctly if the sparse matrix resultant not are big.
Nevertheless, I want to read a big file (arround at 500.000 lines).
How to read an extensive file without getting memory exception?
The matrix it's declarate of this form: vector<vector<int>> matrix;



MatriuSparse::MatriuSparse(const string nomFitxer) {
fstream fitxer;
fitxer.open(nomFitxer);

int m_col;
int m_row;
int dimension = m_Nlinies;
while (!fitxer.eof())
{
fitxer >> m_row >> m_col;
if (dimension <= m_row|| dimension <= m_col) {
if (m_col < m_row) {
dimension = m_row;
dimension++;
}
if (m_row < m_col) {
dimension = m_col;
dimension++;
}
matrix.resize(dimension);
for (int i = 0; i < dimension; i++) {
matrix[i].resize(dimension);
}

matrix[m_row][m_col] = 1;
}
matrix[m_row][m_col] = 1;
}
m_Nlinies = dimension;
m_Ncol = m_Nlinies;
fitxer.close();
}









share|improve this question






















  • The problem is not the code that reads the file. The problem is that your matrix type stores all values of the matrix (even zeros). For large matrices, for example, 500'000 rows and columns, storing all values takes about 233 GiB. You may want to use a sparse matrix type to hold your matrix.
    – pschill
    Nov 11 at 0:59










  • How can I get it to not save the 0 values?
    – desirée rodríguez
    Nov 11 at 1:14






  • 2




    You use the term sparse matrix, but you do not seem to understand what one is. Start your research by familiarizing yourself. Unrelated Why is iostream::eof inside a loop condition considered wrong?
    – user4581301
    Nov 11 at 2:09















up vote
-1
down vote

favorite












I have a constructor for generate a Sparse Matrix after read a file.
The method works correctly if the sparse matrix resultant not are big.
Nevertheless, I want to read a big file (arround at 500.000 lines).
How to read an extensive file without getting memory exception?
The matrix it's declarate of this form: vector<vector<int>> matrix;



MatriuSparse::MatriuSparse(const string nomFitxer) {
fstream fitxer;
fitxer.open(nomFitxer);

int m_col;
int m_row;
int dimension = m_Nlinies;
while (!fitxer.eof())
{
fitxer >> m_row >> m_col;
if (dimension <= m_row|| dimension <= m_col) {
if (m_col < m_row) {
dimension = m_row;
dimension++;
}
if (m_row < m_col) {
dimension = m_col;
dimension++;
}
matrix.resize(dimension);
for (int i = 0; i < dimension; i++) {
matrix[i].resize(dimension);
}

matrix[m_row][m_col] = 1;
}
matrix[m_row][m_col] = 1;
}
m_Nlinies = dimension;
m_Ncol = m_Nlinies;
fitxer.close();
}









share|improve this question






















  • The problem is not the code that reads the file. The problem is that your matrix type stores all values of the matrix (even zeros). For large matrices, for example, 500'000 rows and columns, storing all values takes about 233 GiB. You may want to use a sparse matrix type to hold your matrix.
    – pschill
    Nov 11 at 0:59










  • How can I get it to not save the 0 values?
    – desirée rodríguez
    Nov 11 at 1:14






  • 2




    You use the term sparse matrix, but you do not seem to understand what one is. Start your research by familiarizing yourself. Unrelated Why is iostream::eof inside a loop condition considered wrong?
    – user4581301
    Nov 11 at 2:09













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a constructor for generate a Sparse Matrix after read a file.
The method works correctly if the sparse matrix resultant not are big.
Nevertheless, I want to read a big file (arround at 500.000 lines).
How to read an extensive file without getting memory exception?
The matrix it's declarate of this form: vector<vector<int>> matrix;



MatriuSparse::MatriuSparse(const string nomFitxer) {
fstream fitxer;
fitxer.open(nomFitxer);

int m_col;
int m_row;
int dimension = m_Nlinies;
while (!fitxer.eof())
{
fitxer >> m_row >> m_col;
if (dimension <= m_row|| dimension <= m_col) {
if (m_col < m_row) {
dimension = m_row;
dimension++;
}
if (m_row < m_col) {
dimension = m_col;
dimension++;
}
matrix.resize(dimension);
for (int i = 0; i < dimension; i++) {
matrix[i].resize(dimension);
}

matrix[m_row][m_col] = 1;
}
matrix[m_row][m_col] = 1;
}
m_Nlinies = dimension;
m_Ncol = m_Nlinies;
fitxer.close();
}









share|improve this question













I have a constructor for generate a Sparse Matrix after read a file.
The method works correctly if the sparse matrix resultant not are big.
Nevertheless, I want to read a big file (arround at 500.000 lines).
How to read an extensive file without getting memory exception?
The matrix it's declarate of this form: vector<vector<int>> matrix;



MatriuSparse::MatriuSparse(const string nomFitxer) {
fstream fitxer;
fitxer.open(nomFitxer);

int m_col;
int m_row;
int dimension = m_Nlinies;
while (!fitxer.eof())
{
fitxer >> m_row >> m_col;
if (dimension <= m_row|| dimension <= m_col) {
if (m_col < m_row) {
dimension = m_row;
dimension++;
}
if (m_row < m_col) {
dimension = m_col;
dimension++;
}
matrix.resize(dimension);
for (int i = 0; i < dimension; i++) {
matrix[i].resize(dimension);
}

matrix[m_row][m_col] = 1;
}
matrix[m_row][m_col] = 1;
}
m_Nlinies = dimension;
m_Ncol = m_Nlinies;
fitxer.close();
}






c++ vector sparse-matrix adjacency-matrix






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 0:47









desirée rodríguez

23




23












  • The problem is not the code that reads the file. The problem is that your matrix type stores all values of the matrix (even zeros). For large matrices, for example, 500'000 rows and columns, storing all values takes about 233 GiB. You may want to use a sparse matrix type to hold your matrix.
    – pschill
    Nov 11 at 0:59










  • How can I get it to not save the 0 values?
    – desirée rodríguez
    Nov 11 at 1:14






  • 2




    You use the term sparse matrix, but you do not seem to understand what one is. Start your research by familiarizing yourself. Unrelated Why is iostream::eof inside a loop condition considered wrong?
    – user4581301
    Nov 11 at 2:09


















  • The problem is not the code that reads the file. The problem is that your matrix type stores all values of the matrix (even zeros). For large matrices, for example, 500'000 rows and columns, storing all values takes about 233 GiB. You may want to use a sparse matrix type to hold your matrix.
    – pschill
    Nov 11 at 0:59










  • How can I get it to not save the 0 values?
    – desirée rodríguez
    Nov 11 at 1:14






  • 2




    You use the term sparse matrix, but you do not seem to understand what one is. Start your research by familiarizing yourself. Unrelated Why is iostream::eof inside a loop condition considered wrong?
    – user4581301
    Nov 11 at 2:09
















The problem is not the code that reads the file. The problem is that your matrix type stores all values of the matrix (even zeros). For large matrices, for example, 500'000 rows and columns, storing all values takes about 233 GiB. You may want to use a sparse matrix type to hold your matrix.
– pschill
Nov 11 at 0:59




The problem is not the code that reads the file. The problem is that your matrix type stores all values of the matrix (even zeros). For large matrices, for example, 500'000 rows and columns, storing all values takes about 233 GiB. You may want to use a sparse matrix type to hold your matrix.
– pschill
Nov 11 at 0:59












How can I get it to not save the 0 values?
– desirée rodríguez
Nov 11 at 1:14




How can I get it to not save the 0 values?
– desirée rodríguez
Nov 11 at 1:14




2




2




You use the term sparse matrix, but you do not seem to understand what one is. Start your research by familiarizing yourself. Unrelated Why is iostream::eof inside a loop condition considered wrong?
– user4581301
Nov 11 at 2:09




You use the term sparse matrix, but you do not seem to understand what one is. Start your research by familiarizing yourself. Unrelated Why is iostream::eof inside a loop condition considered wrong?
– user4581301
Nov 11 at 2:09

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244846%2fhow-to-read-an-extensive-file-without-getting-memory-exception%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244846%2fhow-to-read-an-extensive-file-without-getting-memory-exception%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma