顯示廣告
隱藏 ✕
看板 million
作者 emulators (模擬器)
標題 unix2dos in c
時間 2016-06-23 Thu. 19:40:50


/*
** Author: emulators 2016-06-23
** This program will read unix text file and
** Generate another text file that can be
** Displayed properly in Windows with "_dos"
** String appended to original name and keep
** The original file extension.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void print_usage()
{
  char *usage = ""
  "This program will generate \r\n"
  "Another file with suffix \"_dos\" added. \r\n"
  "Usage: \r\n"
  "unix2dos TextFile\r\n";
  printf("%s", usage);
}

int main(int argc, char* argv[])
{
    int   c = 0;
    int   n = 0;
    FILE *read_fp;
    FILE *write_fp;
    char  outputName[200];
    char  fileType[5];
    char  ch;

    for(;c<200;c++)
      outputName[c] = 0;
    for(c=0;c<5;c++)
      fileType[c] = 0;
    if(2 == argc){
      n = strlen(argv[1]);
      strncpy(outputName, argv[1], n);
      for(c=n; c>=0; c--)
        if(outputName[c] =='.')
          break;
      strncpy(fileType, &outputName[c+1], 3);
      outputName[c]='\0';
      strncat(outputName,"_dos",4);
      strncat(outputName,".",1);
      strncat(outputName,fileType,3);
      read_fp  = fopen(argv[1],"r");
      write_fp = fopen(outputName,"w");

      if(read_fp == NULL)
        fprintf(stderr,"open file %s error.\r\n",argv[1]);
      else if( write_fp==NULL )
        fprintf(stderr,"write file %s error. \r\n",outputName);
      else{
        while((ch=fgetc(read_fp))!=EOF){
          if('\n'==ch){
            fputc('\r',write_fp);
          }
          fputc(ch,write_fp);
        }
      }
      fclose(read_fp);
      fclose(write_fp);
    }
    else{
      print_usage();
    }

    return 0;
}

--
※ 作者: emulators 時間: 2016-06-23 19:40:50
※ 看板: million 文章推薦值: 0 目前人氣: 0 累積人氣: 92 
分享網址: 複製 已複製
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇