Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <string> #include <iostream> #include <fstream> #include <vector> using namespace std; int main() { vector<string> v; ifstream in("FILE"); string line; while(getline(in, line)) v.push_back(line); for(int i =0;i <v.size(); i++) cout << i << ": " << v[i] << endl; } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Bsp : //Anfangsmarke 1 //TEXT ,TEXT,TEXT ,TEXT,TEXT ,TEXT //TEXT ,TEXT,TEXT ,TEXT,TEXT ,TEXT //TEXT ,TEXT,TEXT ,TEXT,TEXT ,TEXT //TEXT ,TEXT,TEXT ,TEXT,TEXT ,TEXT //ENdemarke 1 //Anfangsmarke 2 //TEXT ,TEXT,TEXT ,TEXT,TEXT ,TEXT //TEXT ,TEXT,TEXT ,TEXT,TEXT ,TEXT //TEXT ,TEXT,TEXT ,TEXT,TEXT ,TEXT //TEXT ,TEXT,TEXT ,TEXT,TEXT ,TEXT //ENdemarke 2 |
Quellcode |
|
1 |
string::size_type position = v.find( "Anfangsmarke2" ); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <string> #include <iostream> #include <fstream> #include <vector> #include <iomanip> #include <iterator> void read_block_data(const std::string&, std::size_t, std::vector<std::string>&) int main() { std::vector<std::string> data; read_block_data("test.txt", 2, data); std::copy(data.begin(), data.end(), std::ostream_iterator<std::string>(std::cout << "\n")); } void read_block_data(const std::string& filename, std::size_t block, std::vector<std::string>& data) { std::ifstream file(filename.c_str()); if (!file) throw std::invalid_argument("could not open file"); data.clear(); std::ostringstream ss("// Anfang "); ss << std::setw(2) << std::setfill('0') << block; std::string start(ss.str()); ss.str(""); ss.clear(); ss << "// Ende " << std::setw(2) << std::setfill('0') << block; std::string line; bool in_data = false; while (std::getline(file, line)) { if (in_data == false && line.find_first_of(start) != std::string::npos) { in_data = true; continue; } if (in_data == true && line.find_first_of(ss.str()) != std::string::npos) in_data = false; else data.push_back(line); } } |
Quellcode |
|
1 |
std::ostringstream ss("Anfang"); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <string> // std::string; std::getline #include <iostream> // std::cin, std::cout #include <fstream> // std::ifstream #include <vector> // std::vector #include <iomanip> // std::setw, std::setfill #include <iterator> // std::ostream_iterator #include <sstream> // sry hatte ich vergessen void read_block_data(const std::string&, std::size_t, std::vector<std::string>&) int main() { std::vector<std::string> data; read_block_data("test.txt", 2, data); std::copy(data.begin(), data.end(), std::ostream_iterator<std::string>(std::cout << "\n")); } void read_block_data(const std::string& filename, std::size_t block, std::vector<std::string>& data) { std::ifstream file(filename.c_str()); if (!file) throw std::invalid_argument("could not open file"); data.clear(); // falls daten im übergebenen Vektor => löschen! std::ostringstream ss("// Anfang "); // Im Buffer von ss steht jetzt "// Anfang " ... ss << std::setw(2) << std::setfill('0') << block; // das folgende (block) soll 2(setw) Zeichen lang sein => ansonnsten mit (setfill) '0' auffüllen. std::string start(ss.str()); // was im Buffer von ss steht in start packen ... ss.str(""); // ss zurücksetzen ss.clear(); ss << "// Ende " << std::setw(2) << std::setfill('0') << block; // das selbe wie mit // Anfang std::string line; bool in_data = false; while (std::getline(file, line)) { if (in_data == false && line.find_first_of(start) != std::string::npos) { in_data = true; continue; } if (in_data == true && line.find_first_of(ss.str()) != std::string::npos) in_data = false; else data.push_back(line); } } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Anfang 01 var_visible = true; var_show = false; // Ende 01 // Anfang 02 var_visible = false; var_show = true; // Ende 02 // Anfang 03 var_visible = false; var_show = false; // Ende 03 |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <string> // std::string; std::getline #include <iostream> // std::cin, std::cout #include <fstream> // std::ifstream #include <vector> // std::vector #include <iomanip> // std::setw, std::setfill #include <iterator> // std::ostream_iterator #include <sstream> // sry hatte ich vergessen void read_block_data(const std::string&, std::size_t, std::vector<std::string>&); int main() { std::vector<std::string> data; read_block_data("test.txt", 2, data); std::copy(data.begin(), data.end(), std::ostream_iterator<std::string>(std::cout, "\n")); } void read_block_data(const std::string& filename, std::size_t block, std::vector<std::string>& data) { std::ifstream file(filename.c_str()); if (!file) throw std::invalid_argument("could not open file"); data.clear(); std::ostringstream ss; ss << "// Anfang " << std::setw(2) << std::setfill('0') << block; std::string start(ss.str()); ss.str(""); ss.clear(); ss << "// Ende " << std::setw(2) << std::setfill('0') << block; std::string line; bool in_data = false; while (std::getline(file, line)) { if (in_data == false && line.find(start) != std::string::npos) in_data = true; else if (in_data == true) { if (line.find(ss.str()) != std::string::npos) in_data = false; else data.push_back(line); } } } |
Werbeanzeige