By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

adding input file into a matrix

P: n/a
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

Oct 12 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"isaac2004" <is********@yahoo.comwrote in message
news:11**********************@q5g2000prf.googlegro ups.com...
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.
Oct 13 '07 #2

P: n/a
On Oct 12, 10:07 pm, "Christopher Pisz" <some...@somewhere.netwrote:
"isaac2004" <isaac_2...@yahoo.comwrote in message

news:11**********************@q5g2000prf.googlegro ups.com...
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.
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[i] = new int[Cols];
}

thank you for the help

Oct 16 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.