How to get my Program to read every 6th line of data

I have an input file with data the looks like this:

tradedt,open,high,low,close,volume
02/01/2012,5.59,5.59,5.59,5.59,0
03/01/2012,5.71,5.88,5.71,5.76,1674800

and I had it separate every value into it's own line like this:
02/01/2012
5.59
5.59
5.59
5.59
0
03/01/2012
5.71
5.88
5.71
5.76
1674800

I'm trying to figure out how I can get it to read only every 6th value so I can take the average of all those values. Any help would be awesome
Is this data written in a binary file?
You could try this ONLY if it is binary
1
2
3
4
5
6
7
8
9
10
 
ifstream f("filename"); 
int i=0;
while(!f.eof())
{
 f.seekg(6+i,ios: :beg);
 //accept value according to data type 
//whatever manipulation you want to do
i+=6;
}
Last edited on
unfortunately no... It's in a csv file
Last edited on
read them all, but discard everything except every 6th value.
can i get an example of this? I'm new to coding and I have no idea what I'm doing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include <fstream>

bool nth_line( std::istream& stm, std::string& tok, std::size_t n ) // returns false on failure
{
    while( n-- && std::getline( stm, tok ) ) ;
    return bool(stm) ;
}

std::vector< unsigned long long > every_nth_value( std::istream& stm, std::size_t n )
{
    std::vector< unsigned long long > result ;

    std::string tok ;
    while( nth_line( stm, tok, n ) )
    {
        std::size_t pos ;
        result.push_back( std::stoull( tok, std::addressof(pos) ) ) ;
        if( pos != tok.size() ) throw std::runtime_error( "badly formed input" ) ;
    }
    
    return result ;
}

int main()
{
    const std::string path = "trade.txt" ;
    std::ifstream file(path) ;
    const std::size_t n = 6 ;

    std::cout << "values in every 6th line ( as unsigned long long ):\n---------------\n" ;
    try{ for( long long v : every_nth_value( file, n ) ) std::cout << v << '\n' ; }
    catch( const std::exception& ) { std::cerr << "badly formed input\n" ; }
}
Last edited on
Didn't take time to document it, but this gives you everything you need to play with your data.

Edit* use your edited file > every value into it's own line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <fstream>
#include <iostream>
#include <string.h>
using namespace std;

string date;
double a;
double b;
double c;
double d;
int e;
int count=0;
int total=0;

int main (int argc, char *argv[])
{
	ifstream AFile ("test.txt");
    if (AFile.is_open())
	{
	while (AFile.good())
		{
		AFile >> date >> a >> b >> c >> d >> e;

		cout  << date << "\t" <<  a << "\t"<< b << "\t"<< c << "\t"<< d << "\t"<< e << endl;
		count++;
		total=total+e;
		}
		cout << "Average = " << total/count << endl;
    }
	else
	{
	        cout << "File not open" << endl;;
	}

AFile.close();
return 0;
}
Last edited on
Topic archived. No new replies allowed.