Reading a file.. trying to exclude some character

R

RishiD

Hi,

Trying to read an input file, of characters with spaces and carriage
returns. Basically it is a 20x20 matrix of characters with spaces. I
want to load the information into an two dimensional array.

Cannot seem to figure out how to do like if the read character equals a
space or carriage return, don't put it in the array.

Any ideas?

Thanks,

RishiD

ifstream fin;

string fileName = "sample";

char c;

// Open the file associated with stream fin
fin.open(fileName.c_str());

int x = 0, y = 0;
while (fin.get(c))
{
if (x == 20)
{
x = 0;
y++;
}
// if c != space or crlf ( i know the code below is completely
wrong, just trying to show what i want
if (c != " " || c != "\n")
{ dictArray[x][y] = c;
x++;
}
}
 
R

red floyd

RishiD said:
Hi,

Trying to read an input file, of characters with spaces and carriage
returns. Basically it is a 20x20 matrix of characters with spaces. I
want to load the information into an two dimensional array.

Cannot seem to figure out how to do like if the read character equals a
space or carriage return, don't put it in the array.

Any ideas?

Thanks,

RishiD

ifstream fin;

string fileName = "sample";

char c;

// Open the file associated with stream fin
fin.open(fileName.c_str());

int x = 0, y = 0;
while (fin.get(c))
{
if (x == 20)
{
x = 0;
y++;
}
// if c != space or crlf ( i know the code below is completely
wrong, just trying to show what i want
if (c != " " || c != "\n")
First, use && not ||. The above comparison (once it's made well formed
-- see second point) will always compare true. A character is always
(not blank or not newline).

Second, compare against chars (' ', '\n'), not against string literals.
{ dictArray[x][y] = c;
x++;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top