I have a text file with "$" chars only in it, I need to put this file into a 2D array[25][50], then count how many $ chars there are in the file, and output file and count to screen, any clues???Please help!!
Printable View
I have a text file with "$" chars only in it, I need to put this file into a 2D array[25][50], then count how many $ chars there are in the file, and output file and count to screen, any clues???Please help!!
I may not understand the assignment, but here goes.
Code:#include <cstdlib>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
char lines[25][50];
char *filename = "sample.txt";
int num_of_lines = 0;
int count = 0;
int i, j;
ifstream in(filename);
if (!in)
{
cout << "Could not open file:" << filename << endl;
return 0;
}
while (in.getline(lines[num_of_lines],50))
{
num_of_lines++;
}
for (i=0; i<num_of_lines; i++)
{
cout << lines[i] << endl;
for (j=0; j<strlen(lines[i]); j++)
if (lines[i][j] == '$')
count++;
}
cout << "$ found:" << count << endl;
return 0;
}
Forgot to close the file. Add this before the "return" at the end.
in.close();
Or before the for-loop.
This works excellent if I save file as txt file, but my file is a .dat file and it wont read file, I can't find why? any suggestions.
Many thanx swoopy
Ah, no probs....got it working now.