tiodr Posted April 2, 2011 Report Share Posted April 2, 2011 (edited) Salut baieti:)Am si eu o problema. Nu stiu cum sa citesc din fisiere in C++ daca cifrele care trebuie atribuite unui array sunt despartite de un caracter, ex:6*4*2*2*0*3*5*9*5sau daca cifrele / numerele sunt puse pe alt rand, ex:141782417844142890741890584375486655980134980153Imi puteti posta si mie codurile pentru cele doua exemple ?Va multumesc frumos! Edited April 2, 2011 by tiodr Quote Link to comment Share on other sites More sharing options...
tiodr Posted April 2, 2011 Author Report Share Posted April 2, 2011 daca citesc normal pentru exemplul cu caractere intre cifre imi ia totul puna la primul caracter strain , in cazul acesta * si daca citesc direct pentru exemplul cu numerele despartite pe mai multe linii imi ia doar prima linie din fisier Quote Link to comment Share on other sites More sharing options...
alexalghisi Posted April 2, 2011 Report Share Posted April 2, 2011 incearca sa citesti fiecare cifra char Quote Link to comment Share on other sites More sharing options...
Patrunjel Posted April 2, 2011 Report Share Posted April 2, 2011 pentru ala cu stelute intre numere, de ce nu bagi un while, si sa citesti ba un int, ba un char ( charu nu conteaza, scapi de el, merge in ideea ca dai "cursoru" mai incolo )Daca nu, baga tot intr-un string si lucrezi direct de pe el Quote Link to comment Share on other sites More sharing options...
cifratorul Posted April 2, 2011 Report Share Posted April 2, 2011 ai incercat si cu scanf("%d*", ...) sau cu scanf("%d\n", ...) ? Quote Link to comment Share on other sites More sharing options...
tiodr Posted April 2, 2011 Author Report Share Posted April 2, 2011 am incercat doar cu cout si cin dar voi incerca cu astea sa vad daca mergeps: multumesc cifratorul, tromfil, Patrunjel, alexalghisi Quote Link to comment Share on other sites More sharing options...
cmiN Posted April 2, 2011 Report Share Posted April 2, 2011 C like#include <cstdio>#include <cstdlib>#include <cctype>typedef short unsigned int sui;int main(){ char fname[64], chr; unsigned long i, cnt = 0; sui* buffer = 0; printf("Enter file path/name: "); scanf("%s", fname); FILE* fin = fopen(fname, "rt"); while (!feof(fin)) { chr = fgetc(fin); if (isdigit(chr)) { buffer = (sui*) realloc(buffer, (cnt + 1) * sizeof(sui)); buffer[cnt++] = chr - '0'; } } fclose(fin); for (i = 0; i < cnt; i++) { printf("%u", buffer[i]); } free(buffer); return 0;}or C++#include <iostream>#include <fstream>#define n 64using namespace std;void compute(char* buffer){ char c; unsigned long rh, wh; rh = wh = 0; while ((c = buffer[rh++])) { if (c >= '0' && c <= '9') { buffer[wh++] = c; } } buffer[wh] = 0;}int main(){ char fname[n], * buffer; long unsigned size; fstream myfile; cout << "Enter file path/name: "; cin.getline(fname, n); myfile.open(fname, ios::in | ios::binary | ios::ate); size = (long unsigned) myfile.tellg(); buffer = new char[size + 1]; myfile.seekg(0, ios::beg); myfile.read(buffer, size); myfile.close(); buffer[size] = 0; compute(buffer); cout << buffer; delete[] buffer; return 0;}In al doilea exemplu l-am lasat de tip char nu l-am mai facut int/short ca sa-l afiseze direct dar de acolo te descurci nu-i problema. Quote Link to comment Share on other sites More sharing options...