Skiry Posted February 24, 2014 Report Posted February 24, 2014 Cum as putea sa citesc secventa asta :XXXXXX...XXXX.X.XXX.astfel incat dupa ce am citit sa modific "X" in 0 si "." in 1 sa zicem,adica sa pot avea acces la fiecare in parte. asa nu merge :for(i=1;i<=n;i++) for(j=1;j<=m;j++) f.get(a); a l-am declarat -> a[100][100]; Quote
Active Members MrGrj Posted February 24, 2014 Active Members Report Posted February 24, 2014 Poti fii mai neclar de atat ?Pune enuntul aici daca nu esti in stare sa-l reproduci prin propriile cuvinte + poate ne spui daca vrei C sau C++ . Quote
Skiry Posted February 24, 2014 Author Report Posted February 24, 2014 Cat de neclar am fost ? In fisierul de intrare am " XXXXX X...X XXX.X .XXX. "cum citesc aceasta secventa?C++. Quote
Active Members MrGrj Posted February 24, 2014 Active Members Report Posted February 24, 2014 (edited) 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 February 24, 2014 by MrGrj Quote
MARIUSCS Posted February 24, 2014 Report Posted February 24, 2014 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. Quote
yoyois Posted February 24, 2014 Report Posted February 24, 2014 @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. Quote