• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Podmiana lini w pliku tekstowy w c++

Aruba Cloud VPS - 50% taniej przez 3 miesiące!
0 głosów
754 wizyt
pytanie zadane 26 lutego 2019 w C i C++ przez MrChick888 Obywatel (1,020 p.)

Jak z pliku save.txt który wygląda tak 

save_1
Piotr
100
Save_2
Kamil
90
save_1
Piotr
100

i tam gdzie są liczy chce usunąć tą linie i zapisać inną liczbę(punktów) nie naruszając reszty. Da się coś takiego zrobić?

tutaj całość kodu


#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
#include <windows.h>
#include <sstream>
using namespace std;
 
void main_menu(int& main_menu_navigator);
void menu_option1(string& player_name, int& credits, int& color, int& credits_played, int& random_number, fstream& save, string& game_name);
void conditions_color_option(string player_name, int& variable);
void conditions_credits_option(string player_name, int credits, int& credits_played);
void draw_option(int& random_number);
void conditions_result_option1(string player_name, int& credits, int credits_played, int color, int random_number, fstream& save, string& game_name);
void win_option1(string player_name, int& credits, int credits_played, int multiplier);
void lose_option1(string player_name, int& credits, int credits_played, string correct_color);
void p_c();
void save_option1(fstream& save, string& game_name, string player_name, int credits);
void reading_from_a_file(string Save[], string Nick[], int Credits[], int &selected_save, string& player_name, int& credits);
void menu_option2(string player_name, int& credits, int credits_played, int color, int random_number);
void conditions_result_option2(string player_name, int& credits, int credits_played, int color, int random_number);
void save_option2(fstream& save, string& game_name, string player_name, int credits);
 
int main()
{
    int main_menu_navigator, credits, color, credits_played = 0, random_number, selected_save;
    string player_name, game_name;
    fstream save;
    string Save[10], Nick[10];
    int Credits[10];
    while (1)
    {
        main_menu(main_menu_navigator);
        switch (main_menu_navigator)
        {
        case 1:
            menu_option1(player_name, credits, color, credits_played, random_number, save, game_name);
            break;
        case 2:
            reading_from_a_file(Save, Nick, Credits, selected_save, player_name, credits);
            menu_option2(player_name, credits, credits_played, color, random_number);
            break;
        case 3:
            //kod
        default: break;
        }
 
    }
    system("PAUSE");
}
void main_menu(int& main_menu_navigator)
{
    cout << "ROULETTE GAME" << endl;
    cout << "=============" << endl;
    cout << "<<MAIN MENU>>" << endl;
    cout << "=============" << endl;
    cout << "1.Start a new game." << endl;
    cout << "2.Coutinue the game." << endl;
    cout << "3.Rule section." << endl;
    cout << "4.END." << endl;
    cout << "Enter: ";
    if (!(cin >> main_menu_navigator))
    {
        cout << "You can't enter anything other than a number here! " << endl;
        cout << "Restart program and try again." << endl;
        system("PAUSE");
        exit(0);
    }
    system("cls");
    return;
}
void menu_option1(string& player_name, int& credits, int& color, int& credits_played, int& random_number, fstream& save, string& game_name)
{
    credits = 50;
    cout << "WELCOME IN C++ CASINO." << endl;
    cout << "Enter your name: ";
    cin >> player_name;
    cout << "Good luck and have fun " << player_name << "." << endl;
    cout << "Your credits: " << credits << endl;
    cout << "Let's start the game!" << endl;
    cout << player_name << " choose color : " << endl;
    cout << "1.RED" << endl;
    cout << "2.GREEN" << endl;
    cout << "3.BLACK" << endl;
    conditions_color_option(player_name, color);
    while (color < 1 || color>3)
    {
        system("cls");
        cout << "This option dosn't exits!" << endl;
        cout << player_name << " choose color : " << endl;
        cout << "1.RED" << endl;
        cout << "2.GREEN" << endl;
        cout << "3.BLUE" << endl;
        conditions_color_option(player_name, color);
    }
    cout << "How many credits do you want to play? ";
    conditions_color_option(player_name, credits_played);
    conditions_credits_option(player_name, credits, credits_played);
    draw_option(random_number);
    conditions_result_option1(player_name, credits, credits_played, color, random_number, save, game_name);
    return;
}
void conditions_color_option(string player_name, int& variable)
{
    if (!(cin >> variable))
    {
        cout << player_name << " you can't enter anything other than a number here! " << endl;
        cout << "Restart program and try again." << endl;
        system("PAUSE");
        exit(0);
    }
    return;
}
void conditions_credits_option(string player_name, int credits, int& credits_played)
{
    while (credits < credits_played || credits_played <= 0)//można lepiej na dwa przypadki
    {
        system("cls");
        cout << player_name << " you have only " << credits << " credits! You can't play for " << credits_played << "!" << endl;
        cout << "How many credits do you want to play? " << endl;
        conditions_color_option(player_name, credits_played);
    }
    return;
}
void draw_option(int& random_number)
{
    system("cls");
    cout << "The drawing machine begins the countdown." << endl;
    for (int i = 5; i > -2; i--)
    {
        Sleep(500);//1000 to 1 sekunda
        system("cls");
        cout << i;
    }
    system("cls");
    srand(time(NULL));
    random_number = rand() % 99 + 1;
    return;
}
void conditions_result_option1(string player_name, int& credits, int credits_played, int color, int random_number, fstream& save, string& game_name)
{
    if (random_number >= 1 && random_number <= 48)
    {
        if (color == 1)win_option1(player_name, credits, credits_played, 2);
        else lose_option1(player_name, credits, credits_played, "Red");
        save_option1(save, game_name, player_name, credits);
    }
    if (random_number >= 49 && random_number <= 51)
    {
        if (color == 2)win_option1(player_name, credits, credits_played, 14);
        else lose_option1(player_name, credits, credits_played, "Green");
        save_option1(save, game_name, player_name, credits);
    }
    if (random_number >= 52 && random_number <= 99)
    {
        if (color == 3)win_option1(player_name, credits, credits_played, 2);
        else lose_option1(player_name, credits, credits_played, "Black");
        save_option1(save, game_name, player_name, credits);
    }
    return;
}
void win_option1(string player_name, int& credits, int credits_played, int multiplier)
{
    cout << "YOU WON!, Well played " << player_name << "." << endl;
    cout << "You won " << credits_played * multiplier << "." << endl;
    credits += (credits_played * multiplier) - credits_played;
    cout << "Your credits: " << credits << endl;
    return;
}
void lose_option1(string player_name, int& credits, int credits_played, string correct_color)
{
    cout << "YOU LOST! Correct color is " << correct_color << " . You must try one more time " << player_name << "." << endl;
    credits -= credits_played;
    cout << "Your credits: " << credits << endl;
    if (credits == 0)
    {
        cout << "You lose this game :(" << endl;
        system("PAUSE");
        exit(0);
    }
    return;
}
void p_c()
{
    system("PAUSE");
    system("cls");
    return;
}
void save_option1(fstream& save, string& game_name, string player_name, int credits)
{
    save.open("save.txt", ios::in | ios::out | ios::app);
    if (save.good() == true)
    {
        cout << "Save you game. Enter game name: ";
        cin >> game_name;
        save << game_name << endl << player_name << endl << credits << endl;
    }
    else
    {
        cout << "Program can't find save file :(";
        system("PAUSE");
        exit(0);
    }
    save.close();
    p_c();
    return;
}
void reading_from_a_file(string Save[], string Nick[], int Credits[], int& selected_save, string& player_name, int& credits)
{
    cout << "WELCOME IN C++ CASINO AGAIN!" << endl;
    cout << "Your save's: " << endl;
    std::ifstream save("save.txt");
    int i = 0;
    while (getline(save >> ws, Save[i]) && getline(save, Nick[i]) && save >> Credits[i] && i < 3)
    {
        cout << "[" << i + 1 << "]" << Save[i] << endl;
        ++i;
    }
    cout << "Select which save do you want to play: ";
    cin >> selected_save;
 
    player_name = Nick[selected_save - 1];
    credits = Credits[selected_save - 1];
    return;
}
void menu_option2(string player_name, int& credits, int credits_played, int color, int random_number)
{
    system("cls");
    cout << "Good luck and have fun " << player_name << "." << endl;
    cout << "Your credits: " << credits << endl;
    cout << "Let's start the game!" << endl;
    cout << player_name << " choose color : " << endl;
    cout << "1.RED" << endl;
    cout << "2.GREEN" << endl;
    cout << "3.BLACK" << endl;
    conditions_color_option(player_name, color);
    while (color < 1 || color>3)
    {
        system("cls");
        cout << "This option dosn't exits!" << endl;
        cout << player_name << " choose color : " << endl;
        cout << "1.RED" << endl;
        cout << "2.GREEN" << endl;
        cout << "3.BLUE" << endl;
        conditions_color_option(player_name, color);
    }
    cout << "How many credits do you want to play? ";
    conditions_color_option(player_name, credits_played);
    conditions_credits_option(player_name, credits, credits_played);
    draw_option(random_number);
    conditions_result_option2(player_name, credits, credits_played, color, random_number);
    return;
}
void conditions_result_option2(string player_name, int& credits, int credits_played, int color, int random_number)
{
    if (random_number >= 1 && random_number <= 48)
    {
        if (color == 1)win_option1(player_name, credits, credits_played, 2);
        else lose_option1(player_name, credits, credits_played, "Red");
        //save_option2(save, game_name, player_name, credits);
    }
    if (random_number >= 49 && random_number <= 51)
    {
        if (color == 2)win_option1(player_name, credits, credits_played, 14);
        else lose_option1(player_name, credits, credits_played, "Green");
        //save_option2(save, game_name, player_name, credits);
    }
    if (random_number >= 52 && random_number <= 99)
    {
        if (color == 3)win_option1(player_name, credits, credits_played, 2);
        else lose_option1(player_name, credits, credits_played, "Black");
        //save_option2(save, game_name, player_name, credits);
    }
    return;
}
void save_option2(fstream& save, string game_name, string player_name, int credits)
{
    save.open("save.txt", ios::in | ios::out | ios::app);
    if (save.good() == true)
    {
        cout << player_name << " you are saving game on save -  " << game_name << "." << endl;
        //usunąc i napisać nową lnie
    }
    else
    {
        cout << "Program can't find save file :(";
        system("PAUSE");
        exit(0);
    }
    save.close();
    p_c();
    return;
}

chodzi o te dwie trzy funkcje na samym dole

komentarz 26 lutego 2019 przez gagyn Stary wyjadacz (11,050 p.)
edycja 26 lutego 2019 przez gagyn
Nienaruszając pliku nie da się nic zapisywać.

Zawsze w momencie otwarcia pliku do zapisu plik jest "czyszczony". Dlatego najpierw musisz wczytać z pliku, zmienić opowienie linijki, a na koniec z powrotem do pliku całość.

EDIT: No tak, zapomniałem o trybie czytania i zapisywania na raz. Mój błąd.
2
komentarz 26 lutego 2019 przez criss Mędrzec (172,590 p.)
Jak otworzysz jednocześnie z flagą in oraz out, to nie jest czyszczony. I możesz sobie pisać do niego w dowolne miejsce jak do pamięci.
komentarz 26 lutego 2019 przez MrChick888 Obywatel (1,020 p.)
tylko jak mogę usunąc co trzecią linijkę i zapisać tam coś innego?
komentarz 26 lutego 2019 przez Hiskiel Pasjonat (22,830 p.)
seek
2
komentarz 26 lutego 2019 przez Hiskiel Pasjonat (22,830 p.)
Lub też jak Criss napisał, traktować to jak pamięć. Ja bym to wczytał do tablicy stringów i potem po przerobieniu zapisał do pliku. Czy to najbardziej wydajne nie wiem, ale zapewne zaraz zlecą się tu ludzie, którzy by magnesem bity na dysku zmieniali, więc tam nie oceniać..

1 odpowiedź

0 głosów
odpowiedź 26 lutego 2019 przez marcin99b Szeryf (83,930 p.)

Najlepiej to chyba zrobić kopie pliku przed wykonywaniem operacji
Bo nigdy nie wiesz czy przypadkiem coś pójdzie nie tak, skoro to takie ważne dane
Później pobrać zawartość pliku do pamięci, jako array (każdy element to osobna linia)
I podmienić te linie które mają być zmienione

Nie wiem jak w c++, w c# zrobiłbym coś w stylu
 

if(int.TryParse(zmienna, out _))
//jest liczbą do edycji 

(TryParse zwraca bool)

Podobne pytania

+1 głos
3 odpowiedzi 535 wizyt
pytanie zadane 3 kwietnia 2016 w C i C++ przez Ditrix Mądrala (5,650 p.)
0 głosów
1 odpowiedź 625 wizyt
pytanie zadane 26 listopada 2019 w C i C++ przez Hardwell Dyskutant (8,980 p.)
+1 głos
2 odpowiedzi 397 wizyt
pytanie zadane 8 sierpnia 2018 w C i C++ przez zendurix Nowicjusz (130 p.)

93,182 zapytań

142,196 odpowiedzi

322,002 komentarzy

62,513 pasjonatów

Advent of Code 2024

Top 15 użytkowników

  1. 2127p. - dia-Chann
  2. 2092p. - Łukasz Piwowar
  3. 2079p. - Łukasz Eckert
  4. 2037p. - Tomasz Bielak
  5. 2006p. - rucin93
  6. 2005p. - Łukasz Siedlecki
  7. 1964p. - CC PL
  8. 1785p. - Michal Drewniak
  9. 1744p. - rafalszastok
  10. 1724p. - Adrian Wieprzkowicz
  11. 1684p. - Mikbac
  12. 1624p. - Anonim 3619784
  13. 1520p. - Marcin Putra
  14. 1480p. - ssynowiec
  15. 1365p. - Dawid128
Szczegóły i pełne wyniki

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

Wprowadzenie do ITsec, tom 1 Wprowadzenie do ITsec, tom 2

Można już zamawiać dwa tomy książek o ITsec pt. "Wprowadzenie do bezpieczeństwa IT" - mamy dla Was kod: pasja (użyjcie go w koszyku), dzięki któremu uzyskamy aż 15% zniżki! Dziękujemy ekipie Sekuraka za fajny rabat dla naszej Społeczności!

...