could i get help with the syntex on reading a file to an array in c++. all the examples i see are more

While you're at it could you give me the recipe to my favorite drink, too? It's just about as easy for us to help you as it is for you to help me. Be more specific, and post pertinent information.

Member Avatar for Kevin_160

I will try to help you, but it is common that you post the code you already have.

#include <iostream>
#include <string>

using namespace std;

int main(){
cout << "Enter filename: "<<"\n";
cin.getline(input,20);

fstream text(InputFile.c_str(),ios::in);
  
if (text.fail())
  {
    cout << "File not found: " << InputFile <<endl;  
    return 0;
  }
 
// If you want to write the result to another txt file
ofstream myfile;
 myfile.open("RENAME.txt",ios::out);
  
  
   // to check wether the file is correctly opened we include bool is_open(text)
  // this will return a bool of type true if the file is correctly opened, else false
  
   if(! text.is_open())
    { cout << "Error opening file"; exit (1);}
    // exit(1) will stop the programm 

  if (text.is_open())
  {
    while(! text.eof() ) //(getline(text,line) )
    {

// Here it depends what you want to do
// If you want to pars a part of the text
            getline(text,line);// read each time one line
            std::string::size_type pos = line.find("the string you want");
            if(pos != std::string::npos )  // npos = -1
//                       ....

//  To put your values into a 2D array:

double m** = make_matrix(5,6);// 5 lines, 6 columns

    }
}
return 0;
}

double** make_matrix()
	          {
	                  a = (double**)calloc(Row,sizeof(double*));
	                  for (i = 0; i < Row; i+= 1)
	                          a[i] = (double*)calloc(Col,sizeof(double));
	                  return a;
	          }
	double**        a;
        int Row, int Col;

I think this will help you. If you have further questions, try to state the problem.

Greetz,

Kev

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.