This is just going to be a collection of programs that i randomly make as i learn c, free to use.
i very much welcome suggestions on improvements.
This program finds and replaces characters in a file and writes the changes
/*Written by Michael Drahony*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
int i = 0;
char *buff1 = malloc(1000*4);
FILE *file1;
char *help = "--help";
if(*argv[1] == *help){
printf("Usage: prog-name <filename> <arg1> <arg2>\n");
exit(0);
}
file1 = fopen(argv[1], "r+");
if(file1 == NULL){
perror("Error");
exit(0);
}
fread(buff1, sizeof(char), 1000, file1);
fclose(file1);
file1 = fopen(argv[1], "w");
if(file1 == NULL){
perror("Error: Cannot write to file! - ");
exit(0);
}
i = 0;
while(buff1[i] != EOF){
if(buff1[i] == *argv[2]){
buff1[i] = *argv[3];
++i;
}else{
++i;
}
}
fwrite(buff1, sizeof(char),1000, file1);
fclose(file1);
return 0;
}