Remove line 27
At line 30 you are passing a C string, not a file stream. If is that the way you want your program to work you should change/overload it to this:
1 2 3
int matrix(char *file_name) {
ifstream iFile ( file_name );
//etc
By the way, why does it have int as return type if you always return 0 ?
You are never checking the number of arguments passed to your program so argv[1] can lead to problems
That should be because in function matrix_sz you read (and removed) all the character in the stream.
Try something like this:
1 2 3 4 5 6 7 8 9 10 11 12
void matrix_sz(istream &iFile) {
iFile.seekg(0,ios::end);//go to the end of the file
int c = 0, size = iFile.tellg();//get the size in bytes
iFile.seekg(0);//returnt to the beginning of the file
for (int p=0; p<size; p++)//p iterates all the possible character positions
{
if ( iFile.peek() == '\n' ) c++;// check for newline (peek doesn't remove characters from the stream)
iFile.seekg(p);//go to the position
}
cout << "Número de linhas: " << c+1 << endl;
iFile.seekg(0);//returnt to the beginning of the file
}
at the start of each function to make sure you can read the file in each function.
However, a better solution might be to read the data in to vectors once, then print the number of lines and the data that are in the vectors rather than re-reading the file multiple times.
I think this is what you should do.
Get the file character by character or read the entire file into memory (whichever you prefer) and:
To look for lines: Look for newlines. If between [two newlines or [between a newline and [the beginning or end of file]]] there are non-whitespace characters, then that's a line in the matrix. Stop at the EOF.
To look for columns: Look for non-newline whitespace. If between [two whitespaces or [a whitespace and [the beginning of file or a newline or the end of file]]] there are non-whitespace characters, then that's a column in the matrix. Stop at the first newline or at the EOF.
EDIT: Hmm... This may be exceedingly robust for this, though...
cstrieder: No, but you can read one element at a time and call push_back(). In your matrix_columns() function you would call v.push_back(n). Have you used vectors before?