LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-01-2012, 10:36 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
fputc: C program that writes characters out of nowhere.


Hi:

I have the source of a C program (that listed bellow) which was intended to be compiled and executed under MS-DOS. Under MS-DOS version 5.00 it compiles without errors and runs as expected. It converts the Unix style end-of-line sequence 0x0a to MS-DOS style end-of-line sequence 0x0d 0x0a (linefeed to carriage return + linefeed).

There is an odd thing about this program. Nowhere in it is the carriage return written to the output file. There are two files: an input file, the Unix style file, and the output file, the DOS style file. Characters are read by the fgetc statement and written by fputc. The only way I see to understand this program is that, under DOS,

fputc(ch, outfile)

writes 0x0d 0x0a when ch == 0x0a. And this would match the program's logic. If you can unveil the mystery please let me know.

About compiling it and see if it works in linux I say the program cannot do magic.

Code:
#include        <stdio.h>
#include        <sys.h>
#include        <stdlib.h>

/*
 *      Unix To DOS text file conversion utility. 
 *
 *      Converts the Unix style end-of-line sequence 0AH
 *      to MS-DOS style end-of-line sequence 0DH 0AH
 */

#define TMPNAME "$U2D$TMP.$$$"

/*
 *      int     fixfile(char * filename)
 *
 *      "Fix" the text file specified by "filename".
 *      Returns 0 on error, non zero otherwise.
 */

int
fixfile(char * filename)
{
        FILE *          infile;
        FILE *          outfile;
        char            ch;
        unsigned long   in, out;

        printf("u2d: %s: ", filename);
        fflush(stdout);
        infile = fopen(filename, "r");  /* try to open input file */
        if (!infile) {
                printf("not found\n");
                return 0;
        }
        outfile = fopen(TMPNAME, "w");  /* try to open scratch file */
        if (!outfile) {
                fclose(infile);
                printf("unable to open temp file %s\n", TMPNAME);
                return 1;
        }
        in = out = 0;
        while ((ch = fgetc(infile)) != EOF) {   /* while not end of file */
                if (fputc(ch, outfile) == EOF) {
                        fclose(infile);
                        fclose(outfile);
                        printf("error writing to temp file %s\n", TMPNAME);
                        return 1;
                }
                ++in;
                ++out;
                if (ch == '\n')
                        ++out;
        }
        fclose(infile);
        fclose(outfile);
        if (remove(filename) != 0) {
                printf("unable to delete %s\n", filename);
                return 1;
        }
        if (rename(TMPNAME, filename) != 0) {
                printf("unable to rename %s to %s\n", TMPNAME, filename);
                return 1;
        }
        printf("converted: %lu bytes read, %lu bytes written\n", in, out);
        return 0;

}

main(int argc, char ** argv)
{
        int     i;

        if (argc == 1) {
                argv = _getargs(0, "u2d");
                argc = _argc_;
        }
        if (argc == 1) {
                fprintf(stderr, "u2d: usage: u2d file1 file2 ...\n");
                fprintf(stderr, "u2d: wildcards, e.g. *.c are expanded\n");
                exit(1);
        }
        for (i = 1; i != argc; i++) {
                if (fixfile(argv[i])) {
                        fprintf(stderr, "u2d: aborted!\n");
                        exit(1);
                }
        }
}
I attach the same program in case it's more legible.
Attached Files
File Type: txt u2d.txt (1.8 KB, 14 views)

Last edited by XavierP; 07-03-2012 at 03:11 PM.
 
Old 07-02-2012, 06:06 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I'm guessing that '0xa' is being interpreted as '\n' (newline) and automatically being converted to '0xd 0xa' based on the platform you're running on.
 
Old 07-02-2012, 06:56 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
I guessed the same thing. But then, one wouldn't have full control. There would be some character operations on streams that would be impossible to acomplish, save through the use of low-level i/o routines. Thanks for having taken the trouble to read the program.

Last edited by stf92; 07-02-2012 at 08:26 AM.
 
Old 07-02-2012, 09:23 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,783

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
It's because you opened the file in text mode, if you use binary mode, fopen(filename, "rb"), then no translation will occur.

See What's the difference between text and binary I/O?

Quote:
I attach the same program in case it's more legible.
Use [CODE][/CODE] tags to make it legible.

Last edited by ntubski; 07-02-2012 at 09:26 AM. Reason: [code][/code] tags please
 
Old 07-02-2012, 09:39 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
So, when the program makes i/o with fputc somewhere in the way a call to the DOS routines happens and these are who make the translation! Very grateful for your useful post.
 
Old 07-03-2012, 03:12 PM   #6
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


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
[SOLVED] Special Characters in program argument rpcaldeira Programming 7 09-27-2011 05:11 PM
program regarding special characters in c++ ajcapri Programming 5 12-04-2009 12:47 AM
cdrecord writes iso's without adding data, unlike any other burning program confirm? WOTHed Linux - Software 8 06-29-2008 05:02 AM
apache program writes files in the apache group, how can I change? muskiediver Linux - Security 5 01-17-2007 12:27 PM
putc and fputc - and other confusion Linh Programming 8 07-21-2003 06:54 AM

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

All times are GMT -5. The time now is 06:07 PM.

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