Solved

Reading and Array from a text file

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 39
getting a fire fox page (view page source ) in c++ 6 45
c++ syntax question 9 58
Eclipse IDE - Cannot copy/paste from console output 8 325
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

696 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