Thread: counting program worked, but test file CRASHES IT.. WHY?

  1. #1
    Unregistered
    Guest

    Unhappy counting program worked, but test file CRASHES IT.. WHY?

    Hi all, I wrote a program for class that read in a file and counted all occurences of certain key words.

    Each line in the file was read one at a time, stored in a temp array and compared to the keywords I had to check for.

    The line reading went something like this:
    While infile! EOF
    {
    While infile.get(ch)!='\n'
    { temparray[i]=c; i++;}
    ....
    .....
    testing ... etc.
    }


    It worked for every test file i came up with, however, the teacher has posted the "test file" used for marking it and when i checked it against my program, it CRASHED.

    The only reason this happens (or that i can figure out) is because the file has no '\n' null character terminating at the end of each line in the file, causing the temparray to "not" store what is needed.

    (is this making any sense).

    Just wondering if there was some other way of storing parts of the file to compare to keywords, or if my program was correct in its logic

    Any ideas would be appreciated, thanks.

    p.s the file to be read was a program file.

  2. #2
    Unregistered
    Guest
    First of all '\n' is not null... it is newline. '\0' is the nul char that most C string functions need.

    The important question is, how is the test file formatted?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    There are probably several things wrong with your program

    1. it contains a while ( !eof ) loop

    2. you do not test infile.get(ch)!= EOF anywhere

    3. you assume the file contains regularly spaced newlines
    While infile.get(ch)!='\n'
    { temparray[i]=c; i++;}

    If someone gives you a binary file, then newlines will be random, and you'll no doubt overflow whatever buffer you have

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I was looking at this thread, and being quite (read very) new I decided to give something similar a try.

    Code:
    #include "stdio.h"
    #include "iostream.h"
    #include "string.h"
    
    int main(int argc, char* argv[])
    {
    
    FILE	*input_file;
    char	input_string[6]={"0"};
    char	compare_string[6]={"Harry"};
    int	count=0;
    
    int	i=0;
    long	file_loc=0;
    
    if	((input_file=fopen("e:\\test.txt","r"))==NULL)
    	{	
    		cerr << "Could not open input file\n";
    		return 1;
    	}
    	
    while (!feof(input_file))
    	{
    	fseek (input_file,file_loc,SEEK_SET);
    	
    	for (i=0;i<(sizeof(input_string)-1);++i)
    		input_string[i] = fgetc(input_file);
    		 	
    	if (strncmp (input_string,compare_string,(sizeof(input_string)))==0)
    		{
    		cout << "Found "<< input_string << " starting at byte "<< file_loc << "\n";
    		++count;
    		}
    	++file_loc;
    	}
    
    cout << "Found " <<count <<" occurrence(s) of "<< compare_string <<"\n";
    
    return 0;
    }
    Now I wasn't worrying about taking the test data as user arguments, just the mechanism of comparing. The above code seems to run happily for me, and give the right result regardles of newlines etc. But I read your post Salem, can you give me some more information about why I should be testing for eof while reading the characters in.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    The 'copy file' problem illustrates the feof() problem

    Code:
    #include <stdio.h>
    
    // this works
    void good ( char *in, char *out ) {
        FILE *fin = fopen( in, "r" );
        FILE *fout= fopen( out,"w" );
        int ch;
        while ( (ch=fgetc(fin)) != EOF ) {
            fputc(ch,fout);
        }
        fclose(fin);
        fclose(fout);
    }
    
    // this appends an extra char - specifically (char)EOF
    // to the output file
    // feof() returns true only AFTER a read function has returned EOF
    void bad ( char *in, char *out ) {
        FILE *fin = fopen( in, "r" );
        FILE *fout= fopen( out,"w" );
        int ch;
        while ( !feof(fin) ) {
            ch = fgetc(fin);
            fputc(ch,fout);
        }
        fclose(fin);
        fclose(fout);
    }
    
    // this works, but looks less tidy
    void ugly ( char *in, char *out ) {
        FILE *fin = fopen( in, "r" );
        FILE *fout= fopen( out,"w" );
        int ch;
        while ( !feof(fin) ) {
            ch = fgetc(fin);
            if ( ch != EOF ) {        
                fputc(ch,fout);
            }
        }
        fclose(fin);
        fclose(fout);
    }
    
    int main ( ) {
        good( "sample.txt", "good.txt" );
        bad( "sample.txt", "bad.txt" );
        ugly( "sample.txt", "ugly.txt" );
        return 0;
    }

  6. #6
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Thanks for that, it's very useful information. Is there a danger when only reading like in my code? Or is it best just to get in the habbit of not using feof that way?
    Demonographic rhinology is not the only possible outcome, but why take the chance

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Best avoided IMO

    > Is there a danger when only reading like in my code?
    Well towards the end of the file, your buffer will slowly fill up with (char)EOF chars.

    It might not be a specific problem for this code, on your implementation, but at some point, you'll want to implement something which will have a difference.

    Go for an approach which will always work - then you don't have to think about which approach to take, or have to sit there wondering why its not working all of a sudden when you've made the wrong choice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM