Я учу C ++.
Я пытаюсь преобразовать текстовый файл, как этот:
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
...
В файл, подобный этому:
int grid[20][30] =
{
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
Обе части текстового файла являются только примером, чтобы показать вам, что я пытаюсь сделать. Первый текстовый файл не генерирует второй текстовый файл.
Я записал этот код:
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[])
{
if ((argc == 1) || (argc != 5)) {
cout << "Usage: format rows columns input_file output_file\n";
return 0;
}
// Number of rows in the image.
int rows = atoi(argv[1]);
// Number of columns in the image.
int columns = atoi(argv[2]);
// Character read from the input file.
char ch;
// Counter to create arrays of "columns" elements.
int colCount = 0;
// Counter for the number of rows added to the grid.
int rowCount = 0;
// Output file.
ofstream fout;
fout.open(argv[4], ios::out);
// Write the header to output file.
fout << "int grid[" << rows << "][" << columns << "] = {";
// Read input file.
fstream fin(argv[3], fstream::in);
while (fin >> noskipws >> ch)
{
if (colCount == 0)
fout << "\n{";
if ((!isspace(ch)) && ((ch == '1') || (ch == '0') || (ch == ','))) {
fout << ch;
colCount++;
}
if (colCount == columns) {
colCount = 0;
rowCount++;
if (rowCount != rows)
fout << "},";
}
}
fout << "}};\n";
fout.close();
return 0;
}
Но, похоже, он никогда не входит в основной цикл (while (fin >> noskipws >> ch)
). Я получаю этот вывод в текстовом файле:
int grid[365][484] = {}};
Я компилирую с g ++ в Linux (Ubuntu) с помощью следующей командной строки:
g++ FormatMatrix.cpp -o format
Что я делаю не так?