Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

reading data into an array

$
0
0
Hello, so im trying to read an unknown number of numbers into an array, then do some math to those numbers. Im stuck on reading the data into an array...I cant quite figure out how to input the data into an array. can someone point me in the right direction? I've only included the part of the program that i'm having trouble with, if you need, i can post the entire thing


void functions::ReadData(int argc, char *argv[])
{
	int counter =0;
	char placeholder;
		
	std::ifstream InStream(argv[1]);
	
	while(!InStream.eof())
	{
	InStream >> placeholder;
	counter++;
	
    if (placeholder == '\n'|| placeholder == ' ')
	counter --;
	InStream.close();
    }
    //i open the file once todetermine size, then again to read data into array
    int array [counter] = 0;
    int * ptr = array;
    InStream.open(argv[1]);
    while (!InStream.eof())
    {
		InStream >> array;
		ptr++;
		//pointer arithmetic to chage address of array
	}
	InStream.close();
    
    
    for(int i = 0; i < counter; i++)
    {
		std::cout << array[i] << std::endl;
	}
		

}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>