My program, anyhelp

This is a discussion on My program, anyhelp within the C Programming forums, part of the General Programming Boards category; I am writing a program that when run from the terminal in linux (redhat), it is to take in 3 ...

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

    My program, anyhelp

    I am writing a program that when run from the terminal in linux (redhat), it is to take in 3 filenames of txt files, the first 2 file are used to compare lines of text to see if they are different if they are then they are wrote to the 3rd file, the program is to then prinf the contents of the third file to the screen.

    I have not been able to test the program as my other machine is down and i don't get it back till friday, so i can't test it, so i am having to write it by hand.

    Can anyone help me to see if this will work or if there

    /************************************************** **/
    /* Author: Alisdair Comb */
    /* Filename: checker.c */
    /* Date: 27/07/01 */
    /* Program: File Checker v1.0a */
    /* */
    /************************************************** **/
    /* About the Program */
    /* This Program is designed to take lines of text */
    /* from 2 seperate files (txt) and compare the */
    /* lines of text, if they are different, then */
    /* the line of text will be wrote to a third */
    /* file, after which it will display the contents */
    /* of that file. */
    /************************************************** **/

    #include <stdio.h>
    #include <string.h>

    #define MAX_LEN 80;

    int reperror, x;
    FILE *fn1, *fn2, *fn3;
    char buf1[MAX_LEN];
    char buf2[MAX_LEN];

    int main(int argc, char *argv[])
    {

    /*
    -------------------------------------------------------------------------
    Check to see that 3 command arguments were entered along with the program
    executable file.
    -------------------------------------------------------------------------
    */

    reperror = 0;

    if (argc == 3)
    {
    printf(" You Entered a correct amount of information");

    if((fn1 = fopen(argv[1] ,"r")) = NULL)
    {
    printf("There was an error with File 1");
    reperror++;
    }
    else
    {
    printf("File 1 Checked out okay");
    }

    if((fn1 = fopen(argv[2] ,"r")) = NULL)
    {
    printf("There was an error with File 2");
    reperror++;
    }
    else
    {
    printf("File 2 Checked out okay");
    }

    if((fn1 = fopen(argv[2] ,"w")) = NULL)
    {
    printf("There was an error with File 3");
    reperror++;
    }
    else
    {
    printf("File 3 Checked out okay");
    }
    /* 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!!! ");
    prinft(" 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");
    printf("Filenames from command prompt please check and");
    printf("correct");
    exit(1);
    }

    /*
    -------------------------------------------------------------------------

    -------------------------------------------------------------------------
    */

    do
    {
    // Gets a line of text from each txt file usally upto end of line char
    // or reaches the buffer
    fgets( buf1, MAX_LEN, argv[1])
    fgets( buf2, MAx_LEN, argv[2])

    x = strcmp(buf1, buf2)
    if ( x != 0)
    {
    printf("File 1: Line n :%s \n",buf1);
    printf("File 2: Line n: %s \n",buf2);
    fprintf(argv[3], "%s \n %s \n", buf1, buf2 );
    }
    } while ( argv[1] != EOF &| argv[2] != EOF)
    // While file 1 and or file 2 is not EOF


    // With the files check print whats in the text file 3
    if ( argv[3] == Null)
    printf("The File was empty!!!");
    else
    printf("%s" argv[3]); // Print contents of txtfile 3 to screen

    return 0;
    }

    I am usure about the while loop, not sure if you can do a and or situation. Also stinking i am not sure if i need to empty buf1 and buf2, do i need to.

    Thanks for any help I am most grateful.

    -ali

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I don't know if your program will wprk as intended but you'll want to change some of your conditional statements

    if((fn1 = fopen(argv[1] ,"r")) = NULL)

    to


    if((fn1 = fopen(argv[1] ,"r")) == NULL)

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    I've just noticed i haven't closed all the files,will add that to end of program.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    Thanks, just noticed that myself, also i have changed the code so that it use fn1, fn2 and fn3 probalym was still using argv[*], which would no doubt cause an error.
    Sorry, a while since i programed and i am used to having a compiler handy.
    -ali

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Some more:

    1) Your define statement is incorrect.

    2) You've include a semi colon directly after an if statement.

    3) They are some typos 'prinft'.

    4) Header file not included for exit() function.

    5) Incorrect checking for EOF.

    6) Final printf() call missing comma.

    There may be more.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    I fixed the while statement to || from |&

    Thanks, fixed all those errors, oh i miss my compiler, looking at the program will it do what i looks like it will do.

  7. #7
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The file pointer won't tell you when the end of file has been reached. The result of fgets() will, and you're missing some semi-colons from the end of your function calls.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    so does this mean i would to change the while statement to
    do
    {

    } (buf1 != EOF || buf2 != EOF )

    Not sure if iam getting what you are saying
    -ali

  9. #9
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ........and you've written NULL as Null.

    and I'm now a member

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    Thanks for the help Zen, its greatly appricated, good to know there are some helpful people out there willing to help.

    Do you think the program will do what is supposed to do.

    -ali

  11. #11
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Sorry if you want to test for end of file using file pointer you can use the feof() function. So you want -

    while (!feof(fn1)) etc.

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    I would never of thought to use that thanks

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    Here's teh code with the errors fixed (I hope)

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    #define MAX_LEN 80

    int reperror, x;
    FILE *fn1, *fn2, *fn3;
    char buf1[MAX_LEN];
    char buf2[MAX_LEN];

    int main(int argc, char *argv[])
    {

    /*
    -------------------------------------------------------------------------
    Check to see that 3 command arguments were entered along with the program
    executable file.
    -------------------------------------------------------------------------
    */

    reperror = 0;

    if (argc == 3)
    {
    printf(" You Entered a correct amount of information");

    if((fn1 = fopen(argv[1] ,"r")) == NULL)
    {
    printf("There was an error with File 1");
    reperror++;
    }
    else
    {
    printf("File 1 Checked out okay");
    }

    if((fn2 = fopen(argv[2] ,"r")) == NULL)
    {
    printf("There was an error with File 2");
    reperror++;
    }
    else
    printf("File 2 Checked out okay");

    if((fn3 = fopen(argv[2] ,"r+")) == NULL)
    {
    printf("There was an error with File 3");
    reperror++;
    }
    else
    printf("File 3 Checked out okay");


    /* 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!!! ");
    prinft(" 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)
    {
    printf("File 1: Line n :%s \n",buf1);
    printf("File 2: Line n: %s \n",buf2);
    fprintf(fn3, "%s \n %s \n", buf1, buf2 );
    }
    } while ( !feof(fn1) || !feof(fn2))
    // While file 1 or file 2 is not EOF


    // With the files check print whats in the text file 3
    if ( fn3 == NULL )
    printf("The File was empty!!!");
    else
    printf("%s", fn3); // Print contents of txtfile 3 to screen

    fclose(fn1);
    fclose(fn2);
    fclose(fn3);
    return (0);
    }

    To me it seems as though it will work, but its the first time i've done this sort of prog of this sort of scope.
    -ali

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    Do i need to clear the buf1 and buf2 once its finished reading in a line, not sure realisticaly you should but i can't tell. be easy to cleat it just fill it with nulls in a for loop

  15. #15
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144

    No

    I think i answered this before. When you are dealing with strings and text files, you do not need to "clear the buffers". Using memset, bzero, or a for loop is a waste of time. All the C-string manipulation functions (i.e. strcmp) will ignore all of the bytes in the buffer after the '\0' character. So NULLing out the buffer isn't necessary.
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program quits unexpected
    By skelesp in forum C Programming
    Replies: 34
    Last Post: 12-10-2008, 08:10 AM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  3. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  4. Replies: 4
    Last Post: 02-21-2008, 09:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21