Help me plz

#include <stdio.h>

int main ( int argc, char *argv[] )
{
    if ( argc != 2 ) 
    {

        printf( "usage: %s filename", argv[0] );
    }
    else 
    {
               FILE *file = fopen( argv[1], "r" );

                if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            int x;
                        while  ( ( x = fgetc( file ) ) != EOF )
            {
                printf( "%c", x );
            }
            fclose( file );
        }
    }
}

Dani AI

Generated

The code posted by reads a filename from argv and prints its contents; replies from and touched on argument checking and follow-up improvements. Quick clarifications: argc counts the program name, so a program that requires exactly one filename should check for argc == 2; a program that accepts one or more files should test argc < 2 to detect the missing-argument case. Usage and error messages are best sent to stderr and terminated with a nonzero exit code (use EXIT_FAILURE/EXIT_SUCCESS from <stdlib.h>).

For better diagnostics and robustness, check fopen against NULL (not 0) and call perror or print errno when open/read fails. For correctness when printing raw bytes, avoid passing a possibly negative char value directly to character-output functions; cast to unsigned char first or, for best throughput on large files, use block I/O instead of character-by-character loops.

A compact, robust approach is to copy data in chunks with fread/fwrite (this also addresses ’s performance concern):

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

int main(int argc, char *argv[])
{
    FILE *f = fopen(argv[1], "rb");
    if (!f) { perror("open"); return EXIT_FAILURE; }

    char buf[8192];
    size_t n;
    while ((n = fread(buf, 1, sizeof buf, f)) > 0)
        fwrite(buf, 1, n, stdout);

    if (ferror(f)) { perror("read"); fclose(f); return EXIT_FAILURE; }

    fclose(f);
    return EXIT_SUCCESS;
}

Additional practical notes: prefer fprintf(stderr, ...) with a trailing newline for usage text; check ferror after I/O loops; use BUFSIZ or a larger buffer for speed; open with "rb" for binary-safe copies on Windows; and include minimal context (compile command, platform, exact error/warnings) when asking for help, as recommended. For reference see the C library docs for fopen and fread (fopen, fread, ).

Recommended Answers

All 3 Replies

Just replace the condition from line 5 with this one: if ( argc < 2 ) and it should work fine.
Tested on Linux: Ubuntu, and on win 7.
On further posts, please post the question as well, because we can't guess that...

"Help me please" is not a good question. Please be specific about what you want help with. I'd also recommend reading this tutorial on how to ask a smart question.

I'm going to guess you meant to post something more like this:

#include <stdio.h>
int main(int argc, char *argv[])
{
    if (argc != 2) {
        printf("usage: %s filename", argv[0]);
    } else {
        FILE *file = fopen(argv[1], "r");
        if (file == 0) {
            printf("Could not open file\n");
        } else {
            int x;
            while ((x = fgetc(file)) != EOF) {
                printf("%c", x);
            }
            fclose(file);
        }
    }
}

Now, did you have a question? Here are some good questions you might consider asking:

  • "When I run this with the wrong number of arguments, it terminates immediately and the usage message doesn't display! What am I doing wrong?"
  • "This program runs too slowly when I use it on large files. Can you suggest ways to speed it up?"
  • "I tried to use this program in a shell script, but I can't tell from testing $? whether it succeeded or not. How do I make my program return a meaningful value?"
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.