[SOLVED] A compiled C program copied using a self-written program, failed to work
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.
A compiled C program copied using a self-written program, failed to work
Hi
I have a program like this:
Code:
#include <stdio.h>
/* print Fahrenheir-Celcius table
* for fahr = 0, 20, ..., 300 */
main()
{
int fahr, celsius;
int lower, upper, step;
lower = 0; /* lower limit of temperature scale */
upper = 300; /* upper limit of temperature scale */
step = 20; /* step size */
fahr = lower;
while (fahr <= upper) {
celsius = 5*(fahr - 32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}
I compiled it using the command
gcc ./convert.c -o convert
and ran the file and everthing was OK.
Next I wrote my own mycopy.c program as below
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("The program requires exactly 2 file names.");
return 0;
}
FILE *old_file, *new_file;
old_file = fopen(argv[1], "r");
new_file = fopen(argv[2], "w");
int i;
char c;
c = fgetc(old_file);
while (c != EOF)
{
putc(c,new_file);
c = fgetc(old_file);
}
fclose(new_file);
return 1;
}
I compiled this file, output to a filenamed mycopy. I tested and it worked fine with text file. But if I used it to copy the file convert above, using the command
mycopy convert newconvert
chmod +x newconvert
Then the file newconvert fail to run with the error message:
Segmentation failed.
You can't use text-mode file buffers to copy a file because it will skip certain binary characters. Try ls -l convert newconvert to see the size difference. You need to open the files with open, then use read and write.
The mode string can also include the letter 'b' either as a last char-
acter or as a character between the characters in any of the two-char-
acter strings described above. This is strictly for compatibility with
C89 and has no effect; the 'b' is ignored on all POSIX conforming sys-
tems, including Linux. (Other systems may treat text files and binary
files differently, and adding the 'b' may be a good idea if you do I/O
to a binary file and expect that your program may be ported to non-Unix
environments.)
Same here; that's why I suggested using the system calls directly. It's possible that fread and fwrite would work, but there's no sense using buffers if no parsing or formatting needs to be done.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.