Hi everyone
Im just a new student in c++ and im trying to read data from a file
this file looks like

36 69 115 226 278 343 345 358 368 370 401 450 489 494 573 577 581 583 610 682 692 705 722 832 862 886 908 923 932 960 977
8 51 55 73 78 117 140 175 187 229 266 295 304 366 381 413 424 429 501 512 523 529 538 572 575 576 593 675 676 688 735 758 785 797 812 823 826 843 854 868 871 888 893 956 982
53 55 98 159 192 322 332 402 412 413 424 430 450 480 526 538 569 571 572 598 666 672 694 701 797 809 820 826 897 904 928 943 952 956 992

i want to put the items in the first column of the array for example in [0][0], [0][1]...etc
than i want to put the number of the line for each item in the second column of the array
i tried milloin time to do it but still confused with
my code is
and its messy right now
does anyone can help me to solve this problem?
any suggestions will be appreciated

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include<iomanip>
using namespace std ;
 

const long items=1000;
const long lines=100000;
long item_lines[items][lines];// array to save the item with its line
long i=0; // varibles for items
long j=0; // varibale for lines
int main()
 {
	std::string buffer;
	int m=1;
	int num;
	int nu_lines; // varible for number of lines
    // variable for input value
 ifstream file( "Ayman.dat") ;
   string line ;
	 vector<string> lines ;
	 while( getline(file, line ))
		 lines.push_back( line ) ;
	 //cout << "#lines: " << lines.size() << '\n' ;
	 nu_lines = lines.size();
	 cout<< "#lines: "<<nu_lines<<endl;
	 file.close();
//////////////////////////////////////////////////////////////////////////////////////////////////////
   ifstream indata; // indata is like cin
   indata.open("Ayman.dat"); // opens the file
   if(!indata) { // file couldn't be opened
      cout << "Error: file could not be opened" << endl;
      exit(1);
   }
  indata >> num;
  while(!indata.eof() ) 
  { // keep reading until end-of-file  
		  item_lines[i][j]=num;
		  j++;
		  item_lines[i][j]=m;
		  i++;
		  j--;
		    indata >> num; // sets EOF flag if no value found
  }
		
   indata.close();
   cout<< "#lines: "<<m<<endl;
   cout << "the number of items is   " << i<<endl;
				 for(int k=0; k<i; k++)
						 cout<<item_lines[k][j]<<"   in line "<<item_lines[k][j+1]<<endl;

   return 0;

 }

why do you need a 1000X10000 array?

Another way to do that and simlify it is to create a structure then make an array of structures

struct item
{
   vector<int> numbers;
   int linenum;
};

vector<item> items;

i need the array to put the items in the first column but i have huge files
this one is just few lines but in my project we have 3 files and the smallest one has almost 10000 lines and the large one has 100000 lines
also i have no idea about vectors and how can i use them im my code

You realize that 1000x100000 array occupies 400,000,000 bytes ( or 400 megabytes) That's a pretty huge array and will probably slow down your program to a crawl.

Lean to use std::vector, its not all that difficult. std::vector will only use the amount of space actually needed. Learn to think outside the box occasionally.

:O
that's really huge array i wasnt aware for this point
I will try to fix it by the way you told me
thanx again

> i want to put the number of the line for each item in the second column of the array

You really need not store the line number separately; the row number of a two-dimensional array (or array-like structure) would become the implicit line number.

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

int main()
{
    // for a brief tutorial on using std::vector<>
    // see http://www.cprogramming.com/tutorial/stl/vector.html
    std::vector< std::vector<int> > vec2d ;

    std::ifstream indata( "Ayman.dat" ) ;
    if( !indata ) // file couldn't be opened
    {
        std::cerr << "Error: file could not be opened\n" ;
        return 1 ;
    }

    std::string line ;
    // To read a complete line of text into a std::string, use std::getline.
    // see: http://www.cplusplus.com/reference/string/getline/
    while( std::getline( indata, line ) ) // keep reading until end-of-file
    {
        // create an input stringstream to read ints from line
        // see: http://www.fredosaurus.com/notes-cpp/strings/stringstream.html
        std::istringstream stream(line) ;

        // create a pair of iterators to iterate over the ints in the stream
        // about iterators: http://www.mochima.com/tutorials/STL.html
        // http://www.cplusplus.com/reference/std/iterator/istream_iterator/
        std::istream_iterator<int> begin(stream), end ;

        // read the the int values in the stream into a vector, and
        // add the vector<> of ints to vec2d
        vec2d.push_back( std::vector<int>( begin, end ) ) ;
    }

    // vec2d now contains the ints from the file, number of lines is vec2d.size()
    // vec2d[0] - vector of ints from line 0, number of ints are vec2d[0].size()
    // vec2d[1] - vector of ints from line 1, number of ints are vec2d[1].size()
    // etc
    // ...
}

thanx my dears
I found out how to use vectors
vijayan121 thanx alot for ur comment it was helpful

wish to learn more from u guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.