LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 06-26-2013, 09:06 PM   #1
trainee
Member
 
Registered: Dec 2004
Distribution: Slackware
Posts: 142

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

Would you please let me know why?
Thanks.
 
Old 06-26-2013, 09:15 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

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

Kevin Barry

Last edited by ta0kira; 06-26-2013 at 09:22 PM.
 
1 members found this post helpful.
Old 06-27-2013, 02:58 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
As a start, try these commands
Code:
ls -l <original_executable> <copied_executable>
cmp <original_executable> <copied_executable>
 
1 members found this post helpful.
Old 06-27-2013, 03:20 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I agree with ta0kira.

Use fopen to open the file for read binary "rb" and the other for "wb", and then use fread and fwrite to copy the file.
 
1 members found this post helpful.
Old 06-27-2013, 03:38 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
I'd suggest that also if it weren't unix:

Quote:
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.)
 
1 members found this post helpful.
Old 06-27-2013, 03:40 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Anyway, this will stop at the first \xff (0FFH) character:

Code:
    char c;
    c = fgetc(old_file);
fixed:

Code:
    int c;
    c = fgetc(old_file);
 
3 members found this post helpful.
Old 06-27-2013, 09:35 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
I'd suggest that also if it weren't unix:
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.

Kevin Barry
 
1 members found this post helpful.
Old 06-27-2013, 09:40 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Well, there is a reason why fgetc is defined as
Code:
int fgetc(FILE *stream);
It's so that you could tell apart EOF (=-1) and normal characters (0..255)
 
1 members found this post helpful.
Old 06-27-2013, 07:53 PM   #9
trainee
Member
 
Registered: Dec 2004
Distribution: Slackware
Posts: 142

Original Poster
Rep: Reputation: 16
I tried the suggestion

Code:
int fgetc(FILE *stream);
And things work. I have not tried the fread, fwrite or the open, read, write suggestion. I'll try it and report the result later. Thanks everybody.

EDIT: the open, read, write works too.

Last edited by trainee; 06-27-2013 at 08:24 PM. Reason: Update
 
  


Reply



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
I have written a C++ program in windows and it fails to work on linux Corps Linux - Newbie 4 08-10-2009 12:35 PM
program compiled under kernel <=2.4 don't work under kernel 2.6 mihaimdl Linux - Software 3 11-08-2005 06:09 PM
Will a program compiled on one distro work on another? Razza2004 Linux - Newbie 1 05-08-2004 01:56 PM
have you ever written a program for linux? today53 General 33 12-05-2003 06:54 PM
a c program written for linux will work in sco unix? cybercop12us Linux - General 3 04-25-2002 07:44 AM

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

All times are GMT -5. The time now is 10:11 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