Last active
March 31, 2017 17:18
-
-
Save astamatto/1671c5f5160075f1e60d3a0544eff498 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <sstream> //stringstreams | |
using namespace std; | |
int main() { | |
string linha; //variável para onde leremos cada linha | |
for (int i = 1; i <= 5; ++i) { //1.txt 2.txt 3.txt 4.txt 5.txt | |
cout << "Arquivo " << i << "\n"; | |
ostringstream sout; //use como um cout | |
sout << "arquivos/" << i << ".txt"; | |
ifstream arq( sout.str() ); //é preciso converter para string | |
while ( getline(arq, linha) ) { | |
cout << linha << "\n"; | |
} | |
} | |
} | |
/* Algumas observações: | |
1. ifstream significa Input File Stream, serve apenas para leitura. Para saída seria ofstream, | |
e para entrada e saída seria fstream | |
2. Arquivos em C++ (tratados com fstream) são fechados automaticamente no fim do escopo (no próximo | |
fechamento de chaves). Não é preciso dar close que nem C | |
3. String Streams em C++ suportam as mesmas operações que cout e cin, permitindo você "fingir" que está | |
jogando em cout ou pegando de cin, mas na verdade você está jogando ou pegando de uma string. | |
4. ostringstream é de Output String Stream e permite jogar a saída para uma string ao invés de para cout, | |
para entrada seria Input String Stream é istringstream e permite pegar entrada de uma string ao invés do cin. | |
Para os dois seria stringstream | |
5. Muitos lugares, como o fstream, esperam uma string - e não uma *String Stream* - mas String Stream tem uma | |
função para converter o que você colocou lá para string, o método .str() | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment