Итак, я пытаюсь прочитать из файла чисел, чтобы создать матрицу (размеры указаны пользователем). Проблема в том, что всякий раз, когда я пытаюсь сделать вторую матрицу из файла, указатель файла сбрасывается и заканчивается тем, что снимает одну и ту же матрицу одновременно. У меня есть плавник, переданный по ссылке в моей функции, потому что наш профессор сказал, что он исправит это, но он не остановил указатель от сброса.Как остановить «указатель файла» от возврата к началу файла C++?
Вот мои функции:
// creates a matrix from a file
vector<int> filefill(ifstream &fin, string file, int rowsize, int columnsize) {
fin.open(file.c_str());
vector<int> matrix;
int number;
int i=0;
while((fin >> number) && i < rowsize*columnsize) {
matrix.push_back(number);
i++;
}
fin.close();
return matrix;
}
// writes a matrix onto a file
void fileout(string file, vector<int> matrix) {
ofstream fout;
fout.open(file.c_str());
for(int i=0; i < matrix.size(); i++)
fout << matrix[i] << endl;
fout.close();
}
А вот их главное:
vector<int> matrixA;
vector<int> matrixB;
cout << "Would you like to fill the matrices from a file?(Y/N) ";
string answer;
cin >> answer;
if (answer == "Y" || answer == "y") {
cout << "Please enter file name: ";
string file;
cin >> file;
ifstream fin;
matrixA = filefill(fin, file, Arow, Bcolumn);
matrixB = filefill(fin, file, Arow, Bcolumn);
vector<int>matrixC= addmat(matrixA, matrixB);
cout << endl << "Type in (1) to output to file OR" << endl << "Type in (2) to output to console: ";
string output;
cin >> output;
if(output == "1") {
cout << "Enter the file name you wish to output to: ";
string filo;
cin >> filo;
fileout(filo,matrixA);
fileout(filo,matrixB);
fileout(filo,matrixC);
}
не открывать и закрывать файл для каждой матрицы? – immibis
Откройте файл перед '= ... матрица а 'и закройте его после того, как' matrixB = ... ' –