Tratamento de Arquivo .txt

1. Tratamento de Arquivo .txt

Matheus Rocha Ribeiro
powerrangergordo

(usa Outra)

Enviado em 28/06/2018 - 11:26h

Amigos,

Boa Dia, Desde ontem quando estava dando continuidade ao código que foi declarado para eu tratar me deparei com uma dificuldade enorme,

Eu preciso fazer um código onde ele leia um arquivo .txt linha por linha e sempre que a primeira letra for numero 2 ele vai pular de linha e continuar escrevendo o programa até encontrar novamente o numero 2 e ele pular de linha e assim até o final do programa fazendo o mesmo processo, quando o programa terminar de rodar ele vai armazenar em um outro arquivo .txt já formatado.



Já consegui colocar o arquivo para ler o arquivo e salvar com o auxilio de um amigo daqui mesmo mas eu estou me perdendo na lógica de como colocar a primeira letra da linha ser 2 ela pular já tentei de todas as possibilidades, inclusive de tentar como char mas eu me perco mt . por favor galera alguém poderia me ajudar ?





MEU CÓDIGO



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

int main() {

setlocale(LC_ALL, "Portuguese");

ifstream arquivo;
ofstream arquivox;
string linha;

arquivo.open("Declan.txt"); // abre arquivo p leitura
arquivox.open("Teste.txt"); // abre arquivo p escrita

if (arquivo.is_open() && arquivox.is_open()) {
while (getline(arquivo, linha)) {
cout << linha; // lê o arquivo linha a linha
if (arquivo, linha) == 2{
arquivox << linha <<endl; // escreve no arquivo

}
}

arquivo.close();
arquivox.close();
} else {
cout << "Não foi possivel abrir o arquivo" << endl;
}

return 0;
}


  


2. Re: Tratamento de Arquivo .txt

Diego Mendes Rodrigues
diegomrodrigues

(usa Ubuntu)

Enviado em 28/06/2018 - 11:53h

Caro,

Adicionei o teste com if (linha.at(0) != '2') no seu programa.

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

int main() {

setlocale(LC_ALL, "Portuguese");

ifstream arquivo;
ofstream arquivox;
string linha;

arquivo.open("Declan.txt"); // abre arquivo p leitura
arquivox.open("Teste.txt"); // abre arquivo p escrita

if (arquivo.is_open() && arquivox.is_open()) {
while (getline(arquivo, linha)) {
cout << linha << endl; // lê o arquivo linha a linha

//cout << linha.at(0) << endl;
if (linha.at(0) != '2')
arquivox << linha <<endl; // escreve no arquivo
}

arquivo.close();
arquivox.close();

} else {
cout << "Não foi possivel abrir o arquivo" << endl;
}

return 0;
}


Abraço,
Diego M. Rodrigues


3. Tratamento de Arquivo .txt

Matheus Rocha Ribeiro
powerrangergordo

(usa Outra)

Enviado em 28/06/2018 - 12:11h

Amigo você poderia em dizer o que foi aquele código que vc comentou o //cout<< ?

Porque assim eu rodei o programa aqui e não notei nada de diferente.

Porque por exemplo eu queria que o programa ia lendo linha por linha quando encontrar o 2 ele pula de linha.
EX QUALQUER:
2ANGRA DOS REISA 021d5sa3da6sdasdkasdkasmdamsdasdm
6ilemdslsnqdjsikadjjsqidjqkjd
7dsffdsfsdfasf10dfsa05f15rf
4rf1fdwe521cd5212ed1w25edwe
6dwf5w15fd1w2d1ew2d12ew1d5we1f1s3
2RIO DEJANEIRO
8d5sad4ds5a1ds541ad
9sad56as4da52sd25asd

EX DE COMO EU QUERIA COLOCAR:

2ANGRA DOS REISA 021d5sa3da6sdasdkasdkasmdamsdasdm 6ilemdslsnqdjsikadjjsqidjqkjd 7dsffdsfsdfasf10dfsa05f15rf 7dsffdsfsdfasf10dfsa05f15rf 4rf1fdwe521cd5212ed1w25edwe 6dwf5w15fd1w2d1ew2d12ew1d5we1f1s3

2RIO DEJANEIRO 8d5sad4ds5a1ds541ad 9sad56as4da52sd25asd

entende ?


4. Re: Tratamento de Arquivo .txt

Diego Mendes Rodrigues
diegomrodrigues

(usa Ubuntu)

Enviado em 28/06/2018 - 13:09h

Aquela linha comentada servia apenas para exibir o 1o caractere da linha na tela.

Veja se esse novo programa te atende, alterei apenas o 'if'.
#include <iostream>
#include <fstream>
#include <locale>
using namespace std;

int main() {

setlocale(LC_ALL, "Portuguese");

ifstream arquivo;
ofstream arquivox;
string linha;

arquivo.open("Declan.txt"); // abre arquivo p leitura
arquivox.open("Teste.txt"); // abre arquivo p escrita

if (arquivo.is_open() && arquivox.is_open()) {
while (getline(arquivo, linha)) { // lê o arquivo linha a linha
cout << linha << endl;

if (linha.at(0) != '2')
arquivox << linha <<endl; // escreve no arquivo
else
arquivox << endl << linha << endl; // escreve no arquivo
}

arquivo.close();
arquivox.close();

} else {
cout << "Não foi possivel abrir o arquivo" << endl;
}

return 0;
}

Abraço,
Diego M. Rodrigues


5. Re: Tratamento de Arquivo .txt

Diego Mendes Rodrigues
diegomrodrigues

(usa Ubuntu)

Enviado em 28/06/2018 - 13:17h

Outra forma, mais elegante, com um 'if' apenas para pular a linha no caso do 1o caractere ser um '2'.
#include <iostream>
#include <fstream>
#include <locale>
using namespace std;

int main() {

setlocale(LC_ALL, "Portuguese");

ifstream arquivo;
ofstream arquivox;
string linha;

arquivo.open("Declan.txt"); // abre arquivo p leitura
arquivox.open("Teste.txt"); // abre arquivo p escrita

if (arquivo.is_open() && arquivox.is_open()) {
while (getline(arquivo, linha)) { // lê o arquivo linha a linha
cout << linha << endl;

if (linha.at(0) == '2')
arquivox << endl;

arquivox << linha << endl;
}

arquivo.close();
arquivox.close();

} else {
cout << "Não foi possivel abrir o arquivo" << endl;
}

return 0;
}

Abraço,
Diego M. Rodrigues


6. Tratamento de Arquivo .txt

Matheus Rocha Ribeiro
powerrangergordo

(usa Outra)

Enviado em 28/06/2018 - 13:43h

Acho que foi Meu amigo, porra tu me deu uma [*****] duma luz ! mt obrigado vou só ver mesmo se é isso aqui e já te digo.
no caso seria o com if/else


7. Tratamento de Arquivo .txt

Matheus Rocha Ribeiro
powerrangergordo

(usa Outra)

Enviado em 28/06/2018 - 14:14h

Cara eu acho que não é isso não,

vou mandar alguns paragrafos aqui do arquivo que eu to tentando fazer isso pra vc dar uma olhada.
toda linha que começar com 2 na frente eu tenho que dar um <<endl eae a lógica ta certa mas na hora que eu comparo com o arquivo que tenho acho que da bug. ve ae
vou mandar eaqui em baixo:
1201604/08/2017 11:37:31Todas MTTT T06/07/201703/08/2017000000000000000000000000000000
2ANGRA DOS REIS 0000000014
385178911J39164256000170VIA SAT COMERCIAL ELETRONICA LTDA ME NNS07/07/2017 08:28:33 DN NNNNNNN000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000S
5SSSSN000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000
6 JOSÉ JORGE MARQUES 24 33652762SIDNEI COSTA VIEIRA 24 33652762
383067241J29355260000242TRIUNFO LOGISTICA LTDA NNS17/07/2017 16:20:05 DN NNNNNNN000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000S
5NNSNN000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000
6 wilson@wleme.com JORGE RODRIGUES DA SILVA 21 21788800WILSON ROBERTO LEME 21 21788800
378884096J11112293000204VALE DO CAFE AUTO POSTO LTDA NRS17/07/2017 16:22:44 DN SNSNNNN000000558482002000000000000000000000000000000000000914863662000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000558482002000000914863662000000000000000000000000000000000000000000000000000000000000000000000711192000000000000000000000000000000000000000000000000000355670468 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000355670468S
4N00037000000000711192
5NNNNN000000087369776000000000000000000000000000000000000000000000N000000083505478000000000000000000000000000000000000000000000N000000074110649000000000000000000000000000000000000000000000N000000075302716000000000000000000000000000000000000000000000N000000068143708000000000000000000000000000000000000000000000N000000067633557000000000000000000000000000000000000000000000N000000074329043000000000000000000000000000000000000000000000N000000077872727000000000000000000000000000000000000000000000N000000075293488000000000000000000000000000000000000000000000N000000078980667000000000000000000000000000000000000000000000N000000063227581000000000000000000000000000000000000000000000N000000088383080000000000000000000000000000000000000000000000
6 HAROLDO DA SILVA MANCEBO 24 24534614DANIEL MARIA DINIZ 24 24533886
385454455J00468285000190TRANZIRAN TRANSPORTES LTDA NRS19/07/2017 11:01:58 DN SNNSNNN000029414831790000000701696111000000000000000000044743461671000001941418951000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030116527901000046684880622000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001040394000000001040394 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001040394N
4N00026000000001040394
379050008J11921635000148GB SEGURANCA ELETRONICA DE ANGRA LTDA NNS20/07/2017 16:17:50 DN NNNNNNN000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000S
5SSSSN000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000
6 DANIELLE BASILIO RAMALHO BARRA 24 33644474KARLA SUELI VIEIRA SANTIAGO DE OLIVEIRA 24 33677018
378234512J08595642000192COSTA BELLA COMERCIO DE ALIMENTOS LTDA NNS21/07/2017 10:44:35 DN SNSNNNN000000000174879000000000000000000000000000000000000000163608000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000174879000000000163608000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000079200000000000000000000000000067929 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000067929S
5SSNNN000000000163608000000000000000000000000163608000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000
6 contato@matoscontabilidade.com.br LEONARDO DE CARVALHO VITALE 24 33651359JORGE DE MATOS 24 33651359
379943851J15121491000600CLICK - RODO ENTREGAS LTDA NNS24/07/2017 13:09:41 DN SNNSNNN000000000000000000000000000000000000000000000000000182939123000001048485154000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001231424277000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000414078000000000414078 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000414078N
4N00026000000000414078
384088544J28361483000179DOM BOSCO TURISMO E TRANSPORTES LTDA NNS24/07/2017 15:48:36 DN SNNSNNN000000000000000000000000000000000000000000000000000151117966000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000151717966000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000000000000340000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000N
4N00026000000000340000
377669981J05886614000217DIRECT EXPRESS LOGISTICA INTEGRADA S A NNS25/07/2017 14:27:08 DN SNNSNNN000000000000000000000000000000000000000000000000000160999495000001493433648000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001654433143000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000939427000000000939427 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000939427N
4N00026000000000939427
387013952J05886614003585DIRECT EXPRESS LOGISTICA INTEGRADA S A NNS26/07/2017 09:54:24 DN SNNSNNN000000000000000000000000000000000000000000000000000245385624000000492822914000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000738208538000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001937725000000001937725 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001937725N
4N00026000000001937725
376105502J03419953000187MARFRAN TRANSPORTES E LOGISTICA LTDA ME NNS28/07/2017 12:58:14 DN SNNSNNN000000066276412000000001168000000000000000000000000409524507000000086736560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000067444412000000496261067000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008345356000000008345356 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008345356N
4N00026000000008345356
387244628J72435837001146IPANEMA COUNTRY CAFE LTDA NNS31/07/2017 11:05:55 DN SNSNNNN000000018811267000000002299648000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021110915000000000000000000000000000000000000000000000000000003126176000000000000000000000000000000000000000000000000000021110915000000000000000000000003126176 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003126176S
4N00008000000003126176
5NNSNN000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N000000000000000000000000000000000000000000000000000000000000N0000000000000000000000





8. Re: Tratamento de Arquivo .txt

Diego Mendes Rodrigues
diegomrodrigues

(usa Ubuntu)

Enviado em 28/06/2018 - 14:35h

Criei um arquivo chamado Entrada.txt com essa informação que você enviou.
http://www.drsolutions.com.br/vol/18JYT_Entrada.zip

Executei o programa em C++, alterando o nome do arquivo de entrada.
http://www.drsolutions.com.br/vol/18JYT_arquivos.zip

No resultado, apenas na linha
2ANGRA DOS REIS 0000000014
que o programa pulou uma linha, ao enviar para o arquivo de saida, o Teste.txt, já que é a única que começa com 2.
http://www.drsolutions.com.br/vol/18JYT_Teste.zip

O que não está funcionando corretamente?

Você pode me enviar seu arquivo "Declan.txt", ou parte dele, como um zip (diego.m.rodrigues@gmail.com)?

Abraço,
Diego M. Rodrigues






Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts