Archived

This topic is now archived and is closed to further replies.

Parse problems

This topic is 5238 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I get my program to delete certain symbols frfom a char? This is my file:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
using namespace std;

int main(void)
{
	char file2[80];
	cout << "File name: ";
	cin.getline(file2, 80, "*");
	cout << file2 << endl;
	return(0);
}
Any idea''s? Thanks in advance, demonrelams

Share this post


Link to post
Share on other sites
Loop over the array elements:


for(int i=0; i < sizeof file2; i++)
{
if(file2[i] == '\\')
file2[i] = ' '; //replace \ with space

}


[edited by - antareus on August 14, 2003 2:46:30 PM]

Share this post


Link to post
Share on other sites
antareus: He said 'delete', not replace. Incidentally, it would be advisable to use strlen(), so as only to examine the elements that make up the null-terminated string.

demonrealms: Since you are using C++, you really should be using std::string instead of C-style strings. Here is how simple the code would be for your particular program, if say you wanted to erase all occurrences of '/':

#include <fstream>
#include <string>
#include <algorithm>

int main()
{
std::cout << "File name: ";
std::string file_name;
std::getline( std::cin, file_name, '*' );

file_name.erase( std::remove( file_name.begin(), file_name.end(), '/' ),
file_name.end() );

std::cout << file_name << std::endl;

return 0;
}

Or if you meant erase a range of characters, here is an example, in which all the characters before, and including, the last forward-slash ('/') are erased:

#include <fstream>
#include <string>

int main()
{
std::cout << "File name: ";
std::string file_name;
std::getline( std::cin, file_name, '*' );

file_name.erase( file_name.begin(),
file_name.begin() + file_name.find_last_of('/') + 1 );

std::cout << file_name << std::endl;

return 0;
}


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on August 14, 2003 6:21:18 PM]

Share this post


Link to post
Share on other sites