Remove a character from a text file

Hi,
I'd like to remove the character % from a text file using c++
Thanks
Younes
closed account (Dy7SLyTq)
ok... ummm do u have any code so far
I send u the code
I want to use the file surf_db.txt. but it contains some strings starting with %. I want to drop them from the file surf_db.txt.


Mat clustering(){
int vari;
ifstream ifs( "surf_db.txt" );
string temp;
char separateur=' ';
vector<string> vecteur;
Mat matrixe;
line_size=0;
colomn_size=0;
while( getline( ifs, temp))

{
vecteur.clear();

string::size_type stTemp = temp.find(separateur);
while(stTemp != string::npos)
{
colomn_size++ ;
if(temp.substr(0, stTemp)!=temp.substr(0, stTemp))
vari=0;
else
vari=1;

if(vari==1)
vecteur.push_back(temp.substr(0, stTemp)); // add a string to the vector
temp = temp.substr(stTemp + 1);
stTemp = temp.find(separateur);

}

matrixe.push_back(vecteur);
line_size++;

}
return matrixe;
}

thanks
Drop leading '%' char from strings read from a file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main()
{
    std::ifstream file( "surf_db.txt" ) ;
    std::vector< std::string > vecteur ;

    std::string token ;
    while( file >> token )
    {
        std::size_t start = token[0] == '%' ? 1 : 0 ;
        vecteur.push_back( token.substr(start) ) ;
    }

    // ...
}
Hi,

I use the following code:


Mat clustering(){
int vari;
ifstream ifs( "surf_db.txt" );
string temp;
char separateur=' ';
vector<string> vecteur;
Mat matrixe;
line_size=0;
colomn_size=0;
std::string token ;

while( getline( ifs, temp))

{
vecteur.clear();

string::size_type stTemp = temp.find(separateur);
while(stTemp != string::npos)
{
colomn_size++ ;


string start = temp.substr(0, 2) == '[' ? 0 : 1 ;


vecteur.push_back(start);


temp = temp.substr(stTemp + 1);
stTemp = temp.find(separateur);

}

matrixe.push_back(vecteur);
line_size++;

}
return matrixe;
}


But it gives me the following error:

Mat clustering(){
int vari;
ifstream ifs( "surf_db.txt" );
string temp;
char separateur=' ';
vector<string> vecteur;
Mat matrixe;
line_size=0;
colomn_size=0;
std::string token ;

while( getline( ifs, temp))

{
vecteur.clear();

string::size_type stTemp = temp.find(separateur);
while(stTemp != string::npos)
{
colomn_size++ ;


string start = temp.substr(0, 2) == '[' ? 0 : 1 ;


vecteur.push_back(start);


temp = temp.substr(stTemp + 1);
stTemp = temp.find(separateur);

}

matrixe.push_back(vecteur);
line_size++;

}
return matrixe;
}

But it gives me the following error:


monocularc_1.cpp:135: error: no match for ‘operator==’ in ‘std::basic_string<_CharT, _Traits, _Alloc>::substr(typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type) const [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](0ul, 2ul) == '['’



Thanks
> temp.substr(0, 2) == '['

A std::string can be compared with another string. But not with a char

some_boolean_expression ? 0 : 1 is an int, not a std::string

What are you tring to do in your program?
Last edited on
I try it but it doesn't work.
I want to use this code to exploit these numbers which are SURF descritors of an image.They are extracted with OpenCV

I send u sample of the input file:


, 255, 255, 255, 0, 131, 92, 247, 227, 3, 1, 50, 244, 255, 190, 95, 135, 50, 113, 248, 255, 255, 47, 209, 252, 255, 62, 27, 76, 205, 207]
192.65 28.9043 [255, 255, 255, 55, 0, 0, 0, 64, 224, 195, 7, 63, 240, 199, 255, 255, 255, 255, 252, 49, 14, 192, 0, 71, 0, 128, 25, 35, 8, 3, 4, 96, 0, 255, 255, 255, 255, 29, 194, 24, 231, 152, 131, 16, 8, 225, 255, 0, 0, 96, 48, 49, 153, 252, 255, 255, 129, 72, 100, 38, 27, 204, 204, 207]
166 31 [176, 251, 255, 255, 243, 195, 3, 0, 128, 134, 29, 238, 112, 199, 63, 254, 255, 255, 253, 115, 14, 195, 0, 4, 0, 0, 224, 255, 255, 255, 119, 143, 25, 108, 252, 255, 255, 255, 243, 28, 196, 0, 3, 0, 0, 0, 36, 143, 207, 255, 255, 206, 226, 253, 255, 247, 217, 124, 97, 32, 16, 12, 28, 8]


I'd like to drop the [ character.



Assuming that each line in the file has the following format
<double> <double> [<int>, <int>, <int> ...]

1. Define a class to hold the numbers extracted from each line:
1
2
3
4
5
6
struct cluster_t // contents of one line in the file
{
    double first = 0.0 ; // C++11
    double second = 0.0 ; // C++11
    std::vector<int> rest ;
};


2. Read each complete line from the file into a string

3. Replace comma , [ and ] in the line with spaces

4. Use an input string stream to extract the numbers from the line

Something like this:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

struct cluster_t // contents of one line in the file
{
    double first = 0.0 ;  // omit the in-class member initializer in legacy C++
    double second = 0.0 ;
    std::vector<int> rest ;
};

// replace ',' '[' or ']' with space
std::string clean( std::string line )
{
    for( char& c : line ) // C++11 range-base for loop; rewrite in legacy C++ 
        if( c == ',' || c == '[' || c == ']' ) c = ' ' ;

     return line ;
}

// extract the cluster_t in a line (minimal validation)
bool extract_from( const std::string& line, cluster_t& result )
{
    std::istringstream stm( clean(line) ) ; // use a string stream for the extraction

    if( stm >> result.first >> result.second ) // if we can read the first two numbers
    {
        // read the rest into the vector
        int v ;
        while( stm >> v ) result.rest.push_back(v) ;
        return stm.eof() ; // true if we have read everything up to the end of line 
    }

    return false ; // badly formed line
}

std::vector<cluster_t> from_file( const std::string& file_name =  "surf_db.txt" )
{
      std::vector<cluster_t> result ;
      std::ifstream file(file_name) ; // file_name.c_str() in legacy C++

      std::string line ;
      while( std::getline( file, line ) )
      {
          cluster_t cluster ;
          if( extract_from( line, cluster ) ) result.push_back(cluster) ;
      }

      return result ;
}

int main() // minimal test driver
{
    const char* const test_file_name = "surf_db_test.txt" ;
    const char* const test_data = "192.65 28.9043 [255, 255, 0, 0, 64, 224, -111]\n"
                                  "166 31 [176, 251, 255, 25, 243, 3, 0, 128, 999]\n"
                                  "badly formed line\n"
                                  "1234 5678\n" // also badly formed
                                  "123.4 56.789 [ 55, 0, 0, 0, 64, 224, -333, bad. ]\n"
                                  "123.4 56.789 [ 55, 0, 0, 0, 64, 224, -333, 255 ]\n";
    std::cout << test_data << "---------------------------\n" ;

    // create a test file
    std::ofstream(test_file_name) << test_data ;

    // test the code
    std::vector<cluster_t> clusters_read = from_file(test_file_name) ;

    for( const auto& cluster : clusters_read )
    {
        std::cout << "{ " << cluster.first << ", " << cluster.second << ", [ " ;
        for( int v : cluster.rest ) std::cout << v << ' ' ;
        std::cout << "] }\n" ;
    }
}



I tried this code but it doen't work.
I use another descriptor rather than BRISK t generate a text file without brackets.
Thanks

Topic archived. No new replies allowed.