LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-02-2010, 11:32 AM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
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.

Last edited by golmschenk; 03-02-2010 at 11:41 AM.
 
Old 03-02-2010, 11:35 AM   #2
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Oh and sorry, that code didn't copy the indentation as well as it should have...

EDIT: Nevermind, I just edited it.

Last edited by golmschenk; 03-02-2010 at 11:41 AM.
 
Old 03-02-2010, 12:59 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
   FILE *infile;
   FILE *outfile;
   char line[50];
   if (!(infile = fopen("cubismxmarco.txt", "r"))) {
      perror("Died Here");
      exit(1);
   }
   if (!(outfile = fopen("cubismxmarcoreplace.txt", "w"))) {
      perror("Died Here");
      fclose(infile);
      exit(2);
   }
   while (fgets (line, 50, infile) != EOF) {
      fprintf(outfile,"%s\n", line);
   }
   fclose(infile);
   fclose(outfile);
   return 0;
<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 01:05 PM.
 
Old 03-02-2010, 01:03 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
And you're not using sed why?
 
Old 03-02-2010, 01:43 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
Originally Posted by tuxdev View Post
And you're not using sed why?
Because re-inventing wheels is a good learning experience and
can be great fun!? :}
 
Old 03-02-2010, 02:03 PM   #6
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Quote:
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.

Thanks.
 
Old 03-02-2010, 02:17 PM   #7
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Quote:
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.

Thanks.
 
Old 03-02-2010, 02:19 PM   #8
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 403

Rep: Reputation: 51
Learn to use debuggers, e.g. GDB.
 
Old 03-02-2010, 07:26 PM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
One of the most obvious things i can think of is the file name is spelled wrong.
 
Old 03-02-2010, 07:43 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by golmschenk View Post
Can I call sed from C?
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.
 
Old 03-03-2010, 01:04 PM   #11
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
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.

https://netfiles.umn.edu/users/olmsc019/xmacross.png
 
Old 03-03-2010, 01:46 PM   #12
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Oh.... haha, wow.... that's extraordinarily embarrassing. Had the c and r switched in the spelling.... wow....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line gnome file copy stefan_nicolau Linux - Software 2 05-30-2008 09:55 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration