Solved

Reading and Array from a text file

Posted on 2002-05-08
12
217 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
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
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.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now