C - Copy File Line By Line With Some Slight Changes
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
Last edited by golmschenk; 03-02-2010 at 12:41 PM.
Well, the "Died Here:" message is clear: The input file you specified was not found. (Perhaps you should use the whole path?)
And the seg. fault is just because you're trying to close a file pointer that's null.
A better technique is to check immediately after the open statement.
<edit>
Note that the fclose(infile) after the outfile check (and at the end of your code, for both files) is, strictly speaking, redundant since exiting the program will automatically close any open files. But it does no harm.
</edit>
Last edited by PTrenholme; 03-02-2010 at 02:05 PM.
Well, the "Died Here:" message is clear: The input file you specified was not found. (Perhaps you should use the whole path?)
And the seg. fault is just because you're trying to close a file pointer that's null.
I've tried the full path and several ways to refer to it. It's in the same folder as it. Don't know why that's not working. And for now I've thrown the output file as just a random text file so it can still open and close it. Still getting the segfault though.
Quote:
And you're not using sed why?
Mostly because I don't know what I'm doing. Can I call sed from C? If so how? I'm going to need this text replacing part as parter of a larger c program that loops several times so I can't just use the sed once.
Well, the "Died Here:" message is clear: The input file you specified was not found. (Perhaps you should use the whole path?)
And the seg. fault is just because you're trying to close a file pointer that's null.
I've tried the full path and several ways to refer to it. It's in the same folder as it. Don't know why that's not working. And for now I've thrown the output file as just a random text file so it can still open and close it. Still getting the segfault though.
Quote:
And you're not using sed why?
Mostly because I don't know what I'm doing. Can I call sed from C? If so how? I'm going to need this text replacing part as parter of a larger c program that loops several times so I can't just use the sed once.
yes you can using some system() function, BUT try not to do that . If your program is already done in C , then use C. If you find yourself needing to call system commands , you might as well use the shell.
Here's a link to a screenshot of the whole set up. As far as I can see I've got the files in the correct spot, spelled correctly. I've tried using the full path name and that does nothing different. I keep getting an error that the first file can't be found. And the program was just recently recompiled. Any suggestions? Thanks.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.