I changed some of the code again. Same segmentation fault error.
Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
char str[5]; //Char to hold .out
char outFILE[100]; //Char to hold name of outfile.
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.
int in, out; //Int to hold the return values of in and out.
int index; //Declares an int variable named index.
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 = malloc (101); //Allocates memory for string.
inFILE = fopen(argv[1],"r"); //Opens the file.
strcpy(outFILE, argv[1]); //Copies filename to outFILE.
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.
}
else
{
while (!feof(inFILE))
{
line = fgets(string, 101, inFILE); //Gets a string from the array.
pch = strstr (line,replace1); //Finds the first occurance of replace1.
string2 = malloc (101); //Allocates memory for string2.
if (pch == NULL)
{
strcpy (string2, string); //Copies string if pch is null.
}
else
{
index = strlen(string) - strlen(pch); //Makes index equal to the length of string minus
//length of pch.
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.
strcat (string2, replace2); //Adds replace2 to the modified string.
strcat (string2, pch + strlen(replace1)); //Adds the rest of the string to the modified
//string.
write (out, string2, strlen(string2)); //Writes the string to file.
}
}
}
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 (string); //Frees the memory used to create string.
free (string2); //Frees the memory used to create string2.
exit(0); //Exits program and returns success.
}
Except now I can't seem to find a place to put the
Code:
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.