adding input file into a matrix

I

isaac2004

hello i am trying to read an input file into a matrix, i have the
following setup to get the file

#include <iostream>
#include <string>
#include <fstream>
using namespace std;


int main()
{
ifstream file;
string name;

cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();

while (!file.fail()) {
cout << c;
c = file.get();
}

file.close();

return 0;
}

The first line of the file are values that I want to grab as
variables, but I dont know an east method to do this. Then for the
rest of the file, I would like to store it in a two dimensional array.
How would I do this in C++. Thanks for the help
What I am trying to do is
 
C

Christopher Pisz

isaac2004 said:
hello i am trying to read an input file into a matrix, i have the
following setup to get the file

#include <iostream>
#include <string>
#include <fstream>
using namespace std;


int main()
{
ifstream file;
string name;

cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();

while (!file.fail()) {
cout << c;
c = file.get();
}

file.close();

return 0;
}

The first line of the file are values that I want to grab as
variables, but I dont know an east method to do this. Then for the
rest of the file, I would like to store it in a two dimensional array.
How would I do this in C++. Thanks for the help
What I am trying to do is

Depends what the files looks like. the fstream operator >> should work if
your values are seperated by whitespace in the file. Handy file parsing
tools are std:::fstream, std::string, and std::stringstream. I'd start by
examing a reference on those.
 
I

isaac2004

Depends what the files looks like. the fstream operator >> should work if
your values are seperated by whitespace in the file. Handy file parsing
tools are std:::fstream, std::string, and std::stringstream. I'd start by
examing a reference on those.

well i can grab the first line, i just want to know how to bring in
the text and put it into a 2d array. i know you should use like
char ** in_array
in_array = new *int[height];

height and width are variables gotten to create a dynamic array. from
here i dont know how to do the looping construct. are there any
pointers on how to do this like

while infile.eof or

for (int i = 0; i < Rows; i++) {
in_array = new int[Cols];
}

thank you for the help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top