Thread: Going mad can anyone help

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    27

    Going mad can anyone help

    Ok after testing checking my program seems to work fine, it reads in the command line ok, it checks all the files, it reads in the lines from both files and compares them for a difference, were if they are different it writes it to a third txtfile, All this works fine, it writes the relevent info to the file.

    The problem i am having is that when i try to display the contents of the 3rd txt file it won't work at all.

    I checked and dbl checked and i am 98% sure that its correct, can anyone see anything wrong or am i missing something.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_LEN 256 /* max length of buffer string */
    #define BUFFERSIZE 1000 /* Used to display 3rd txt file*/
    
    /* Set up variables */
    int reperror, x, line;
    FILE *fn1, *fn2, *fn3;
    char buf1[MAX_LEN];
    char buf2[MAX_LEN];
    char filebuff[BUFFERSIZE];
    
    int main(int argc, char *argv[])
    {
    
      /*
      -------------------------------------------------------------------------
      Check to see that 3 command arguments were entered along with the program
      executable file, and test them to see if they will work.
      -------------------------------------------------------------------------
      */
    
      reperror = 0;
      line = 0;
    
      if (argc == 4)
    	{
    		printf(" You Entered a correct amount of information\n");
    	  
    		if((fn1 = fopen(argv[1] ,"r")) == NULL)
    		{
    			printf("There was an error with File 1\n");
    			reperror++;
    		}
    		else
    		  printf("File 1 Checked out okay\n");
    
    	  if((fn2 = fopen(argv[2] ,"r")) == NULL)
    		{
    			printf("There was an error with File 2\n");
    			reperror++;
    		}
    		else
    			printf("File 2 Checked out okay\n");
    		
    		if((fn3 = fopen(argv[3] ,"r+")) == NULL)
    		{
    			printf("There was an error with File 3");
    			reperror++;
    		}
    		else
    			printf("File 3 Checked out okay\n");
    
    
        /* If there has been any errors the program terminates*/
        if (reperror > 0)
    		{
    			printf(" Sorry There was a problem with opening the files, please check/n ");
    			printf(" Filenames using error messages above!!!\n ");
    			printf(" program terminated\n ");
    			exit(1);
    		}
        else
    			printf("All the files check out okay, program will continue....\n");
    
    	}
    	else
    	{
    		printf("There was and error, you did not enter enough\n");
    		printf("Filenames from command prompt please check and\n");
    		printf("correct\n");
    		exit(1);
    	}
    
    /*
    -------------------------------------------------------------------------
    This is the actual program, the previous part of the program checked teh files
    for errors and the files are still open.
    -------------------------------------------------------------------------
    */
    
      do
      {
    	  /* Gets a line of text from each txt file usally upto end of line char
    	   or reaches the buffer */
        fgets( buf1, MAX_LEN, fn1 );
    	  fgets( buf2, MAX_LEN, fn2 );
    
        x = strcmp(buf1, buf2);
        /* x = < 0 Str1 is less then str2 
           x = 0  Str1 is equal to str2
    	     x = > 0 Str is greater than str2
    	     if x not = 0 then display and write lines to text file 3 */
    	  if ( x != 0)  
    	  {
    		  line++;
    	    printf("File 1: Line %d :%s \n",line, buf1);
    		  printf("File 2: Line %d :%s \n",line, buf2);
          fprintf(fn3, "File 1 line:%d %sFile 2 line:%d %s\n",line, buf1, line, buf2 );
    	  }
      
      } while ( !feof(fn1) || !feof(fn2) );
     
    	/* While file 1 or file 2 is not fEOF keep reading in lines and comparing
         With the files check print whats in the text file 3
         if it empty it will say so else it will display 
         the files contents to the screen */
    	
      printf("Contents of the File '%s'\n",argv[3]);
    	
    	while (! feof(fn3))
    	{
    	  
    		fgets(filebuff,BUFFERSIZE,fn3);
    		
    		if(filebuff[0] == '\n');
    		{
    		  printf("File is Empty or invalid\n");
    			break;
    		}
    		
    		printf("\n%s\n",filebuff);
    	}
    	
    	fclose(fn1);
    	fclose(fn2);
    	fclose(fn3);
    	
    	return (0);
    }

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You may have to use fseek() to return the file pointer back to the begining of the file between writing and reading the file. You could try something like fseek(fn3,0,SEEK_SET);

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Code:
    if(filebuff[0] == '\n');
    {
        printf("File is Empty or invalid\n");
        break;
    }
    That semicolon after the if makes it pretty useless. If you don't remove it, every time the program reaches this part of execution it will print "File is Empty or invalid" and exit the loop.
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makes me mad..
    By Warrax in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 05-01-2007, 07:49 AM
  2. I, Robot -- It's a trick, and I'm mad.
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-15-2004, 01:57 PM
  3. mad libs
    By CheesyMoo in forum C++ Programming
    Replies: 17
    Last Post: 01-18-2003, 11:14 PM
  4. mad scrolling
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-20-2001, 02:38 PM
  5. Spam :mad:
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-22-2001, 10:43 PM