Προς το περιεχόμενο

C++ remove characters from string


Προτεινόμενες αναρτήσεις

Δημοσ.

Γεια σας παιδιά!

Προσπαθώ να λύσω ένα πρόβλημα,

έχω ένα αρχείο, το διαβάζω χρησιμοποιώντας ένα string, και

ελέγχω εαν μέσα σε αυτό το string υπάρχει το text "<br>"! εάν το βρει

τότε να περάσει αυτό το string σε μια άλλη μεταβλητή και από αυτή να

αφαιρέσαι το "<br>" χωρίς να πειράξει το υπόλοιπο περιεχόμενο!

 

έχω αυτό εδώ τον κώδικα!   έχω ψάξει και σε ιστοσελίδες αλλά ακόμα

δεν έχω καταφέρει να κάνω κάτι.  Επίσης όλος ο κώδικας δουλεύει μέχρι το σημείο "lengthOf = readLevel.length(); "! Επίσης το αρχείο που ανοίγω το έχω ήδη φτιάξει μόνος μου!

 

edit: Eίμαι σε windows xp professional, με visual c++ 2010

 

Ευχαριστώ προκαταβολικά!

 

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

ifstream	levelRead;

string		readLevel;
string		askLevel;

int			lines  = 0;
int			lengthOf;
int			length = 0;

int main()
{
	cout << "Type the name of the level you want to open:\n> ";
	cin  >> askLevel;

	levelRead.open(("C:\\RPG\\level" + askLevel + ".lev")); 
	if (levelRead.is_open())
	{
		while(!levelRead.eof())
		{
			getline(levelRead, readLevel);

			if (readLevel.find("<br>"))
			{
				lines++;
				
				lengthOf = readLevel.length();   
				length = readLevel.erase(lengthOf.first() lengthOf-3, lengthOf.last() lengthOf);
				
				
			}
		}
		levelRead.close();
	}
	else cout << "File didn't open!\n";
	levelRead.close();

	cin.get();
	cin.get();

	return 0;
}

Δημοσ.

Λοιπόν, παιδιά το βρήκα! Είναι κάπως buggy αλλά δουλεύει!

 

όποιος έχει κάποια ιδέα για βελτίωση ή κάτι άλλο ελεύθερα! :)

 

κώδικας:

 

#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

ifstream	levelRead;

string		readLevel;
string		askLevel;
string		contents;
int			lines  = 0;
int			length = 0;

int main()
{
	cout << "Type the name of the level you want to open:\n> ";
	cin  >> askLevel;

	levelRead.open(("C:\\RPG\\level" + askLevel + ".lev"));		//Opens a file with custom name!  
	if (levelRead.is_open())
	{
		while(!levelRead.eof())
		{
			getline(levelRead, readLevel);
			
			cout << readLevel << endl;

			if (readLevel.find("<br>"))
			{
				lines++;				   //We know how many lines the array will be.

				length = readLevel.length();
				
				length -= 4;				   //give him -3 length (<br>) 

				ostringstream convert;     // stream used for the conversion

				convert << length;

				//now we need to delete the <br> from the string
				readLevel.erase(readLevel.begin()+length, readLevel.end()-(length+4));

				contents = readLevel;

				cout << endl << contents << "\t Edited to make '<br>' disappear!!" << endl;
			}
			readLevel = "";			   //For memory issues we make it nothing.
		}
		levelRead.close();
	}
	else cout << "File didn't open!\n";
	levelRead.close();


	//cout << endl << contents << endl;

	cin.get();
	cin.get();

	return 0;
}

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα
  • Δημιουργία νέου...