K
K
Kirill Zhuravlev2019-05-04 17:27:48
C++ / C#
Kirill Zhuravlev, 2019-05-04 17:27:48

How to jump to a specific matrix element from a text file?

There is a matrix written in a cvs file. The symbol ';' is used as a separator.

29;89;77;56;7;40;100;29;57;60;
49;4;44;51;86;37;25;54;51;61;
5;93;55;15;37;92;68;54;28;48;
83;48;16;50;93;20;64;76;78;42;
96;1;100;57;45;53;63;97;22;47;
84;25;76;61;31;52;53;90;46;5;
17;43;23;49;84;34;26;1;63;52;
68;54;48;96;83;37;34;37;96;92;
27;70;58;41;94;48;14;89;99;86;
40;21;66;98;40;78;72;27;72;76;

It is necessary to go to a certain element in the matrix (later this element will need to be replaced) and output it. The numbers are random and do not have to be two digits.
Stuck on this moment
cpp_int getElementInFile(fstream &file, unsigned int size, size_t i, size_t j)
{
  cpp_int toReturn;
  cpp_int temp;
  string test;
  int sizeOfElement = (sizeof(cpp_int) + sizeof(';'));
  streampos saveCurrentCursor = file.tellg();//сохраняем позицию курсора
  file.seekg(0, ios::beg);//переходим на начало файла
  file.seekg(((i-1)*size+(j-1))*sizeOfElement, ios::cur);
  file >> test;
  cout << test;
  return toReturn;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vanyamba-electronics, 2019-05-04
@zhuravlevkk

#include <iostream>

using namespace std;

int main() 
{
    unsigned int col = 3;
    unsigned int row = 1;
    char ch;
    unsigned int cell = 0;
    
    // Skip rows less than row
    for (unsigned int nRow = 0; nRow < row; ++nRow)
        do {
            cin.get(ch);
        } while (ch != '\n');
    // Skip columns less than col
    for (unsigned int nCol = 0; nCol < col; ++nCol)
        do {
            cin.get(ch);
        } while (ch != ';');
    // Read number
    do {
        cin.get(ch);
        if (isdigit(ch))
            cell = cell * 10 + ch - '0';
    } while(ch != ';');
    cout << cell << endl;
    return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question