BlueSkies21
BlueSkies21

Reputation: 13

Opening a text file from command line argument?

I have two C files, one is the function definitions file titled "function.c" and the other is the main file titled "main.c". I am trying to open a text file that is entered by the user as a command-line argument, like below.

./program file.txt

The program then uses "file.txt". My main.c code is below:

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

char *readFile(char *filename);

int main(int arg_count, char *arg[]) {

  FILE *fptr = fopen(arg[1] ,"r");

  if(fptr == NULL) {
    printf("Error! opening file");
    exit(1);
  }
  readFile(arg[1]);

  return 0;
}

The main function calls the function within the other c file called "readFile," in "function.c," which is below:

char *readFile(char *filename) {

fgets(arg[1], sizeof(arg[1]), stdin);

rest of code

  return 0;
}

What I want to do with the file, will be written inside of function.c (within the readFile function). When I tried compiling it, it gave me an error saying "error: ‘arg’ undeclared," but when I try to declare it, it gives me another error. Note that I compile both files together, along with a header file containing the function prototype. How do I make it so that what is entered from the command line, carries over into the function.c file, so I can use that file?

Upvotes: 1

Views: 1240

Answers (2)

user3629249
user3629249

Reputation: 16540

the following proposed code::

  1. cleanly compiles
  2. performs the desired functionality
  3. properly checks for errors (except for call to fgets())
  4. documents why each header file is included
  5. properly verifies existence of expected command line parameter
  6. makes use of dynamic memory for passing string back to caller
  7. cleans up after itself (fclose() and free())
  8. uses a 'include' guard to prevent multiple include of the header file
  9. inserts any error messages into stderr to inform the user of any problems
  10. uses a #define to give the 'magic' number (20) a meaningful name

and now, the proposed code:

// myHeader.h
#ifndef MY_HEADER_H
#define MY_HEADER_H

char *readFile(char *filename);   

#endif  // MY_HEADER_H


// main.c
#include <stdio.h>    // fprintf()
#include <stdlib.h>   // exit(), EXIT_FAILURE, free()
#include "myHeader.h"


int main(int arg_count, char *arg[]) 
{
    if( arg_count <= 1 )
    {
        fprintf( stderr, "USAGE: %s inputFileName\n", arg[0] );
        exit( EXIT_FAILURE );
    }
    
    // implied else, command line parameter was entered by user
        

    char *buffer = readFile(arg[1]);
    
    printf( "%s]n", buffer );
    
    free( buffer );

  return 0;
}


// function.c
#include <stdio.h>   // fopen(), fclose(), perror(), fgets()
#include <stdlib.h>  // exit(), EXIT_FAILURE, malloc()
#include "myHeader.h"

#define MAX_BUF_LEN 20

char *readFile( char *filename ) 
{
    FILE *fptr = fopen( filename , "r" );

    if( fptr == NULL ) 
    {
        perror( "fopen failed" );
        exit( EXIT_FAILURE );
    }
    
    //implied else, fopen successful
    
    // read first line from file
    char *buffer = malloc( MAX_BUF_LEN );
    if( buffer == NULL )
    {
        perror( "malloc failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful
    
    fgets( buffer, sizeof(buffer), fptr );

    fclose( fptr );
    
    return buffer;
}

Upvotes: 0

Govind Parmar
Govind Parmar

Reputation: 21572

Arguments to a function only exist within the scope of that function. char *arg[] does not exist in the scope of the function readFile, even if both functions are in the same source file.

readFile gets a pointer to arg[1] in the argument called filename; use that instead in readFile.

Upvotes: 2

Related Questions