LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-08-2006, 07:27 AM   #1
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Reading in data in columns from a file (C++)


Hi people, I have a file with some data in columns (separated by tabs) like so:

Code:
1.23	1.354	2.78
1.44	10.023	2.756
2.31	20.3	10.07
What I need to do is read each line of the file in and put each column in an array/vector (i.e. the first number in each line should go in one array, second number in each line in another, third number in yet another array, if that makes sense). I found a similar thread using strtok() (though the data was delimited by commas, not tabs) here, but I don't know how to get each line read into a char[] for strtok(). I can read into a string, though, but then using string's c_str() gives me a const char*.

Any ideas on how to do what I need?

Thanks!
 
Old 03-08-2006, 08:27 AM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
well if you know the structure of the file is always going to be like that, you can use the stream extractor >> operator with ifstream

ex
Code:
double cola, colb, colc;
ifstream in(file);
while(!in.eof()){
  in >> cola;
  in >> colb;
  in >> colc;
}
 
Old 03-08-2006, 09:09 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
<edit> My browser hadn't refreshed so I didn't see xhi's post.
</edit>
Please not this code expects you to know the number of cols and rows and does not delete the memory, but it shows you how to do what you ask.
Code:
#include <fstream>
#include <iostream>
#include <string>
float** read_file(std::string filename,int rows,int cols)
{
	std::fstream file;
	file.open(filename.c_str(), std::ios::in);
	if(!file.is_open()){return 0;}

	float** floats = new float*[cols+1];
	for(int i = 0; i <cols;++i){ floats[i] = new float[rows+1]; }

	//read each row
	for(int i =0;i<rows;++i)
	{
		for(int j =0;j<cols;++j)//push into the col
		{ file >>floats[j][i]; }
	}
	file.close();

	return floats;
}

int main()
{
	int rows = 3;
	int cols = 3;
	float** floats;
	if( !(floats = read_file("test",rows,cols) ) ){return 0;}

	//write out the data
	for(int i =0;i<rows;++i)
	{
		for(int j =0;j<cols;++j){ std::cout <<floats[j][i] <<"\t"; }
		std::cout <<"\n";
	}
	char wait;
	std::cin >>wait;

	return 0;
}
test
1.23 1.354 2.78
1.44 10.023 2.756
2.31 20.3 10.07

output
1.23 1.354 2.78
1.44 10.023 2.756
2.31 20.3 10.07

Last edited by dmail; 03-08-2006 at 09:11 AM.
 
Old 03-08-2006, 02:28 PM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Original Poster
Rep: Reputation: Disabled
Thank you both for the replies.

xhi, your example does pretty much what I need (with a slight modification to use arrays and stuff, but that was easy enough for me to do). I don't know why I thought that doing that wouldn't work. I was trying to use getline() and stuff.

dmail, I didn't really understand your code to be honest and I've never had to use double pointers before (I assume that's what you call them, but obviously I could be wrong). However, thank you again for taking the time to write it as it is appreciated!
 
Old 03-08-2006, 05:55 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Good to hear it that you have have sorted Nylex. I would never have written the code if I had seen xhi's post, but maybe i should add some comments so that anyone in future can understand it.

Code:
#include <fstream>
#include <iostream>
#include <string>

/*param filename :the name of the file to open
param rows       :number of rows in the file data
param cols       :number of columns in the files data
return float**   :a 2d float array holding the read in values
			or NULL on failure.*/
float** read_file(std::string filename,int rows,int cols)
{
	std::fstream file;//create a stream for the file
	file.open(filename.c_str(), std::ios::in);//open the file to read in
	if(!file.is_open()){return 0;}//if the file failed to open return NULL

        //float** is just a 2d array like float values[cols][rows]
	//create the column pointers
	float** floats = new float*[cols+1];
	
	//create the row pointers
	for(int i = 0; i <cols;++i){ floats[i] = new float[rows+1]; }

	//read each through each row
	for(int i =0;i<rows;++i)
	{
		//read the values in this row and push into the correct
		//column.floats is [cols][rows]
		for(int j =0;j<cols;++j)//push into the col
		{ file >>floats[j][i]; }
	}
	file.close();//close the file

	return floats;//return the 2d array
}

int main()
{
	int rows = 3;//number of rows in the file
	int cols = 3;//number of columns in the file
	float** floats;//2d array
	
	/*the func read_file returns null on failure, so set floats to the 
	value returned (which should be a 2d array) and check for failure.
	if the function failed exit main.*/ 
	if( !(floats = read_file("test",rows,cols) ) ){return 0;}

	//write out the data reading through the columns and rows
	for(int i =0;i<rows;++i)
	{
		for(int j =0;j<cols;++j){ std::cout <<floats[j][i] <<"\t"; }
		std::cout <<"\n";
	}
	char wait;
	std::cin >>wait;

	return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a command to merge two files as two columns of one file? davee Linux - General 2 07-19-2005 10:52 AM
Reading audio data files as an actual audio file? Erik_the_Red Linux - Software 1 06-01-2005 07:22 AM
Reading comma-separated data from file MeLassen Programming 4 04-04-2004 02:41 PM
Reading data from file (field organizzation) eiem Programming 1 03-29-2004 05:03 AM
reading data from a file afrm Programming 2 12-16-2003 07:43 PM


All times are GMT -5. The time now is 05:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration