?
Solved

Reading and Array from a text file

Posted on 2002-05-08
12
Medium Priority
?
222 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 800 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
Industry Leaders: 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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