Solved

Reading and Array from a text file

Posted on 2002-05-08
12
221 Views
Last Modified: 2010-04-02
Hi,
How do I get information into an array (10 by 10 grid) from a text file?
The data in the text file can be set out any way.
Using straight c++.
Some code would be very greatful.

Thanks
0
Comment
Question by:dsteers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 2

Accepted Solution

by:
jonnin earned 200 total points
ID: 6996203
any way? any whitespace (tab, space, end of line)
is ok with this, but no code can read any format someone dreams up without knowing something about that format...

#include<fstream>
ifstream f;
f.open("whatever.txt",ios::nocreate); //visual needs
//nocreate, else creates empty file and reads from that!
double a[10][10];
for(int x = 0; x < 10; x++)
for(int y = 0; y < 10; y++)
f >> a[x][y];
f.close();


 
0
 

Author Comment

by:dsteers
ID: 6996293
No whitespace or end of line as far as i know yet.
I'll check it out and get back to you in a few days.
Thanks for your help so far.
0
 

Author Comment

by:dsteers
ID: 7011167
jonnin,
The text file looks as follows :
1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890

All on one continuious line without the space between them.
so it's : 12345678901234567890 etc (another 8 times).

I have a module to open the file and close the file, but to read the file I couldn't get your code to work.
It's most likely a problem at my end, so could you please run through it with me.

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dsteers
ID: 7011170
I've increased the points a little to be fair.
0
 

Author Comment

by:dsteers
ID: 7011180
I tried a "cout << a[x][y]" to display the code but to no avail.

Could you please help me out on this one too.
Thanks
0
 

Author Comment

by:dsteers
ID: 7011187
I need it displaying this Grid on output :

1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890

Thanks
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7012502
Hmmm, am I right then in thinking each number in the grid is a single digit number?

If so, you can just read the whole thing in then subtract '0' from each byte:


char xy[10][10];
int x, y;

...Open file as binary...

fs.read(xy, 100);
for(x = 0; x < 10; x++)


..Close file...
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7012503
Hmmm, am I right then in thinking each number in the grid is a single digit number?

If so, you can just read the whole thing in then subtract '0' from each byte:


char xy[10][10];
int x, y;

...Open file as binary...

fs.read(xy, 100);
for(x = 0; x < 10; x++)


..Close file...
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7012504
Hmmm, am I right then in thinking each number in the grid is a single digit number?

If so, you can just read the whole thing in then subtract '0' from each byte:


char xy[10][10];
int x, y;

...Open file as binary...

fs.read(xy, 100);
for(x = 0; x < 10; x++)


..Close file...
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7012506
Hmmm, am I right then in thinking each number in the grid is a single digit number?

If so, you can just read the whole thing in then subtract '0' from each byte:


char xy[10][10];
int x, y;

...Open file as binary...

fs.read(xy, 100);
for(x = 0; x < 10; x++)
    for(y = 0; y < 10; y++)
        xy[x][y] -= '0';

..Close file...
0
 
LVL 11

Expert Comment

by:griessh
ID: 7178705
Dear dsteers

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "jonnin"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 

Author Comment

by:dsteers
ID: 7181892
Sorry for the delay.
Thanks Again
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question