hi again people, im working on a simple program to look into a text file and output details about it. It seems to work fine for the first part but at the end of a while loop the code seems to stop working, i have had a little play about with it moving the code below the error into different places and i have found that there seems to be a problem with that little piece, i will explain as i go along
/*This program is to find out statistics of a specified file*/
#include<fstream.h>
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
int characternumber,arraylength,nextcharacter,
numberofcharacters,totalnumberofcharacters,numberoflines;
char filename[100];
char linebuffer[100];
char characters[100]="abcdefghijklmnopqrstuvwxyz0123456789[];'#,./<>?:@~{}!\"£$%^&*()_+=-\\`¬|";
int main ()
{
cout<<"Enter the filename: "; //get user input
cin.getline(filename,100);
ifstream file1(filename); //check to see if file exists
if (!file1.is_open()) //if file doesnt exist clear screen and restart
{
cout<<endl<<endl<<"file does not exist\n\n";
system("PAUSE");
system("CLS");
main();
}
file1.close();
arraylength = (strlen(characters)-1); //used in main loop to find # of characters**needs to be length - 1 or else it shows the number of NULL chars too
characternumber = 0; //reinitialize the variables
numberofcharacters = 0;
totalnumberofcharacters = 0;
numberoflines = 0;
/*this section finds out how many of each character is in the file*/
while (characternumber <= arraylength) //main loop do this for each character
{
ifstream file(filename);
while (!file.eof()) //2nd loop, goes through file 1ce for each character
{
nextcharacter = file.get();
if (nextcharacter == characters[characternumber])
{
numberofcharacters++;
}
else
{
if (characternumber == 0) //makes sure it calculates total # of chars 1nce
{
totalnumberofcharacters++;
}
}
}
cout<<characters[characternumber]<<" = "<<numberofcharacters<<"\t"; //output results in columns
numberofcharacters = 0; //re-initialize this variable for next character
characternumber++; //get next character
file.close(); //close file as it is re-opened at beginning of main loop
}
cout<<endl<<endl<<"Total number of Characters: "<<totalnumberofcharacters; //output total number of chars only 1ce
//number of lines...code stops excecuting here !!!***********************
ifstream file(filename); //open file
while (!file.eof()) //loop till eof() is reached
{
cin.getline(linebuffer,100); //count how many times cin.getline()
numberoflines++; //needs to be used before eof()
}
file.close();
cout<<endl<<endl<<"Number of lines: "<<numberoflines; //output result
/*extra features are needed like
number of lines: how many times do ya need to do cin.getline() before eof(),
filesize: need to look this up
wordcount: if character == space or newline and character after that is not == a space or newline then tht is 1 word
*****need lots of extra features******
*/
system(filename); //open file for comparing - testing
getch(); //keep on screen
/*
take user input on a file they want to check
use arraylength as product of loop with incremental characternumber that starts at 0 while (characternumber <= arraylength)
take 1st char from array and then use that to compare
use a loop to get next char from file and compare
if the character matchs the 1 brought form the array increment the numberofcharacters
when eof if reached break from loop, output number of chars and add 1 to characternumber and go through the loop again
do this till all characters in the array have been checked and outputted
next section to count the total number of chars and estimated file size
then get actual file size
*/
}
i dont know whether i am right but i think the problem lies within this section of code
//number of lines...code stops excecuting here !!!***********************
ifstream file(filename); //open file
while (!file.eof()) //loop till eof() is reached
{
cin.getline(linebuffer,100); //count how many times cin.getline()
numberoflines++; //needs to be used before eof()
}
file.close();
cout<<endl<<endl<<"Number of lines: "<<numberoflines; //output result
I have tried to comment it as much as i can, but a lot of teh comments on there are to help me as i am going along. Any ideas why i am having this problem ?
I am using winXP and Dev-Cpp compiler