LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C - Copy File Line By Line With Some Slight Changes (https://www.linuxquestions.org/questions/programming-9/c-copy-file-line-by-line-with-some-slight-changes-792663/)

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.

golmschenk 03-02-2010 11:35 AM

Oh and sorry, that code didn't copy the indentation as well as it should have...

EDIT: Nevermind, I just edited it.

PTrenholme 03-02-2010 12:59 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.
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>

tuxdev 03-02-2010 01:03 PM

And you're not using sed why?

Tinkster 03-02-2010 01:43 PM

Quote:

Originally Posted by tuxdev (Post 3882941)
And you're not using sed why?

Because re-inventing wheels is a good learning experience and
can be great fun!? :}

golmschenk 03-02-2010 02:03 PM

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.

golmschenk 03-02-2010 02:17 PM

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.

10110111 03-02-2010 02:19 PM

Learn to use debuggers, e.g. GDB.

smeezekitty 03-02-2010 07:26 PM

One of the most obvious things i can think of is the file name is spelled wrong.

ghostdog74 03-02-2010 07:43 PM

Quote:

Originally Posted by golmschenk (Post 3883018)
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.

golmschenk 03-03-2010 01:04 PM

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

golmschenk 03-03-2010 01:46 PM

Oh.... haha, wow.... that's extraordinarily embarrassing. Had the c and r switched in the spelling.... wow....


All times are GMT -5. The time now is 02:22 PM.