Reading Certain values from a file

how to read the first value only in a row

Page 1 of 1

7 Replies - 505 Views - Last Post: 01 December 2009 - 12:19 AM Rate Topic: -----

#1 regre12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-November 09

Reading Certain values from a file

Posted 30 November 2009 - 10:48 PM

Hi, I'm having trouble getting my program to input the values i need.

I have a list with 543 rows and 11 items in each row

ex.

1 4 6 4 6 3 1 3 7 6 4
1 4 6 4 2 4 6 7 8 9 9
3 4 5 6 1 2 6 8 9 5 3

For one of my arrays i need only the first column, so for that ex. i need to read 1 1 3 to use later in my program.

I have tried using loops, but I can only get it to read across the rows.

I understand that I need to use a loop to get the values, but I don't know exactly what kind of loop and how to set it up.

Any Help would be great!

Is This A Good Question/Topic? 0
  • +

Replies To: Reading Certain values from a file

#2 &rew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-November 09

Re: Reading Certain values from a file

Posted 30 November 2009 - 11:05 PM

This isn't the most efficient way of doing the task, but since there will always be 11 values in each row you can fetch the value that you need and then perform a dummy fetch 10 times after that. Put that in a loop and you will get the first column of values.
Was This Post Helpful? 0
  • +
  • -

#3 regre12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-November 09

Re: Reading Certain values from a file

Posted 30 November 2009 - 11:11 PM

View Post&rew, on 30 Nov, 2009 - 10:05 PM, said:

This isn't the most efficient way of doing the task, but since there will always be 11 values in each row you can fetch the value that you need and then perform a dummy fetch 10 times after that. Put that in a loop and you will get the first column of values.



Sorry, I'm very new to this stuff, but how do you perform a dummy fetch?
Was This Post Helpful? 0
  • +
  • -

#4 DeCompile  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 301
  • Joined: 20-July 08

Re: Reading Certain values from a file

Posted 30 November 2009 - 11:21 PM

Why not feed the data into a two dimensional array and read the columns that way.

Are the numbers delimited someway? Spaces / Commas / Semicolons.
Was This Post Helpful? 0
  • +
  • -

#5 regre12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-November 09

Re: Reading Certain values from a file

Posted 30 November 2009 - 11:24 PM

View PostDeCompile, on 30 Nov, 2009 - 10:21 PM, said:

Why not feed the data into a two dimensional array and read the columns that way.

Are the numbers delimited someway? Spaces / Commas / Semicolons.



I don't think I can use a two dimensional array, because I need each column in a parallel array, one array for each column.

This is my list ( http://lib.stat.cmu....ts/CPS_85_Wages )
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Reading Certain values from a file

Posted 30 November 2009 - 11:59 PM

why not? read the info into a 2 dimentional array first, then do your stuff -- it will make your life easier later on

this code is directed towards your example file
#include <iostream>
#include <fstream>

using namespace std;




int main()
{
	//load elements in multi-dimentional array first
	int grid [3][11];

	ifstream in("data.txt");

	for(int h=0; h<3; h++)
	{
		for(int w=0; w<11; w++)
		{
			in>>grid[h][w];
		}
	}

	// Then get first Column
	int *first = new int[3];

	for(int i=0; i<3; i++)
		first[i] = grid[i][0];
				 


				delete []first;

	




	

	cin.ignore();
	cin.get();

		return 0;
}



This post has been edited by ImaSexy: 01 December 2009 - 12:00 AM

Was This Post Helpful? 0
  • +
  • -

#7 regre12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-November 09

Re: Reading Certain values from a file

Posted 01 December 2009 - 12:07 AM

View PostImaSexy, on 30 Nov, 2009 - 10:59 PM, said:

why not? read the info into a 2 dimentional array first, then do your stuff -- it will make your life easier later on

this code is directed towards your example file
#include <iostream>
#include <fstream>

using namespace std;




int main()
{
	//load elements in multi-dimentional array first
	int grid [3][11];

	ifstream in("data.txt");

	for(int h=0; h<3; h++)
	{
		for(int w=0; w<11; w++)
		{
			in>>grid[h][w];
		}
	}

	// Then get first Column
	int *first = new int[3];

	for(int i=0; i<3; i++)
		first[i] = grid[i][0];
				 


				delete []first;

	




	

	cin.ignore();
	cin.get();

		return 0;
}






That helps a lot! thank you, but i didnt quite understand what the delete[] first was about? Also, in my list one column has decimals, i can just make the array a float right?
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Reading Certain values from a file

Posted 01 December 2009 - 12:19 AM

or a double, but yeah of course


this allocates memory for a pointer to an int array
int *first = new int[3];



and you have to delete to prevent memory leakage
delete []first;



you could simply not use a pointer and just put
double first[3];


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1