473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Read 2d Array from Text File-

Hi-
I have read many posts with specific applications of reading in text
files into arrays, however I have not been able to successfully modify
any for my application. I want to take a text file filled with a tab
delimited list of 10 columns (floats) and read it into a 2D array.
The length of the columns are all the same, however this will be
variable from text file to text file. Any help (starter code or where
to read) would be much appreciated. Thanks-
-Ryan

May 29 '07 #1
2 5496
RyanS09 wrote:
Hi-
I have read many posts with specific applications of reading in text
files into arrays, however I have not been able to successfully modify
any for my application. I want to take a text file filled with a tab
delimited list of 10 columns (floats) and read it into a 2D array.
The length of the columns are all the same, however this will be
variable from text file to text file. Any help (starter code or where
to read) would be much appreciated. Thanks-
-Ryan
Read them lines into a vector of vectors. Once read, you can extract
the elements and place them in a newly created 2d array if you want or
just keep the vector of vectors around.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 29 '07 #2
"RyanS09" <Ry*****@gmail.comwrote in message
news:11*********************@p77g2000hsh.googlegro ups.com...
Hi-
I have read many posts with specific applications of reading in text
files into arrays, however I have not been able to successfully modify
any for my application. I want to take a text file filled with a tab
delimited list of 10 columns (floats) and read it into a 2D array.
The length of the columns are all the same, however this will be
variable from text file to text file. Any help (starter code or where
to read) would be much appreciated. Thanks-
This is somewhat trivial and may be homework. There are a few ways to do
it, I decided to go ahead and code it up the way I would do it. Others may
use different methods ( InputFile >SomeFloat instead of a string, for
example).

Code was tested and ran with a simple data file of 10 floating point values
per line. It displayed the appropriate error message if a line had too many
or too less values. A possible optimization (which I would probably do if
this was in production) would be to push an empty vector<floatto data,
grab an iterator to it and push the data into that, rather than pushing the
full vector which requires a copy of the values. Pre-sizing the vector may
help, but I think that 10 floats is less than the default allotment anyway
(didn't bother to test since I'm not going to be using this code).

#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>

static const int LineLength = 10;

void Pause()
{
std::string Wait;
std::getline( std::cin, Wait );
}

int main()
{
std::ifstream InputFile( "testinput.txt" );
if ( ! InputFile.is_open() )
{
std::cout << "Error opening testinput.txt!" << std::endl;
Pause();
return 1;
}

std::vector< std::vector< float Data;

std::string Input;
while ( std::getline( InputFile, Input ) )
{
// Input contains entire line
std::stringstream Parse;
Parse << Input;
float Value;
std::vector<floatLine;
while ( Parse >Value )
Line.push_back( Value );
if ( Line.size() != LineLength )
{
std::cout << "Line " << Data.size() + 1 << " expected " <<
LineLength << " values, received " << Line.size() << " values, aborting." <<
std::endl;
Pause();
return 1;
}
Data.push_back( Line );
}

// close not specifically needed, will be called by destructor when
// ifstream goes out of scope, but I always call it anyway.
InputFile.close();
// At this point, all read values are in our vector of vector of floats
named Data.
std::cout << "Data:\n";
for ( size_t i = 0; i < Data.size(); ++i )
{
for ( size_t j = 0; j < Data[i].size(); ++j )
std::cout << Data[i][j] << " ";
std::cout << "\n";
}

Pause();

return 0;
}

May 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190,...
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
9
by: srikanth | last post by:
i have a text file like below, test.txt file (actually my test file file is with 10000 lines but here i tested with 3 lines) 3 06.09.2006 16:37:25 3 06.09.2006 16:40:02 3 06.09.2006 16:42:31...
6
by: xdeath | last post by:
Hi guys, i've currently got an assignment, whereby, im supposed to create 2 classes, a Vehicle superclass, and a Taxi subclass. Vehicle class needs to have Reg Number, model, price, and Taxi is...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.