Jump to content
Skiry

Citire caractere

Recommended Posts

  • Active Members

OFF:Tot tu esti ala cu gura mare ? In primul rand, nu ai specificat nicaieri ca ar fii vorba de un fisier.

ON:

Pentru a citi linie cu linie:


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void main ()
{
string STRING;
ifstream infile;
infile.open ("fisier.txt");
while(!infile.eof) // pentru a parcurge toate liniile din fisier
{
getline(infile,STRING); // salvez liniile in STRING
cout<<STRING; // Afisez STRING.
}
infile.close();
system ("pause");
}

Edited by MrGrj
Link to comment
Share on other sites

Uite si varianta mea:

#include <iostream.h>

#include <fstream.h>

#include <string.h>

#include <conio.h>

fstream f("fin.txt", ios :: in);

fstream g("fout.txt", ios :: out);

char a[30];

int n;

void citire()

{

int i=0;

while(!f.eof())

{

f>>a;

i++;

}

n=i;

}

void reinitializare()

{

int i;

for(i=0; i<n; i++)

{

if(a == 'X')

a = '0';

else if(a == '.')

a = '1';

}

}

void afisare_1()

{

int i, j=0;

for(i=0; i<n; i++)

{

cout<<a<<" ";

j++;

if(j==5)

{

cout<<endl;

j=0;

}

}

}

void afisare_2()

{

int i, j=0;

for(i=0; i<n; i++)

{

g<<a<<" ";

j++;

if(j==5)

{

g<<endl;

j=0;

}

}

}

void main()

{

citire();

reinitializare();

afisare_1();

afisare_2();

}

Nu stiu daca ai inteles ce ti-a scris MrGrj, cel putin noi la scoala nu am invatat sa scriem asa...

Sfat: Citeste si prelucreaza informatiile folosind un vector pe care il afisezi ulterior ca pe o matrice, banuiesc ca e corect.

Sper ca te-am ajutat.

Link to comment
Share on other sites

@MARIUSCS sau: poti citi caracter cu caracter. Daca caracterul este X pui in matrice / stiva 0, daca este . pui 1.

Daca caracterul este "\n"(newline) treci la urmatoarea coloana (au introduci \n in stiva/vector.

Faci asta cat timp nu esti la eof.

(Varianta mai industriala).

Combinate:

Cinesti prima linie, vezi cate caractere are (n).

Parcurgi tot fisierul, faci inlocuirile (cum a dat MARIUSCS exemplu) si afisezi randurile de cate n.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...