After entering a file name, I need to read the contents of that file into an array. I am having trouble reading one integer at a time from the file. In the text file I will be reading, the fist two numbers on the first line I want to assign as n=first#, m=second#. After that, I want to create a mx4 array given by the next m lines with 4 numbers per line. Once I know how to read one character at a time, I will be able to fill in my array.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
// Variables
int n,m;
string myfile;
ifstream testfile;
int main()
{
cout << "Enter file name: ";
cin >> myfile; //read in file name
testfile.open(myfile.c_str()); //open file
if (!testfile) //test to see if file is open
{
cout << "Error opening file\n";
system("pause");
return -1;
}
while (!testfile.eof()) //while file is open and not at the end
{
// n = first # of first line in testfile;
// m = second # of first line in testfile;
int array[m][4];
// array[0][0] = first # of second line in file
// array[0][1] = second # of second line
// array[0][2] = third # ...
// array[1][0] = first # of third line in file
//...
}
testfile.close();
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
// Variables
int n,m;
string myfile;
ifstream testfile;
int main()
{
cout << "Enter file name: ";
cin >> myfile; //read in file name
testfile.open(myfile.c_str()); //open file
if (!testfile) //test to see if file is open
{
cout << "Error opening file\n";
system("pause");
return -1;
}
else
{
testfile >> n >> m;
int ForwardStar[m][4]; // Create a m x 4 matrix
for (int i=0;i<m;i++)
for (int j=0; j<4; j++)
testfile >> ForwardStar[i][j];
}
testfile.close();
system("PAUSE");
return EXIT_SUCCESS;
}