C ++ Как изменить содержимое текстового файла? - PullRequest
1 голос
/ 09 октября 2019

Помимо моего задания на C ++, мне сказали создать программу, которая может выводить ромб на экран с указанным пользователем символом и количеством строк. Результаты затем должны быть сохранены в текстовом файле, который я заполнил. Затем меня попросили найти способ изменить существующий текстовый файл с помощью полученных результатов алмазов, чтобы можно было изменять символы алмазов. Мне сказали использовать прямой двоичный доступ для поиска и изменения символов, хранящихся в файле, содержащем алмазы, но я не знаю, как это сделать.

Любая помощь приветствуется:)

#include <iostream>
#include <fstream>
using namespace std;

void diamond(int n, char c);
int main()
{
int a, b, e, d = 1;
char c;

cout << "Enter a single character:" << endl;
cin >> c;

cout << "Enter amount of rows:" << endl;
cin >> a;

//File output to text file code is shown below
//After program is run, the variable info is stored in output.txt

ofstream file; //Output stream
file.open("output.txt", ofstream::app); //Opened in append mode to prevent it from overwriting the files contents each time

if (!file) {
    file << "Unable to open file." << endl;
    return 1;
}

if (file.is_open()) {
    file << "The character that was entered is ";
    file << c;
    file << " ";
    file << "and the number of rows that were specified is ";
    file << a;
    file << ".\n\n";
}

d = a - 1;

for (e = 1; e <= a; e++)

{
    for (b = 1; b <= d; b++)
        file << " ";
    d--;

    for (b = 1; b <= 2 * e - 1; b++)
        file << c;

    file << "\n";
}

d = 1;
for (e = 1; e <= a; e++)
{

    for (b = 1; b <= d; b++)
        file << " ";
    d++;

    for (b = 1; b <= 2 * (a - e) - 1; b++)
        file << c;
    file << "\n";

}

file.close();
std::cin.get(); //Prints variables and diamonds to file, please check output.txt for results

return 0;

}
...