Top.Mail.Ru
Ответы

Удаление строк в файле C++

Делаю что-то похожее на небольшую базу данных, и пробую реализовать удаление строк через перезапись, а после последующие чтение строк, из одного файла - в другой.

но уже на этой стадии выдает ошибку

12345678910111213141516171819202122232425262728293031
 ofstream file_Delete; 
file_Delete.open(way_to_file_delete, ofstream::app); 
 
ifstream file; 
file.open(way_to_file); 
 
while (!file.eof()) { 
				str_file = " "; 
				getline(file, str_file); 
				file_Delete << str_file; 
} 
 
file_Delete.close(); 
file.close(); 
 
ifstream file(way_to_file, ios::out);// Удаление данных из исходного файла 
file.close(); 
 
ofstream file_Delete; 
file_Delete.open(way_to_file_delete, ofstream::app);  
fstream file; 
file.open(way_to_file, ofstream::app); 
 
while (!file_Delete.eof()) { 
				str_file = " "; 
				getline(file_Delete, str_file); 
				file << str_file; 
} 
file_Delete.close(); 
file.close(); 
break; 

что можно сделать?

По дате
По рейтингу
Аватар пользователя
Новичок
6мес

Странно, что у тебя не выдаёт ошибку на всё другое, например на то, что несколько объявлений переменных.

1234567891011121314151617181920212223242526272829303132333435
 #include <iostream> 
#include <fstream> 
 
using namespace std; 
 
int main(void) { 
    string way_to_file_delet = "/path/to/file"; 
    string way_to_file = "/path/to/file"; 
    ofstream file_Delete;  
    file_Delete = ofstream(way_to_file_delet, ios::app);
 
    ifstream file;  
    file = ifstream(way_to_file);
 
    string str_file = ""; 
    while (!getline(file, str_file)) {  
        file_Delete << str_file;  
    }  
 
    file_Delete.close();  
    file.close();
 
    ofstream file2(way_to_file, ofstream::app);  
    ifstream file_Delete2(way_to_file_delet); 
 
    while (!getline(file_Delete2, str_file)) {  
        file2 << str_file;  
    }  
     
    file_Delete2.close();  
    file2.close();  
     
    return 0; 
} 
 

Исправно компилируется, но работает ли?

Аватар пользователя
Высший разум
6мес
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
 #include <fstream> 
#include <iostream> 
#include <string> 
#include <vector>

using namespace std;

static vector<string> load(const string& path) { 
    vector<string> content; 
    ifstream inp(path); 
    if (!inp.is_open()) return content; 
    string line; 
    while (getline(inp, line)) content.push_back(line); 
    inp.close(); 
    return content; 
}

static bool save(const string& path, const vector<string>& content) { 
    ofstream out(path); 
    if (!out.is_open()) return false; 
    for (const auto& line : content) out << line << '\n'; 
    out.close(); 
    return true; 
}

static vector<string> truncate(const vector<string>& content, const size_t max_length) {
    vector<string> new_content; 
    for (const auto& line : content) { 
        if (line.length() < max_length) { 
            new_content.push_back(line); 
        } 
    } 
    return new_content; 
}

int main() { 
    system("chcp 1251 > nul"); 
    const string first{ "first.txt" }; 
    const auto content = load(first); 
    const auto new_content = truncate(content, 32); 
    if (new_content.empty()) puts("Not found!"); 
    else { 
        const string second{ "second.txt" }; 
        puts(save(second, new_content) ? "Success!" : "Failure!"); 
    } 
    system("pause > nul"); 
}