Home > Net >  Trouble Compiling External '.c' Files: Word Search Program written in C
Trouble Compiling External '.c' Files: Word Search Program written in C

Time:02-27

Hope everyone's doing well.

I was tasked with making a word search program, which I technically did succeed in doing. Problem is, I was expected to use external files with these two functions:

extern int copyfile(char[], char *, int);
extern int output_array(int[], int);

I have the code already written and working inside the main function.

Initially having an external copyfile.c file with the below arguments and while loop:

    copyfile(filename, searchPatt, wordLen);
    while ((onebyte = fgetc(specialdata)) != EOF)

As well as an external output_array.c file with a for loop and these arguments:

    output_array(&i, found);
    for(i = 0; i < (strLen - wordLen); i  )

I thought would have done the trick but I've had no luck.

P.S. You will need to make your own sample text file to test input.txt with this program.

Here is the code:

#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h> 
#include <string.h> 
#include "copyfile.c"
#include "output_integers.c"

extern int copyfile(char *, char *, int);
extern int output_array(int[], int);

int main(int argc, char* argv[])
{  
    char *filename;
    filename = (char *)malloc(1000 * sizeof(char)); // Allocate space for the name of a file
    char *searchPatt;
    searchPatt = (char *)malloc(1000 * sizeof(char)); // Allocate space for the contents of a file

    printf("\nWelcome to 'Find Your Match' produced by Orange Blossom Lucky Software, Inc.\n");
    printf("Project leader: Nate");
    printf("\nThis program will find the matching strings in any file.\n");

    printf("\nPlease enter the name of the file to be searched: ");
    // Input ----------------------------------------------------------------

    scanf("%[^\n]", filename); // Read filename from user

    printf("Thank you. You entered ");
    printf("%s", filename);

    // Open File
    char *myfile = (char *)malloc(1000 * sizeof(char)); // Allocate space for file contents
    FILE *specialdata = fopen(filename, "r"); // Open file for read operations
    int j = 0;
    int onebyte; 

    copyfile(filename, searchPatt, wordLen);
    
    //printf("\n%s", myfile);

    int i;
    int found;
    int strLen;
    int wordLen;

    printf("\n\nPlease enter the search pattern you are seeking: ");
    // Input ----------------------------------------------------------------

    scanf("%s", searchPatt); // Read search pattern you are seeking

    printf("Thank you. You entered: ");
    printf("%s", searchPatt);
    printf(".\n");

    strLen  = strlen(myfile);  // Find length of string
    wordLen = strlen(searchPatt);

    printf("\nThe search has completed. The search key, ");
    printf("%s", searchPatt);
    printf(", is found in these positions within the file:\n");

    output_array(&i, found);

    fclose(specialdata);

    printf("\nThat completes the string search. Please enjoy your matches.");
    printf("\nA zero will be returned to the operating system.");
    printf("\nBye.");

    return 0;
}

copyfile.c:

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

extern char *myfile;
extern FILE *specialdata;
extern int onebyte;
extern int j;

int copyfile(char *filename, char *searchPatt, int wordLen) { // Definition of function

    // Reading -----------------------------------------------------------------

    while ((onebyte = fgetc(specialdata)) != EOF) { 
        //putchar(onebyte); // Just for us to see what's happening!
        myfile[j] = onebyte; // Copy the byte from the file into our array
        j  ; // Update our 'j' counter
    }
            
    return 0;
}

output_array.c:

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

extern char *myfile;
extern int strLen;
extern int wordLen;
extern char searchPatt;
extern int found;

//int output_array(int[], int); // Protoype

int output_array(int i[], int found) { // Definition

    for (i = 0; *i < (strLen - wordLen); i  ) {
        // Match word at current position
        found = 1;
        for (int k = 0; k < wordLen; k  ) {
            // If word is not matched
            if (myfile[*i   k] != searchPatt[&k]) {
                found = 0;
                break;
            }
        }

        // If word have been found then print found message
        if (found == 1) {
            printf("%d \n", *i);
        }
    }

    return 0;
}

Here is the bash.sh script I used to try compilation:

# First compile the c files into object files in any order, doesn't matter
gcc -m64 -c -std=c17 -o outputs.o output_integers.c
gcc -m64 -c -std=c17 -o copy.o copyfile.c
gcc search.c -m64 -std=c17 -c -o sea.o
#gcc -m64 -c -std=c17 -o sea.o search.c Line above is what the professor wrote, but I wonder if it should be this instead

# Now call the linker to link these object files into an executable file
gcc -m64 -std=c17 sea.o copy.o outputs.o -o search.out

# Now execute the executable
./search.out
echo Goodbyte

Here is the error the terminal gives me:

String_Search % ./r.sh
ld: library not found for -lmsea.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
./r.sh: line 16: ./search.out: No such file or directory

CodePudding user response:

There are a lot of problems in the code:

  • extern definitions should be moved to a header file search.h, included by all .c files.
  • C source files should not include other .c files
  • global variables are error prone and in your case non existent, consider passing arguments instead of trying to refer to local variables in the main function.
  • the bash script should be more consistent: object files should have the same basename as the corresponding source file and compiled with consistent compiler arguments. Consider using a Makefile instead of a bash script or at least echo the commands as they are executed.

Here are some proposals:

Makefile:

CFLAGS= -m64 -Wall -Werror

all: search.out
        ./search.out

search.out: output_array.o copyfile.o search.o
        gcc $(CFLAGS) -o $@ output_array.o copyfile.o search.o

%.o: %.c search.h Makefile
        gcc $(CFLAGS) -o $@ -c $<

search.h:

extern char *copyfile(const char *filename);
extern int output_array(const char *myfile, const char *searchPatt);

search.c:

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

int main(int argc, char *argv[]) {
    char filename[1000];
    char searchPatt[1000];

    printf("Welcome to 'Find Your Match' produced by Orange Blossom Lucky Software, Inc.\n");
    printf("Project leader: Nate\n");
    printf("This program will find the matching strings in any file.\n");

    printf("Please enter the name of the file to be searched: ");
    if (scanf("           
  •  Tags:  
  • c
  • Related