Jump to content
Timelord

Citire matrice caractere din fisier C++

Recommended Posts

Posted

Se da o matrice de caractere pe mai multe linii(liniile si coloanele au mereu aceeasi lungime).Am aflat numaraul de linii,coloane,iar cand am incercat sa le introduc intr-o matrice si sa le afisez imi arata toate linii,mai putin ultima din care imi afiseaza aproximativ jumatate.Sugestii legate de aceasta problema?

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;
ifstream TestIn("test");


int main()
{

char Grid[1000][1000],s[1000];

int LungimeX,LungimeY,i,j;

TestIn.getline(s,1000);
//aflam lungimea randurilor/numarul coloanelor/lungimea X
LungimeX=strlen(s);
cout<<LungimeX<<endl;

//aflam lungimea colonelor/numarul randurilor/lungimea Y
while (!TestIn.eof()) {
TestIn.getline(s,1000);
LungimeY++;}
cout<<LungimeY;

//resetam flagul .eof pentru reciti fisierul
TestIn.clear();
TestIn.seekg (0, TestIn.beg);

//introducem gridul intr-o matrice
for(i=1;i<=LungimeY;i++){
for(j=1;j<=LungimeX;j++){
TestIn.get(Grid[i][j]);
}}

for(i=1;i<=LungimeY;i++){
for(j=1;j<=LungimeX;j++){
cout<<Grid[i][j];
}}



return 0;
}

Posted (edited)

Variabila LungimeY nu este setata corect.

Numerotarea liniilor se face de la 0 , deci daca matricea ta va avea 3 randuri, variabila LungimeY va avea valoarea 2.

Modifica cele 2 for-uri astfel incat sa incluzi toate liniile:

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

{

for(j=0;j<=LungimeX;j++)

{

TestIn.get(Grid[j]);

}

}

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

{

for(j=0;j<=LungimeX;j++)

{

cout<<Grid[j];

}

}

Edited by dekeeu
Posted (edited)

In primul rand seteaza LungimeY sa porneasca de la 1 sau pune TestIn.seekg (0, TestIn.beg); dupa ce gasesti numarul coloanelor.

In al doilea rand, tie o sa-ti ia si caracterele \n cand folosestiTestIn.get(). Trebuie sa le ignori pe alea.

Deci codul o sa arate ceva de genu:

#include <iostream>

#include <fstream>

#include <cstring>

using namespace std;

ifstream TestIn("test");

int main()

{

char Grid[1000][1000],s[1000];

int LungimeX,LungimeY,i,j;

TestIn.getline(s,1000);

TestIn.seekg (0, TestIn.beg);

//aflam lungimea randurilor/numarul coloanelor/lungimea X

LungimeX=strlen(s);

cout<<LungimeX<<endl;

//aflam lungimea colonelor/numarul randurilor/lungimea Y

while (!TestIn.eof()) {

TestIn.getline(s,1000);

LungimeY++;}

cout<<LungimeY;

//resetam flagul .eof pentru reciti fisierul

TestIn.clear();

TestIn.seekg (0, TestIn.beg);

//introducem gridul intr-o matrice

for(i=1;i<=LungimeY;i++){

for(j=1;j<=LungimeX;j++){

TestIn.get(Grid[j]);

if ((int)Grid[j] == 10)

TestIn.get(Grid[j]);

}}

for(i=1;i<=LungimeY;i++){

for(j=1;j<=LungimeX;j++){

cout<<Grid[j];

}}

return 0;

}

Edited by Byte-ul

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...