Solved

Reading and Array from a text file

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

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

20 Experts available now in Live!

Get 1:1 Help Now