繁体   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