code that reads from a file and writes it, inverted into another file

the error message is this one, and this is my first c++ code so i really want to see it work. any piece of advice?
7::too few template-parameter-lists


so this is the code...
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
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;
 
 std::string::filename = "in.txt"
 std::string::data = " this line of text will save in out.txt"
 std::string::filename2 = "out.txt"

//opening and reading module//
int open(filename)
{
	ifstream buffer(filename); //abrir para lectura//
	if( buffer.bad()){
	cout<< "there's something wrong with the file!";  //verifies//
	cin.get();
		return 1;
}

	while (! buffer.eof()) cout <<(char)buffer.get();
	buffer.close();
	cout << endl << "end of file has been reached" <<endl;
	return 0;
}
//writing and out module//

int write(filename2)
{
    ofstream archivo(filename2); // creates or rewrites file
    // verifies
    if ( archivo.bad() ) {
        cout << "there has been a mistake with the out :(";
        cin.get();
        return 1;
    }
 
    // writes file data//
    for (unsigned int t = 0; t < strlen(data); t++ )
        archivo.put(data[t] );
 
    archivo.close();
    cout << "archivo creado exitosamente" << endl;
    return 0;
}

//inverting module//

int invert(data) {

	
    string data;
    int longitud;

    longitud = data.length();
    cout<<" "<<data<<endl;
    cout<<" ";
    for (int contador = longitud - 1; contador > -1; contador--)
        cout<<data[contador];
    cout<<endl;
    return 0;
}

//instructions module//
int main()
{
	open(filename);
	invert(data);
	write(filename2);
return 0;
}

Last edited on
You should remove the double colon between the variable type and the name:
std::string::fileName = becomes std::string fileName = .

By the way, when posting code, put it in code tags (<> button).
i did and now it says error: expected ‘,’ or ‘;’ before ‘std’
in line 8? im sorry for being this newbie but as i said its my first code, thx for the help
You forgot to add a semicolon after lines 7, 8 and 9.
now i get 12: error: expected unqualified-id before ‘public’;
ok so now my code looks like this i wonder if all this trouble is because i am mixing some paradigms?
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
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;
 
 std::string filename = "entrada.txt";
 std::string data = " esta linea de texto se guardara en salida.txt";
 std::string filename2 = "salida.txt";

//modulo de apertura y lectura//
public class open(filename)
{
	ifstream buffer(filename); //abrir para lectura//
	if( buffer.bad()){
	cout<< "hay algun problema con el archivo!";  //verifica//
	cin.get();
		return 1;
}

	while (! buffer.eof()) cout <<(char)buffer.get();
	buffer.close();
	cout << endl << "se ha leido la informacion hasta el final del texto" <<endl;
	return 0;
};
//modulo de escritura y creacion de salida//

public class write(filename2)
{
    ofstream archivo(filename2); // crear o rescribir archivo
    // verificar la creación del archivo
    if ( archivo.bad() ) {
        cout << "hay un error con salida :(";
        cin.get();
        return 1;
    }
 
    // escribir datos al archivo
    for (unsigned int t = 0; t < strlen(data); t++ )
        archivo.put(data[t] );
 
    archivo.close();
    cout << "archivo creado exitosamente" << endl;
    return 0;
};

//modulo de transformacion//

public class invert(data) {

	
    string data;
    int longitud;

    longitud = data.length();
    cout<<" "<<data<<endl;
    cout<<" ";
    for (int contador = longitud - 1; contador > -1; contador--)
        cout<<data[contador];
    cout<<endl;
    return 0;
};

//modulo de instruccion//
public class  main()
{
	open(filename);
	invert(data);
	write(filename2);
return 0;
};
Last edited on
i wonder if all this trouble is because i am mixing some paradigms?

Yes, definitely. You should make clear for yourself the difference between functions and classes.
Something like this: public class invert( data ) looks like you've mixed Java, C++ and Python :)

Functions: http://www.learncpp.com/cpp-tutorial/14-a-first-look-at-functions/
Classes: http://www.learncpp.com/cpp-tutorial/82-classes-and-class-members/
ok i redesigned my code with the tutorials and now i have this code
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
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

int main () {
  string line;
  ifstream myfile ("/home/him/Escritorio/marinhector/info/entrada.txt"); //direccion del archivo de entrada completa
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line); //guarda los datos en line
      cout << line << endl; 
    }

    myfile.close();
  }

  else cout << "No se pudo abrir el archivo"; 

  ofstream myfile2 ("/home/him/Escritorio/marinhector/info/salida.txt"); //direccion del archivo de salida completa
  if (myfile2.is_open())
  {
 ifstream myfile ("/home/him/Escritorio/marinhector/info/entrada.txt"); //direccion del archivo de entrada completa
	getline (myfile,line);	
	strrev(line);           //this is the function that appears to be wrong
    myfile2 << line;
    myfile2.close();
  }
  else cout << "no se pudo escribir o abrir el archivo";

  return 0;

} 

which actually only gives me 2 mistakes, and i havent found an actual solution to it, google says it will only compile in windows

1
2
tarea_1_23.cpp: In function ‘int main()’:
tarea_1_23.cpp:27: error: ‘strrev’ was not declared in this scope



any good ideas? btw in the code before i had used a function that had a for cycle and it gave me errors talking about you cannot convert a string to an int so maybe is there a command to do that? atoi maybe? i am guessing that if i do that it will print the number of characters into the file and that is something i dont need, thanks in advance


It would seem like you haven't included the header for strrev although looking it up that's included with string.h.

It also seems to return a char* so it would seem like you have to use it like this.

 
line = strrev(line);
its okay now, i have fixed it using the resources from this page and another one so i will post the code that fully compiles and works
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("/home/him/Escritorio/marinhector/info/entrada.txt"); //direccion del archivo de entrada completa
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line); //guarda los datos en line
      cout << line << endl; 
    }

    myfile.close();
  }

  else cout << "No se pudo abrir el archivo"; 

  ofstream myfile2 ("/home/him/Escritorio/marinhector/info/salida.txt"); //direccion del archivo de salida completa (cambiar por la actual)
  if (myfile2.is_open())
  {
 ifstream myfile ("/home/him/Escritorio/marinhector/info/entrada.txt"); //direccion del archivo de entrada completa (cambiar por la actual)
	getline (myfile,line);	
	string str (line);
	string::reverse_iterator rit;
	for ( rit=str.rbegin() ; rit < str.rend(); rit++ )
	 
    myfile2 << *rit;
	cout <<"se ha guardado alrevez en el archivo salida!" << *rit <<endl;
    myfile2.close();
  }
  else cout << "no se pudo escribir o abrir el archivo";

  return 0;

}


thank you for everything this forum is awesome
Topic archived. No new replies allowed.