0

I was assigned a program to write that uses file system calls to take a command line argument(assuming you pass in a text file address) and return the contents of said file. I have this code so far, but can't seem to figure out why my compiler is giving me errors in terms of recognizing the text-file passed as an argument, along with printing the information received from the file. Any sort of assistance/help is greatly appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>



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




int FP;
ssize_t bytes;

char buffer [100];

if(argc == 1){

FP = open(argv[1], O_RDONLY);
printf("Program name is : %s", argv[0])
bytes = read(FP, buffer,sizeof(buffer) -1);
printf("%s", bytes);
close(FP);

}






return 0;


}
  • What is the error message? – PhillipD Nov 27 '17 at 21:18
  • @PhillipD id status 1 file format not recognized also my previous version was returning only -1 and not outputting the file contents – Douglas Speaks Nov 27 '17 at 21:49
  • In `printf("%s", bytes);` the format `"%s"` is for displaying a string, but `bytes` is a number. Note that `buffer` is not a nul-terminated string, so `"%s"` is not the correct format for that either. Check the reference! – Bo Persson Nov 27 '17 at 22:15
  • please remove unneeded vertical spacing. DO separate code blocks (for, if, else, while, do...while, switch, case, default) via a single blank line. Please follow the axiom: *one one statement per line and (at most) one variable declaration per statement.* – user3629249 Nov 28 '17 at 08:36
  • regarding: `if(argc == 1)` The parameter `argc` includes the total number of arguments to be found in `argv[]` The first argument: `argv[0]` is always the name of the program being executed. If there were NO command line arguments. then `argc` will contain 1. Since the application needs a single command line argument, then `argc` will contain 2 and `argv[1]` will contain a pointer to the string that is the command line argument (in this scenario, the name of the file to read. So the statement should be: `if(argc == 2`) – user3629249 Nov 28 '17 at 08:40
  • unless the assignment requires that `open()` and `read()` be used, strongly suggest using `fopen()` and `fgets()` – user3629249 Nov 28 '17 at 08:42
  • the posted code contains a 'magic' number. 'magic' numbers are numbers with no basis. I.E. 100. 'magic' numbers make the code much more difficult to understand, debug, etc. Suggest using a `enum` statement or a `#define` statement to give the 'magic' number a meaningful name then using that meaningful name throughout the code. – user3629249 Nov 28 '17 at 08:44
  • when calling system functions, they can return an error indication. Always check that error indication and if it shows an error, call `perror( "..." );` followed by `exit( EXIT_FAILURE );` The call to `perror()` will output the enclosed text and the text that indicates why the system thinks the error occurred to `stderr` The call to `exit()` will exit the program, with a returned value of -1 to indicate an error occurred. – user3629249 Nov 28 '17 at 08:48

1 Answers1

0

the following proposed code:

  1. incorporates the comments to the question
  2. implements the desired functionality
  3. proper checks for errors
  4. documents why the header files are being included. In general, if you cannot state why a header file is being included, then don't include it. (the compiler will then tell you if you actually need that header file, and why
  5. when compiling always enable the warnings, then fix those warnings. (for gcc, at a minimum use: -Wall -Wextra -pedantic -Wconversion -std=gnu11 )

and now, the proposed code.

#include <stdio.h>   // fopen(), perror(), fgets(), fprintf(), printf(), FILE
#include <stdlib.h>  // exit(), EXIT_FAILURE


#define MAX_INPUT_LEN 100

int main(int argc, char *argv[])
{
    FILE *fp;
    char buffer [ MAX_INPUT_LEN ];

    if(argc != 2)
    {
        fprintf( stderr, "USAGE: %s fileName\n", argv[0] );
        exit( EXIT_FAILURE );
    }

    // implied else, correct number of command line parameters

    printf( "Program name is : %s", argv[0] );
    printf( "file to read: %s\n", argv[1] );

    fp = fopen( argv[1], "r" );
    if( NULL == fp )
    {
        perror( "fopen failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    while( NULL != fgets( buffer, sizeof buffer, fp ) )
    {
        printf( "%s", buffer );
    }

    fclose( fp );

    return 0;
}
user3629249
  • 16,402
  • 1
  • 16
  • 17