Está en la página 1de 15

Archivo de Texto 1 C++

1.Abra un archivo para su lectura. Lea cada carácter del archivo y escriba en la
pantalla solo las letras. Luego cierre el archivo y salga.

#include <fstream> // ifstream, ofstream

#include <cassert> // assert()

#include <iostream>

using namespace std;

int main() {

char file_name[80];

char buffer[255]; // for user input

cout << "File name: ";

cin >> file_name;

ofstream fout(file_name); // establish connection,

assert( fout.is_open() ); // and check for success

cout << "Enter text for the file: ";

cin.ignore(1,'\n'); // eat the newline after the file name

cin.getline(buffer,255); // get the user's input

fout << buffer << "\n"; // and write it to the file

fout.close(); // close the file, ready for reopen


ifstream fin(file_name); // establish connection,

assert( fin.is_open() ); // and check for success

cout << "Here's the contents of the file (letter only):\n";

char ch;

while (fin.get(ch)) {

if ((65 <= ch && ch << 90) || (97 <= ch && ch << 122)) cout << ch;

cout << "\n***End of file contents.***\n";

fin.close(); // close the connection

return 0;

2.Abra un archivo para su lectura. Lea cada carácter del archivo y escriba en la
pantalla todos los caracteres excepto los espacios en blanco. Luego cierre el
archivo y salga.

#include <fstream> // ifstream, ofstream

#include <cassert> // assert()

#include <iostream>

using namespace std;

int main() {

char file_name[80];
char buffer[255]; // for user input

cout << "File name: ";

cin >> file_name;

ofstream fout(file_name); // establish connection,

assert( fout.is_open() ); // and check for success

cout << "Enter text for the file: ";

cin.ignore(1,'\n'); // eat the newline after the file name

cin.getline(buffer,255); // get the user's input

fout << buffer << "\n"; // and write it to the file

fout.close(); // close the file, ready for reopen

ifstream fin(file_name); // establish connection,

assert( fin.is_open() ); // and check for success

cout << "Here's the contents of the file (letter only):\n";

char ch;

while (fin.get(ch)) {

if (ch != 32) cout << ch;

cout << "\n***End of file contents.***\n";

fin.close(); // close the connection


return 0;

3.Dado un nombre de archivo y una palabra, escriba en la pantalla cada línea


que contiene esa palabra anteponiendo el numero de línea. Sugerencia:
getline().

#include <fstream> // ifstream, ofstream

#include <cassert> // assert()

#include <iostream>

#include <cstring>

using namespace std;

int main() {

char file_name[80];

char buffer[255];

char word[20]; // for user input

char* ptr;

int count = 0;

cout << "File name: ";

cin >> file_name;

cout << "Word: ";

cin >> word;


ifstream fin(file_name); // establish connection,

assert( fin.is_open() ); // and check for success

cout << "Here's the contents of the file (letter only):\n";

while (fin.getline(buffer,255,'\n')) {

ptr = strstr(buffer, word);

++count;

if (ptr) cout << count << "\t:\t" << buffer << endl;

cout << "\n***End of file contents.***\n";

fin.close(); // close the connection

return 0;

4.Concatene dos archivos de texto, es decir, agregue un archivo al final del


otro.

#include <fstream> // ifstream, ofstream

#include <cassert> // assert()

#include <iostream>

using namespace std;

int main() {
char file_name_1[40];

char file_name_2[40];

char ch = ' ';

cout << "File name 1: ";

cin >> file_name_1;

cout << "File name 2: ";

cin >> file_name_2;

ofstream fout(file_name_1, ios::app); // establish connection,

assert( fout.is_open() ); // and check for success

ifstream fin(file_name_2); // establish connection,

assert( fin.is_open() ); // and check for success

while (fin.get(ch)) {

fout << ch;

fout.close(); // close the file, ready for reopen

fin.close(); // close the connection

return 0;

5.Lea un archivo de texto y escriba toda su contenido en min ́uscu-


la en otro archivo de texto.

#include <fstream> // ifstream, ofstream

#include <cassert> // assert()

#include <iostream>

#include <string.h>

using namespace std;

int main() {

char ch = ' ';

ofstream fout("hola.txt", ios::out); // establish connection,

assert( fout.is_open() );

// and check for success

ifstream fin("ala.txt",ios::in); // establish connection,

assert( fin.is_open() ); // and check for success

while (fin.get(ch)) {

ch=toupper(ch);

fout <<ch;

cout<<ch;

}
fout.close(); // close the file, ready for reopen

fin.close(); // close the connection

return 0;

6.Lea un archivo de texto y escriba toda su contenido en sentido

invertido en otro archivo de texto.

#include <fstream> // ifstream, ofstream

#include <cassert> // assert()

#include <iostream>

#include <string.h>

using namespace std;

int main() {

char file_name_1[40];

char file_name_2[40];

char ch = ' ';

string texto;

string texto1;

cout << "File name 1: ";


cin >> file_name_1;

cout << "File name 2: ";

cin >> file_name_2;

ifstream fout(file_name_1, ios::in); // establish connection,

assert( fout.is_open() ); // and check for success

ofstream fin(file_name_2,ios::out); // establish connection,

assert( fin.is_open() ); // and check for success

while (!fout.eof()) {

getline(fout,texto);

for(int i=0;i<texto.length();i++){ texto1[i]=toupper(texto[texto.length()-i] )


;cout<<texto1[i];fin<<texto1[i];}

fout.close(); // close the file, ready for reopen

fin.close(); // close the connection


return 0;

7.Lea el archivo de texto final.txt y cuente las ocurrencias

en el archivo de una palabra especificada ingresada durante la ejecuci ́on del

programa.

#include <fstream> // ifstream, ofstream

#include <cassert> // assert()

#include <iostream>

#include <cstring>

using namespace std;

int main() {

char file_name[80];

char buffer[255];

char word[20]; // for user input

char* ptr;

int count = 0;

cout << "File name: ";

cin >> file_name;

cout << "Word: ";

cin >> word;


ifstream fin(file_name); // establish connection,

assert( fin.is_open() ); // and check for success

cout << "Here's the contents of the file (letter only):\n";

while (fin.getline(buffer,255,'\n')) {

ptr = strstr(buffer, word);

if (ptr){ ++count ;}

}cout<<word<<count;

cout << "\n***End of file contents.***\n";

fin.close(); // close the connection

return 0;

8.Lea un archivo de texto y cuente los caracteres en cada l ́ınea.

El programa debe mostrar el n ́umero de l ́ınea y la longitud de las l ́ıneas m ́as

cortas y m ́as largas del archivo, as ́ı como el n ́umero promedio de caracteres


por

l ́ınea.

#include <fstream> // ifstream, ofstream

#include <cassert> // assert()

#include <iostream>
#include <cstring>

using namespace std;

int main() {

char file_name[80];

char buffer[255];

char word[20]; // for user input

char* ptr;

int count = 0;

int p;

int v[3];

int k=0;

cout << "File name: ";

cin >> file_name;

ifstream fin(file_name); // establish connection,

assert( fin.is_open() ); // and check for success

cout << "Here's the contents of the file (letter only):\n";

while (fin.getline(buffer,255,'\n')) {

p=strlen(buffer);

v[k]=p;
++count;

k++;

cout << count << "\t:\t" << buffer <<p<< endl;

for(int i=0;i<3;i++){

cout<<v[i];

cout << "\n***End of file contents.***\n";

fin.close(); // close the connection

return 0;

9.Copie un archivo de texto en otro archivo de texto en el que

las l ́ıneas est ́en numeradas 1, 2, 3, . . . con un n ́umero a la izquierda de cada l


́ınea.

#include <fstream> // ifstream, ofstream

#include <cassert> // assert()

#include <iostream>

#include <cstring>

using namespace std;


int main() {

char file_name[80];

char buffer[255];

char word[20]; // for user input

char* ptr;

int count = 0;

int p;

int v[3];

int k=0;

cout << "File name: ";

cin >> file_name;

ifstream fin(file_name); // establish connection,

assert( fin.is_open() ); // and check for success

ofstream fout("ana.txt");

assert( fout.is_open() );

cout << "Here's the contents of the file (letter only):\n";

while (fin.getline(buffer,255,'\n')) {

++count;

fout<<count;
fout<<buffer;

fout<<endl;

cout << count << "\t:\t" << buffer <<endl;

cout << "\n***End of file contents.***\n";

fin.close(); // close the connection

fout.close();

return 0;

También podría gustarte