Ir ao conteúdo
  • Cadastre-se

C++ Ler os 4 primeiros numeros de um arquivo


Posts recomendados

Galera boa tarde,

 

Estou com um arquivo onde preciso lêr linha por linha, só que por exemplo sempre que o arquivo começar com 2000, 2100, 2010, 2120, 2121,2122,2130, 2200, 2999 eu crie um arquivo para cada um e armazene tudo que for referente a eles no arquivo. Meu programa ele está lendo, criando os arquivos só que  não consegue armazenar nos arquivos .txt no caso o arquivo sempre terá que ler os 4 primeiros caracteres. e se for igual aos números citados ele vai armazenar nos arquivos.

Meu código:

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

int main() {

    setlocale(LC_ALL, "Portuguese");

    ifstream arquivo1;
    ofstream arquivox3;
    ofstream arquivox4;
    ofstream arquivox5;
    ofstream arquivox6;
    ofstream arquivox7;
    ofstream arquivox8;
    ofstream arquivox9;
    ofstream arquivoxx;
    ofstream arquivoxx1;
    
    string linha;
    string ins;    
    arquivo1.open("DEFIS2013.txt"); // abre arquivo p leitura
    arquivox2.open("arquivo2000.txt"); // abre arquivo p escrita
    arquivox3.open("arquivo2100.txt"); // abre arquivo p escrita
    arquivox4.open("arquivox2110.txt"); // abre arquivo p escrita
    arquivox5.open("arquivox2120.txt"); // abre arquivo p escrita
    arquivox6.open("arquivox2121.txt"); // abre arquivo p escrita
    arquivox7.open("arquivox2122.txt"); // abre arquivo p escrita
    arquivox8.open("arquivox2130.txt"); // abre arquivo p escrita
    arquivox9.open("arquivox2200.txt"); // abre arquivo p escrita
    arquivoxx.open("arquivox2999.txt"); // abre arquivo p escrita


            
    
        
    if (arquivo1.is_open() && arquivox2.is_open() && arquivox3.is_open()  && arquivox4.is_open() && arquivox5.is_open() && arquivox6.is_open() && arquivox7.is_open()  && arquivox8.is_open() && arquivox9.is_open() && arquivoxx.is_open())  
    {
        while (getline(arquivo1,linha))
        {
            cout<< linha;
            
        if (linha.at(0) && linha.at(1) && linha.at(2) && linha.at(3) == '2000')
        {    
        /*    ins = linha.at(1);
            ins = ins + linha.at(2);
            ins = ins + linha.at(3);
            ins = ins + linha.at(4);
            ins = ins + linha.at(5);
            ins = ins + linha.at(6);
            ins = ins + linha.at(7);
            ins = ins + linha.at(8);
        */

            arquivox2 << linha <<endl; //arquivox3 recebe linha e pula linha
            
        }
        
        if (linha.at(0) && linha.at(1) && linha.at(2) && linha.at(3) == '2100')
        {
            
            arquivox4 << linha  <<endl;
            
        }
        
        if (linha.at(0) && linha.at(1) && linha.at(2) && linha.at(3) == '2110')
        {
            
            arquivox5 << linha  <<endl;
            
        }
        
        if (linha.at(0) && linha.at(1) && linha.at(2) && linha.at(3) == '2120')
        {
            arquivox5<< linha  <<endl;
        }
            
        }
        if (linha.at(0) && linha.at(1) && linha.at(2) && linha.at(3) == '2121')
        {
            
            arquivox6 << linha  <<endl;
            
        }
        
        if linha.at(0) && linha.at(1) && linha.at(2) && linha.at(3) == '2122')
        {
            arquivox7<< linha  <<endl;
        }
        if linha.at(0) && linha.at(1) && linha.at(2) && linha.at(3) == '2130')
        {
            arquivox8<< linha  <<endl;
        }
        
        if (linha.at(0) && linha.at(1) && linha.at(2) && linha.at(3) == '2200')
        {
            arquivo9<< linha  <<endl;
        }
        
            if (linha.at(0) && linha.at(1) && linha.at(2) && linha.at(3) == '2999')
        {
            arquivoxx<< linha  <<endl;
        }
        
        }
    
    
    

return 0;    
}

 

 

Código .txt exemplo que ele quer que eu leia

AAAA|100|29/06/2018 16:05:55|2013|DEFIS/DEFIS-C/PGDAS-D||Somente as atividades e distribuições do IPM|||Todas (Normais e Retifics.) |Definitivo Válido - 18/12/2014 15:47:41||NOVA IGUACU||||
2000|2013|45435032013001|2071408706699649|1|N|S|04543503|MULTHOTEL DISTRIBUIDORA DO MOBILIARIO LTDA - EPP|4543503000164|28/03/2014 15:00:50|17/04/2014 06:24:44|15648435|N|20/05/2014 16:54:34
2100|4543503000164|77217452
2110|1|S|3432217,29
2120|RJ|5869|NOVA IGUACU|01/01/2013
2121|16|S|N|920018,46
2130|S|5869|NOVA IGUACU|2018327,99
2999|7
2000|2013|10333062013001|2071408608630016|1|N|S|01033306|HELP STAR BLUE COMERCIO DE MATERIAIS MEDICOS E HOSPITALARES LTDA|1033306000107|27/03/2014 17:27:41|17/04/2014 05:04:49|||
2100|1033306000107|85833162
2110|1|S|3115344,72
2110|2|S|1509537,38
2120|RJ|5869|NOVA IGUACU|01/01/2013
2130|S|5869|NOVA IGUACU|1479962,27
2200|1033306201301001|01259032483362706689|15/02/2013 10:58:24|14/08/2013 19:16:39|2013|1
2200|1033306201302002|01358031583327706810|13/03/2013 16:44:36|15/08/2013 13:19:56|2013|2
2200|1033306201303001|01044039873346306170|19/04/2013 10:27:07|18/08/2013 19:47:46|2013|3
2200|1033306201304001|01355035133389006031|20/05/2013 13:05:45|19/08/2013 16:37:51|2013|4
2200|1033306201305002|01138030913346606182|16/07/2013 17:36:49|02/08/2014 02:11:37|2013|5
2200|1033306201306001|01072031853300506798|16/07/2013 17:47:02|02/08/2014 02:11:37|2013|6
2200|1033306201307001|01270036473362506675|19/08/2013 14:58:46|22/08/2013 06:52:07|2013|7
2200|1033306201308001|01081032773333206559|18/09/2013 16:06:39|08/10/2013 06:02:58|2013|8
2200|1033306201309001|01086034373391006400|16/10/2013 12:05:41|23/10/2013 06:01:06|2013|9
2200|1033306201310001|01076030693323006440|01/11/2013 18:18:57|09/01/2014 01:27:31|2013|10
2200|1033306201311001|01201033553354706518|13/12/2013 17:09:14|10/01/2014 03:36:00|2013|11
2200|1033306201312001|01200035273343306550|17/01/2014 16:15:53|13/02/2014 13:18:23|2013|12
2999|19
2000|2013|60194892013001|2071408708140053|1|N|S|06019489|CLEAR OPTICAL LENS IMPORTACAO E EXPORTACAO LTDA - EPP|6019489000120|28/03/2014 15:59:25|17/04/2014 06:24:44|15655339|N|21/05/2014 15:01:34
2100|6019489000120|77657673
2110|4|S|3172739,25
2120|RJ|5869|NOVA IGUACU|01/01/2013
2121|16|S|N|331691
2130|S|5869|NOVA IGUACU|1346967,56
2999|7
2000|2013|57993712013001|2071408200034131|1|N|S|05799371|FORMAGGI ITALIANI BAR E RESTAURANTE LTDA|5799371000107|23/03/2014 09:46:27|17/04/2014 01:49:30|||
2100|5799371000107|77586920
2110|1|S|3097632,61
2110|2|S|836627,56
2120|RJ|5869|NOVA IGUACU|01/01/2013
2130|S|5869|NOVA IGUACU|1258963,25
2200|5799371201301001|05197793649307671007|14/02/2013 10:27:12|14/08/2013 17:33:58|2013|1
2200|5799371201302001|05311799529331971055|13/03/2013 11:46:56|15/08/2013 13:19:56|2013|2
2200|5799371201303001|05208798919384171179|11/04/2013 11:36:12|18/08/2013 09:39:08|2013|3
2200|5799371201304001|05351798799310571169|14/05/2013 13:49:36|19/08/2013 08:52:18|2013|4
2200|5799371201305001|05229794049380171467|12/06/2013 07:59:18|20/08/2013 00:22:28|2013|5
2200|5799371201306001|05185796579375271609|15/07/2013 10:33:39|20/08/2013 19:37:50|2013|6
2200|5799371201307001|05353790529354071160|13/08/2013 07:49:48|22/08/2013 00:09:35|2013|7
2200|5799371201308001|05110799309367271901|16/09/2013 09:18:31|08/10/2013 03:33:36|2013|8
2200|5799371201309001|05311795819334171033|15/10/2013 12:18:09|23/10/2013 04:23:21|2013|9
2200|5799371201310001|05176796409383571172|13/11/2013 08:11:38|09/01/2014 08:30:23|2013|10
2200|5799371201311001|05067798699336771723|13/12/2013 09:57:49|10/01/2014 03:36:00|2013|11
2200|5799371201312001|05178797779300971357|14/01/2014 12:52:01|13/02/2014 06:58:22|2013|12
2999|19
2000|2013|143394672013001|2071409003110828|1|N|N|14339467|MAYBE DO BRASIL COSMETICOS LTDA - ME|14339467000175|31/03/2014 10:28:28|17/04/2014 08:41:38|||
2100|14339467000175|79491853
2110|2|S|789919,18

 

Link para o comentário
Compartilhar em outros sites

@MatheusTITributos Olá. De maneira bem básica seu código poderia ficar assim:

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

int main() {

    setlocale(LC_ALL, "Portuguese");

    ifstream arquivo1;
    ofstream arquivox2, arquivox3, arquivox4, arquivox5, arquivox6, arquivox7, arquivox8, arquivox9, arquivox10;

    string linha;

    arquivo1.open("arquivoDadosC++.txt"); // abre arquivo p leitura
    arquivox2.open("arquivox2000.txt"); // abre arquivo p escrita
    arquivox3.open("arquivox2100.txt"); // abre arquivo p escrita
    arquivox4.open("arquivox2110.txt"); // abre arquivo p escrita
    arquivox5.open("arquivox2120.txt"); // abre arquivo p escrita
    arquivox6.open("arquivox2121.txt"); // abre arquivo p escrita
    arquivox7.open("arquivox2122.txt"); // abre arquivo p escrita
    arquivox8.open("arquivox2130.txt"); // abre arquivo p escrita
    arquivox9.open("arquivox2200.txt"); // abre arquivo p escrita
    arquivox10.open("arquivox2999.txt"); // abre arquivo p escrita

    if (arquivo1.is_open() && arquivox2.is_open() && arquivox3.is_open()
        && arquivox4.is_open() && arquivox5.is_open() && arquivox6.is_open()
        && arquivox7.is_open()  && arquivox8.is_open() &&
        arquivox9.is_open() && arquivox10.is_open()) {

        while (getline(arquivo1,linha)) {

            cout << linha << endl;

            if (linha.substr(0, 4) == "2000") {
                arquivox2 << linha << endl; //arquivox3 recebe linha e pula linha
            }
            if (linha.substr(0, 4) == "2100") {
                arquivox3 << linha  << endl;
            }
            if (linha.substr(0, 4) == "2110") {
                arquivox4 << linha  << endl;
            }
            if (linha.substr(0, 4) == "2120") {
                arquivox5 << linha  << endl;
            }
            if (linha.substr(0, 4) == "2121") {
                arquivox6 << linha  << endl;
            }
            if (linha.substr(0, 4) == "2122") {
                arquivox7 << linha  << endl;
            }
            if (linha.substr(0, 4) == "2130") {
                arquivox8 << linha  << endl;
            }
            if (linha.substr(0, 4) == "2200") {
                arquivox9 << linha  << endl;
            }
            if (linha.substr(0, 4) == "2999") {
                arquivox10 << linha  << endl;
            }
        }

    } else {
        cout << "Não foi possível abrir um ou mais arquivos!\n" << endl;
        exit(1);
    }

    arquivo1.close();
    arquivox2.close();
    arquivox3.close();
    arquivox4.close();
    arquivox5.close();
    arquivox6.close();
    arquivox7.close();
    arquivox8.close();
    arquivox9.close();
    arquivox10.close();

    return 0;
}

Devo confessar q está longe de ser um código bem elaborado, mas está funcionando assim 

Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...