Wczytywanie danych z / zapis danych do plikow
Wczytywanie wszystkich linii pliku tekstowego do stringu i tablicy znakowej, nastepnie sortowanie wczytanych linii i zapis do pliku
wczytywanie pliku tekstowego do tablicy znakowej znak po znaku ...
wczytywanie pliku tekstowego linia po linii
#include <iostream>
#include <fstream>
#include <string>
#include <set>
int main(int argc, char* argv[])
{
// read in the data
std::ifstream plik("Test.txt");
if (!plik.is_open())
throw-1;
std::string sLine, sText;
std::multiset<std::string> oSetOrdered;
while (getline(plik, sLine))
{
std::cout << sLine << std::endl;
sText += sLine + "
";
oSetOrdered.insert(sLine);
}
// character array conversion
char * cArray = const_cast<char*>(sText.c_str());
// display the ordered oSetOrdered
for (std::multiset<std::string>::iterator I1=oSetOrdered.begin();I1!=oSetOrdered.end();I1++)
std::cout << (*I1) << std::endl;
// saving the ordered set into a file ...
std::ofstream oFileOut("Test_out.txt");
for (std::multiset<std::string>::iterator I1=oSetOrdered.begin();I1!=oSetOrdered.end();I1++)
oFileOut << (*I1) << std::endl;
// clean-up and exit
oFileOut.close();
plik.close();
sText.clear();
return 0;
}
#include <fstream>
#include <string>
#include <set>
int main(int argc, char* argv[])
{
// read in the data
std::ifstream plik("Test.txt");
if (!plik.is_open())
throw-1;
std::string sLine, sText;
std::multiset<std::string> oSetOrdered;
while (getline(plik, sLine))
{
std::cout << sLine << std::endl;
sText += sLine + "
";
oSetOrdered.insert(sLine);
}
// character array conversion
char * cArray = const_cast<char*>(sText.c_str());
// display the ordered oSetOrdered
for (std::multiset<std::string>::iterator I1=oSetOrdered.begin();I1!=oSetOrdered.end();I1++)
std::cout << (*I1) << std::endl;
// saving the ordered set into a file ...
std::ofstream oFileOut("Test_out.txt");
for (std::multiset<std::string>::iterator I1=oSetOrdered.begin();I1!=oSetOrdered.end();I1++)
oFileOut << (*I1) << std::endl;
// clean-up and exit
oFileOut.close();
plik.close();
sText.clear();
return 0;
}
wczytywanie pliku tekstowego do tablicy znakowej znak po znaku ...
#include <iostream>
#include <fstream>
using namespace std;
int main (void) {
ifstream plik("xxx.txt");
if (!plik.is_open())
throw-1;
char tablica[100];
for (int i=0; i<100; i++) {
plik >> tablica[i];
}
for (int i=0; tablica[i]!=NULL; i++) {
cout << tablica[i];
}
return 0;
}
#include <fstream>
using namespace std;
int main (void) {
ifstream plik("xxx.txt");
if (!plik.is_open())
throw-1;
char tablica[100];
for (int i=0; i<100; i++) {
plik >> tablica[i];
}
for (int i=0; tablica[i]!=NULL; i++) {
cout << tablica[i];
}
return 0;
}
wczytywanie pliku tekstowego linia po linii
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (void) {
ifstream odczyt("xxx.txt");
if (!plik.is_open())
throw-1;
string linia, zawartosc;
while (getline(odczyt, linia)) {
zawartosc+=linia + "
";
}
cout << zawartosc;
return 0;
}
#include <fstream>
#include <string>
using namespace std;
int main (void) {
ifstream odczyt("xxx.txt");
if (!plik.is_open())
throw-1;
string linia, zawartosc;
while (getline(odczyt, linia)) {
zawartosc+=linia + "
";
}
cout << zawartosc;
return 0;
}
Autorem tekstu jest:
Marek Hajduczenia
Materiał dodany przez użytkownika: marek_haj
