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

Recommended Answers

All 11 Replies

>cin.getline(linebuffer,100);
This should be

file.getline(linebuffer,100);

The program is stopping dead in its tracks because the loop is keyed on whether the file is empty or not, and you never read from the file, just standard input.

Also, using eof() as a loop condition is wrong because the eofbit is only set for the stream after you try and fail to read. This will usually cause the loop to iterate one more time than you expect.

ok thanks for your help. Before looking back on here i spotted that error in the code where i needed to change cin to file lol, thanks for your help anyway

Don't forget to read all of my post. Your code has a subtle bug that needs to be fixed.

im not exactly sure what you mean with using eof() as the loop condition, it works anyway, but thanks a lot for the tip i will read up on it

could you give me an example of how i mite overcome this problem, eof() is the only thing i have used while using files.

thanks for those links, i can know see the problem, but i cant seem to find a way of incorporating this into my own 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

could you show me how i could alter this code to overcome the problem ?

A new [post=155265]tip[/post] for you and those who will follow.

thankyou for that tip, i have managed to get this to work in all places but 1, where i used file.eof() as the loop and when i used nextcharacter = file.get() to read and work with 1 character individually.

while (!file.eof())
                {
                      nextcharacter = file.get();
                }

I have tried to incorporate the input as the loop by putting:

while(nextcharacter = file.get())
{
}

The above didnt work, i thought it was because the compiler may have took the = as comparing the 2 instead of assigning file.get() to nextcharacter, so I then tried this:

nextcharacter = file.get();
while(nextcharacter)
{
}

but that didnt work either, do you have any suggestions ?

#include <iostream>
#include <fstream>

int main()
{
   std::ifstream file("file.txt");
   if ( file )
   {
      int c;
      while ( (c = file.get()) != EOF )
      {
         std::cout << static_cast<char>(c);
      }
   }
   return 0;
}

ok, thanks alot for your help on this :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.