why dosnt this work ????

This is a discussion on why dosnt this work ???? within the C Programming forums, part of the General Programming Boards category; i wrote this prog to read in a file and number all but the blank lines and write to another ...

  1. #1
    Unregistered
    Guest

    why dosnt this work ????

    i wrote this prog to read in a file and number all but the blank lines and write to another fill all of the lines including non numbered blanks it worked fine but now i want to get the input and output files from the command line i cant figure out why this doesnt work it compiles and runs but it doesnt wright anything to the output file PLEASE HELP

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

    #define MAXLEN 81
    #define NUMARG 5



    int main (int argc, char *argv[])
    {
    char buffer[MAXLEN];
    FILE *finp, *foutp;
    unsigned int line_number=0;
    if (argc != NUMARG)
    {
    printf("usage : p2 infile outfile -m -u\n");
    exit (1);
    }

    if ((finp = fopen(argv[1], "r")) == NULL)
    {
    printf("could not open file %s for reading. \n", argv[1]);
    exit (1);
    }

    if ((foutp =fopen(argv[2], "w")) == NULL)
    {
    printf("could not open file %s for writing.\n", argv[2]);
    exit(1);
    }
    /* count non empty lines */
    /*output all lines*/
    while (fgets(buffer, sizeof buffer, finp))
    {
    if(strlen(buffer)>1)
    if(fprintf(foutp, "%u", ++line_number)<=0)
    break;

    if(fprintf(foutp, "%s",buffer)<=0)
    break;
    }



    if (fclose(finp) == EOF)
    {
    printf("error in closing file input.dat.\n");
    }

    if (fclose(foutp) == EOF)
    {
    printf("error in closing output.dat.\n");
    }
    return 0;
    } /*end of main */

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    The line

    while (fgets(buffer, sizeof buffer, finp))

    should read

    while(fgets(buffer, sizeof(buffer),finp))

    I notice your file extension are .dat instead of .txt are you intending to use text or binary files?
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Salem beat me to it. The parenthesis requirement is compiler specific. MSVC++ requires it it seems, but I have seen it used (and working) both ways.

    Quzah.

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    sizeof with brackets is only required for primitives (sizeof (int) etc). As you have #define NUMARG 5, what else are you getting off the command line?
    zen

  5. #5
    Unregistered
    Guest
    Im using emacs in redhat 7.1 and the reason for
    #define NUMARG 5 is when i run this i need progname unputfile outputfile up to 2 flags

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  4. my function progie ( that dosnt work)
    By datainjector in forum C Programming
    Replies: 3
    Last Post: 07-06-2002, 10:17 PM
  5. Why dosnt my netbios work?
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-30-2001, 12:34 AM

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