QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1182|回复: 3

各位高手请帮帮菜鸟.

[复制链接]
发表于 2002-11-28 00:51:11 | 显示全部楼层 |阅读模式
我正在学习linux系统下编程,用的是linux programming in 24 hours这本书.
书中第四章有个例程,是一个dos文本与unix文本格式互换的程序,主要就是去掉
或加上控制字符,该程序我在redhat8.0上编译通过,但程序似乎并未工作,请各位
前辈指教,最好能写出排错过程,谢了.下面是源程序:
[code:1]
/* dos_cvrt.c */

static const char rcsid[] =
    "$Id: dos_cvrt.c,v 1.7 1998/11/27 01:31:39 student1 Exp $";

/* REVISION HISTORY:
* $Log: dos_cvrt.c,v $
* Revision 1.7  1998/11/27 01:31:39  student1
* Now handles multiple input files, plus
* put basename code into its own Basename() 
* function.
*
* Revision 1.6  1998/11/27 01:01:28  student1
* Separated conversions into unix2dos() and
* dos2unix() functions.
*
* Revision 1.5  1998/11/23 05:32:21  student1
* Completed utility & tested ok.
*
* Revision 1.4  1998/11/23 05:04:23  student1
* Coded basename test
*
* Revision 1.3  1998/11/21 22:49:39  student1
* Demonstration of RCS substitutions
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/*
* Convert file named by pathname to DOS
* text format, on standard output:
*/
int
unix2dos(const char *pathname) {
    int ch;                 /* Current input character */
    FILE *in = 0;           /* Input file */

    if ( !(in = fopen(pathname,"r")) ) {
        fprintf(stderr,"Cannot open input file.\n");
        return 2;
    }

    while ( (ch = fgetc(in)) != EOF ) {
        if ( ch == '\n' )
            putchar('\r');
        putchar(ch);
    }

    fclose(in);
    return 0;
}

/*
* Convert file named by pathname, to
* UNIX text file format on standard output:
*/
int
dos2unix(const char *pathname) {
    int ch;                 /* Current input character */
    int cr_flag;            /* True when CR prev. encountered */
    FILE *in = 0;           /* Input file */

    if ( !(in = fopen(pathname,"r")) ) {
        fprintf(stderr,"Cannot open input file.\n");
        return 2;
    }

    cr_flag = 0;        /* No CR encountered yet */
    while ( (ch = fgetc(in)) != EOF ) {
        if ( cr_flag && ch != '\n' ) {
            /* This CR did not preceed LF */
            putchar('\r');
        }
        if ( !(cr_flag = ch == '\r') )
            putchar(ch);
    }

    fclose(in);
    return 0;
}

/*
* Return the pointer to the basename component
* of a pathname:
*/
char *
Basename(const char *path) {
    char *base_name = 0;

    if ( ( base_name = strrchr(path,'/') ) != 0 )
        ++base_name;                        /* Skip over '/' */
    else
        base_name = (char *) path;         /* No dir. */
    return base_name;
}

int
main(int argc,char **argv) {
    char *base_name = 0;    /* Basename of command */
    int rc = 0;             /* Command return code */
    int (*conv)(const char *pathname); /* Conv. Func. Ptr */
    int x;

    /*
     * Determine the basename of the command name. This
     * is necessary since this command could be invoked
     * with a pathname. For example, argv[0] could be
     * "/usr/local/bin/unix_cvrt" if it was installed
     * in the system that way.
     */
    base_name = Basename(argv[0]);

    /*
     * Check for missing input file:
     */
    if ( argc < 2 ) {
        fprintf(stderr,"Missing input file(s).\n");
        return 1;
    }

    /*
     * Now that we know the basename of the command
     * name used, we can determine which function we
     * must carry out here.
     */
    if ( !strcmp(base_name,"unix_cvrt") )
        conv = unix2dos;
    else
        conv = dos2unix;

    /*
     * Perform a text conversion
     */
    for ( x=1; x<argc; ++x )
        if ( (rc = conv(argv[x])) != 0 )
            break;      /* An error occured */

    return rc;
}
[/code:1]
发表于 2002-11-28 08:37:59 | 显示全部楼层
到Text Console下去运行,不要在x windows下运行。
回复

使用道具 举报

 楼主| 发表于 2002-11-28 16:16:21 | 显示全部楼层

似乎还是不行,下面是用gdb的调试结果.

编译命令gcc -o dos_cvrtdebug -d -Wall  dos_cvrt.c
调试命令gdb ./dos_cvrtdebug  test.txt (一个用于测试的dos文本)
调试输出:
GNU gdb Red Hat Linux (5.2.1-4)
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
(gdb) break main
Breakpoint 1 at 0x8048642
(gdb) r
Starting program: /home/snoopy/My source/c/dos_cvrtdebug

Breakpoint 1, 0x08048642 in main ()
(gdb) n
Single stepping until exit from function main,
which has no line number information.
0x420158d4 in __libc_start_main () from /lib/i686/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.

Program exited with code 01.
(gdb) q
回复

使用道具 举报

发表于 2002-11-28 16:45:08 | 显示全部楼层
没问题。
dos格式转成unix:程序名是unix_cvrt以外的任何名字,如dos_cvrt,用法dos_cvrt dos.txt >unix.txt
unix格式转成dos格式:程序名必须是unix_cvrt,用法unix_cvrt unix.txt >dos.txt
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-16 18:37 , Processed in 0.041396 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表