| Thread Closed |
program to read file |
Share Thread |
| Dec9-07, 04:54 AM | #1 |
|
|
program to read file
hi! i was writing a program which asks the user to input the file name. the program then reads the file and prints out how many times each characeter has occured in the file(uppercase and lowercase are different). Pls help me out.
this is what i have got-i know this is not correct. #include<iostream> #include<fstream> #include<stdlib.h> using namespace std; int main() { fstream file_ptr; char filename[81]; int a = 65; int e=0; char number[58]={0}; for(int i=0; i<58; i++) { number[i]=a; a++; } // Get name of file cout << "Enter filename : "; cin.getline(filename,81); // Open data file for READING cout << "Opening file "<< filename << endl; file_ptr.open(filename, ios::in); if (!file_ptr.good()) { //The file could not be opened cout << "ERROR - file " << filename << " could not be opened"; exit(1); } // READ data from file until the end-of-file (EOF) is reached. do { file_ptr >> number; cout << "Read " << number << " from file." << endl; } while(file_ptr.peek()!=EOF); // Close file cout << "Closing file " << filename << endl; file_ptr.close(); system("pause"); return 0; } |
| Dec9-07, 05:56 AM | #2 |
|
|
Code:
char filename[81]; Code:
int a = 65; Code:
int e=0; Code:
char number[58]={0};
Code:
for(int i=0; i<58; i++)
{
number[i]=a;
a++;
}
Code:
cin.getline(filename,81); Code:
do {
file_ptr >> number;
cout << "Read " << number << " from file." << endl;
} while(file_ptr.peek()!=EOF);
|
| Dec9-07, 06:02 AM | #3 |
|
|
hmm, man, I hate C++. C++ is way too overloaded (it's a joke). Try placing comments or exlanations on each part of your code, so you, yourself, and others know what a section does. Look at all those questions by KTC. Try simplifying your code, or remove code that isn't really needed. And I believe, C++ has helper functions for getting on a character by character basis. Anyway, I'll check again and post here another time.
|
| Dec9-07, 09:41 AM | #4 |
|
|
program to read file
this is something different which i wrote
#include<iostream> #include<fstream> #include<stdlib.h> using namespace std; int main() { fstream file_ptr; char filename[81]; // Get name of file cout << "Enter the filename (make sure that the test file and the program is in the same directory): "; cin.getline(filename,81); // Open data file for READING cout << "Opening file "<< filename << endl; file_ptr.open(filename, ios::in); if (!file_ptr.good()) { //The file could not be opened cout << "ERROR - file " << filename << " could not be opened"; exit(1); } int a = 65; char number[58]={0}; for(int i=0; i<58; i++) { number[i]=a; a++; } int x[58]={0}; char c; // READ data from file until the end-of-file (EOF) is reached. while(file_ptr.eof()==0) { file_ptr.get(c); //reading character for(int i=0; i<58; i++) { if(number[i]==c) { x[i]=x[i]+1; break; } } } for(int i=0; i<58; i++) { cout<<number[i]<<"="<<x[i]<<endl; } // Close file cout << "Closing file " << filename << endl; file_ptr.close(); system("pause"); return 0; } |
| Dec9-07, 09:45 AM | #5 |
|
|
i took 65 because 65 is ASCII equivalent of 'a'. as i wnt to get all the alphabets(both uppercase and lowercase) so i used size of d array as 58.
65 + 58 =123 which is ASCII equivalent of 'Z'. i still dont know how to correct the filename part in d code. |
| Dec9-07, 11:53 AM | #6 |
|
Mentor
|
When reading files using a "while" loop in C++, it is almost never a good idea to use the eof() member function or the predefined EOF symbol, because it gives you "true" only after the program has tried to read past the end of file and failed. Therefore the loop makes one more cycle after the last real data item, giving you some garbage input.
In your case, the usual way to read one character at a time is: Code:
while (file_ptr.get(c))
{
// do your stuff with c here
}
The appropriate way to use eof() is to test whether the loop ended because of end of file or some other kind of input error: Code:
while (file_ptr.get(c))
{
// do your stuff with c here
}
if (file_ptr.eof())
cout << "End of file reached normally." << endl;
else
cout << "There was an input error." << endl;
|
| Thread Closed |
Similar discussions for: program to read file
|
||||
| Thread | Forum | Replies | ||
| legal music file sharing program. | Computing & Technology | 26 | ||
| safest file sharing program | Computing & Technology | 10 | ||
| C++ File Program | Programming & Comp Sci | 20 | ||
| Make a Pdf file into an ordinary web file (html file)? | Computing & Technology | 3 | ||
| My network directory file synch batch file thing :) | Computing & Technology | 2 | ||