Text files.

Hi guys, hope you are learning every day. Need your knowledge.

I have this program and I managed to open it and read results but I don't know how to save the output in a different file. The exercise says the following:

"Read an existing text file, and convert all the characters, one to uppercase, one to lowercase, and so on, and save the result to a new file"..

This is my code:

#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h> //malloc
#include <conio.h>
#include <string.h>
#include <ctype.h>

char *nombre_archivo=(char*)malloc(sizeof(250));

//Prototipos de funciones
void Leer_Archivo_Linea();
void Leer_Archivo_Caracter();
void Crear_Archivo();

void main(){
Crear_Archivo();
Leer_Archivo_Linea();
Leer_Archivo_Caracter();

}

void Leer_Archivo_Linea(){
cout<<endl<<endl;
cout<<"Lectura de Archivo Linea por Linea"<<endl;
cout<<"Ingrese la ubicacion y nombre del archivo:"<<endl;
cout<<"(Ejemplo: c:\\carpeta1\\archivo_entrada.txt)"<<endl;
cin>>nombre_archivo;

ifstream Archivo(nombre_archivo,ios::in);
if(Archivo.fail()){
cout<<"ERROR AL ABRIR EL ARCHIVO"<<endl;
getch();
exit(0);
}else{
do{
char linea[2000]="";
Archivo.getline(linea,sizeof(linea));
cout<<linea<<endl;
}while (!Archivo.eof());
Archivo.close();
}
getch();
}

void Leer_Archivo_Caracter(){
cout<<endl<<endl;
cout<<"Lectura de Archivo Caracter por Caracter"<<endl;
cout<<"Ingrese la ubicacion y nombre del archivo:"<<endl;
cout<<"(Ejemplo: c:\\carpeta1\\archivo_entrada.txt)"<<endl;
cin>>nombre_archivo;

ifstream Archivo(nombre_archivo,ios::in);
if(Archivo.fail()){
cout<<"ERROR AL ABRIR EL ARCHIVO"<<endl;
getch();
exit(0);
}else{
do{
char caracter;
Archivo >> caracter;

{
if (caracter % 2==0)
{
caracter = toupper (caracter);
}
else
{
caracter = tolower(caracter);
}
}
printf(" %c",caracter);
}while (!Archivo.eof());
ofstream Archivo("Archivo2.txt");
Archivo << "Prueba" <<endl;
Archivo.close();
cout<<"\n \n Nuevo archivo archivo2.txt Creado en la ubicacion del programa fuente"<<endl;
}
getch();
}

void Crear_Archivo(){

ofstream Archivo("archivo.txt");

if(Archivo.fail()){
cout<<"ERROR al crear archivo"<<endl;
}else{
Archivo << "Archivo de letras intercaladas" <<endl;
Archivo.close();
cout<<"Archivo archivo.txt Creado en la ubicacion del programa fuente"<<endl;
}
getch();
}




any ideas? I´m using the ofstream but this just creates a new file.
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <cctype>
using namespace std;

istringstream strm( "Shche ne vmerla Ukrainy i slava, i volia" );

int main()
{
   istream &in  = strm;
   ostream &out = cout;
   // ifstream in( "input.txt" );
   // ofstream out( "output.txt" );
   
   int (*func[])(int) = { ::toupper, ::tolower };
   bool state = 0;
   
   for ( char c; in.get( c ); out.put( c ) )
   {
      if ( isalpha( c ) )
      {
         c = func[state]( c );
         state = !state;
      }
   }
}


ShChE nE vMeRlA uKrAiNy I sLaVa, I vOlIa 
You're using a very old version of C++, like pre '98 old.
1
2
#include <iostream.h>
#include <fstream.h> 


Beyond saying that nombre_archivo should be a string and not a char* initialized with malloc(), there isn't a lot to say about your code.

With all the free modern software around, it's inexcusable to not use something more modern.

For example, if you have an Android device, you can install Termux and The Hacker's Keyboard, then install make and clang with nano or vim; and you'll have an up-to-date environment.
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
#include <fstream>
#include <iostream>
#include <string>
#include <utility>
#include <cctype>

int main() {
	std::string in, op;

	std::cout << "Input file name:";
	std::getline(std::cin, in);

	std::ifstream ifs(in);

	if (!ifs)
		return (std::cout << "Cannot open input file\n"), 1;

	std::cout << "Output file name:";
	std::getline(std::cin, op);

	std::ofstream ofs(op);

	if (!ofs)
		return (std::cout << "Cannot open output file\n"), 2;

	for (auto [ch, cnt] {std::pair {char(0), size_t(1)}}; ifs.get(ch); )
		if (std::isalpha(static_cast<unsigned char>(ch)))
			if (cnt++ % 2)
				ofs.put(static_cast<char>(std::toupper(static_cast<unsigned char>(ch))));
			else
				ofs.put(static_cast<char>(std::tolower(static_cast<unsigned char>(ch))));
		else
			ofs.put(ch);
}



Four score and seven years ago our
fathers brought forth on this continent
a new nation, conceived in liberty, and
dedicated to the proposition that all
men are created equal.


becomes as required:


FoUr ScOrE aNd SeVeN yEaRs AgO oUr
FaThErS bRoUgHt FoRtH oN tHiS cOnTiNeNt
A nEw NaTiOn, CoNcEiVeD iN lIbErTy, AnD
dEdIcAtEd To ThE pRoPoSiTiOn ThAt AlL
mEn ArE cReAtEd EqUaL.

Topic archived. No new replies allowed.