golmschenk |
03-02-2010 11:32 AM |
C - Copy File Line By Line With Some Slight Changes
In C, I want to make a program that will take a file and replace it with a file that's nearly the same but with some minor changes. Also, I would like to point out that I'm still fairly much a beginner with C. As for an example of the file, I want to take something like this:
Code:
Random Crap
More Random Crap
Even More
Something That Changes XXXXX
Continuing Random Crap After
And make this happen to it:
Code:
Random Crap
More Random Crap
Even More
Something That Changes YYYYY
Continuing Random Crap After
I figured the best way to go about doing this was to open the file and a blank file, read the original bit by bit and when it gets to the point that needs to be changed exchange the part that needs to be changed with what it should be changed to, delete the original file, and rename the new one to the correct name. So the first problem I've run into (and I'll probably have more) is that when I'm trying to read stuff from the original file, my program doesn't seem to be finding the original. I'm sure much of my problems will be just from not knowing how to use the C functions so bear with me. Right now I have the following:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
FILE *infile;
FILE *outfile;
char line[50];
infile = fopen("cubismxmarco.txt", "r");
outfile = fopen("cubismxmarcoreplace.txt", "w");
if (infile == NULL)
perror ("Died Here");
else {
fgets (line, 50, infile);
printf("%s", line);
}
fclose(infile);
fclose(outfile);
return 0;
}
And when I try to run it I get the following:
Code:
Died Here: No such file or directory
Segmentation fault
Right now the length of 50 was just a random test length. Pretty much I was just trying to get it to read anything from the file. In the end I'm going to want it to read the entire file bit by bit, but at the moment I can't seem to get it to read anything. Can anyone help? Thanks for the time.
|