What's wrong with my code here???
Hello, everybody. Need help with this program. It's meant to take a pattern 1 and replace the first occurance in a string with pattern 2. The patterns are case sensitive. I keep getting a segmentation fault.
Here's an example of what the code should do:
Text before code:
Chris is great!
Is
is
Text after code with i as pattern 1 and x as pattern 2.
Chrxs is great!
Is
xs
Here's my code:
Code:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
int main(int argc, char *argv[])
{
char str[5]; //Char to hold .out
char * line; //Char to hold line read in.
char * pch; //Char to hold first occurance of pattern
char * string; //Char to hold the entire string.
char * string2; //Makes a second array to hold the modified string.
char * replace1; //Char to hold string of pattern to replace.
char * replace2; //Char to hold string of pattern to replace with.
FILE * inFILE; //Name of file to read in.
char * outFILE; //Name of outfile.
int in, out; //Int to hold the return values of in, out, and nread.
if (argc != 4)
{
fprintf(stderr,"Not enough arguments.\n"); //Prints out error message if there is not
//enough arguments.
exit(1); //Exits the program and returns failure.
}
strcpy (str, ".out"); //Copies .out to str.
replace1 = argv[2]; //Gets the pattern to be replaced from the arguments.
replace2 = argv[3]; //Gets the pattern that will replace pattern one from the arguments.
string = (char*) malloc (101); //Allocates memory for string.
inFILE = fopen(argv[1],"r"); //Opens the file.
outFILE = argv[1]; //Gets the name of the file.
strncat (outFILE,str,5); //Adds .out to that end of the file.
out = open(outFILE, O_RDWR|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR); //Uses system calls
//to open the outfile and create permissions for the file.
if (out == -1)
{
fprintf(stderr, "Error with system calls.\n"); //Prints error message if there is an error
//opening the outfile.
exit(1); //Exits the program and returns failure.
}
if (inFILE == NULL)
{
fprintf(stderr, "Error opening file.\n"); //Prints error message if there is an error
//opening the infile.
exit(1); //Exits the program and returns failure.
}
if (!feof(inFILE))
{
line = fgets(string, 101, inFILE); //Gets a string from the array.
pch = strstr (line,replace1); //Finds the first occurance of replace1.
int index; //Declares an int variable named index.
string2 = (char *) malloc (101); //Allocates memory for string2.
index = strlen(string2); //Makes index equal to the length of string2
if (pch == NULL)
strcpy (string2, string); //Copies string if pch is null.
else
memcpy (string2, line, (strlen(string) - strlen(pch))); //Copies the string up to the
//first occurance of replace1.
string2[index] = '\0'; //Adds null terminating value to the end of string2.
strncat (string2, replace2, strlen(replace2)); //Adds replace2 to the modified string.
strncat (string2, pch + strlen(replace1), (strlen(pch) - strlen(replace1))); //Adds the
//rest of the string to the modified string.
write (out, string2, strlen(string2)); //Writes the string to file.
}
else
fprintf(stderr, "Error detected in file.\n"); //Prints error message if there is an error
//with the file.
exit(1); //Exits the program and returns failure.
if (out == -1)
{
fprintf(stderr, "Error with system calls.\n"); //Prints error message if there is an error
//closeing the file.
exit(1); //Exits the program and returns failure.
}
close(out); //Closes the output file.
fclose (inFILE); //Closes the input file.
free (string2); //Frees the memory used to create string2.
free (string); //Frees the memory used to create string.
exit(0); //Exits program and returns success.
}
Any help given will be greated appreciated.
Thanks!!!
Signed,
Chris Beech