簡體   English   中英

ifstream我不能同時讀寫c ++

[英]ifstream i cant write and read at the same time c++

如果我使用switch語句,則讀取的部分確實可以在此代碼中工作,因為同一“ codigo”不能使用兩次,因此我正在使用Mensage反饋
“埃斯·科迪戈·傑存在”

請幫助我找出這段代碼有什么問題

標頭1

#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Paciente
{
private:
    int  codigo, fixo, celular;
    string nomePaciente, nomeCovenio;
public:



    void setCodigo(int x) {
        codigo = x;
    }
    int getCodigo() {
        return codigo;
    }
    void setFixo(int x) {
        fixo = x;
    }
    void setCelular(int x) {
        celular = x;
    }
    void setNomePaciente(string x) {
        nomePaciente = x;
    }
    void setNomeCovenio(string x) {
        nomeCovenio = x;
    }
    int getFixo() {
        return fixo;
    }
    int getCelular() {
        return celular;
    }
    string getNomeCovenio() {
        return nomeCovenio;
    }
    string getNomePaciente() {
        return nomePaciente;
    }
};

<i>#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include "Paciente.cpp"

using namespace std;
int main() {
    fstream texto1("paciente.txt", ios::in  |ios::out| ios::app);

    int s;
    Paciente p;
    string q,z,r;
    cout << "1-Cadastramento" << endl << "2-Agendamento" << endl << "3-Alteracao de Paciente" << endl << "4-Vizualizacao de Consultas" << endl;
    cin >> s;
    switch (s) {
    case 1:
        int w;
        cout << "diga seu codigo" << endl;
        cin >> s;
        p.setCodigo(s);
        cout << "diga seu nome" << endl;
        cin >> q;
        p.setNomePaciente(q);
        cout << "diga seu nome do covenio" << endl;
        cin >> q;
        p.setNomeCovenio(q);
        cout << "diga o seu telefone fixo" << endl;
        cin >> w;
        p.setFixo(w);
        cout << "diga o seu telefone celular" << endl;
        cin >> w;
        p.setCelular(w);
        texto1 << endl << "Paciente " << p.getNomePaciente() << endl;
        texto1 << "Covenio " << p.getNomeCovenio() << endl;
        texto1 << "Fixo " << p.getFixo() << endl;
        texto1 << "Celular " << p.getCelular() << endl;
        texto1 << "Codigo " << p.getCodigo() << endl;
        texto1.clear();
        while (texto1 >> z >> r) {
            if (z == "Codigo") {
                int x = atoi(r.c_str());
                if (x == 12) {
                    cout << "esse codigo ja existe" << endl;
                };

            };
        };

        break;
    case 2:
        break;
    case 3:
        break;
    case 4:
        break;
    default:
        cout << "Nao eh uma opcao" << endl;
        break;
    };


    texto1.close();
    system("pause");
    return 0;

}</i>,

fstream僅保留一個文件位置,在讀取和寫入之間共享。

每次在讀寫之間切換時,都必須進行搜索以設置下一個操作的位置。 如果您確實想使用當前頭寸,則可以使用

texto1.seekp(0, ios_base::cur);

這會將文件位置從當前位置“移動” 0個字節。 但這仍然是一種尋求,您可以從該位置讀取或寫入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM