2011-10-07 79 views
3

好吧,這是令人尷尬的,儘管我討厭,我沒有別的選擇。我不認識C,但是我遇到了一個需要解決的問題,雖然我已經做了一些研究,但是我自己修改程序的時間太短了,所以我不得不忍受我的自豪感(和我猜測一些代表)尋求幫助。接受通配符打開文件

這是一個將unix文件轉換爲dos的簡單程序,唯一的問題是我需要它接受通配符(例如:c:/> unix2dos * .txt或file * .txt)沒什麼特別的。

這裏是我現在有代碼..

// UNIX2DOS - a Win32 utility to convert single text files from Unix to MS-DOS format. 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <io.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <sys/utime.h> 

#ifndef TRUE 
# define TRUE (1) 
# define FALSE (0) 
#endif 

#define R_CNTRL "rb" 
#define W_CNTRL "wb" 

struct stat s_buf; 

int u2dos (path) 
char *path; 
{ 
    FILE *in, *out; 
    int ch, 
     prev_ch= 0, 
     rval = FALSE; 
    char temppath [16]; 
    struct _utimbuf ut_buf; 
    strcpy (temppath, "./clntmp"); 
    strcat (temppath, "XXXXXX"); 
    mktemp (temppath); 
    if ((in=fopen (path, R_CNTRL)) == (FILE *) 0) 
     return TRUE; 
    if ((out=fopen (temppath, W_CNTRL)) == (FILE *) 0) 
    { 
     fclose (in); 
     return TRUE; 
    } 

    #define LF  0x0A 
    #define CR  0x0D 

    while ((ch = getc (in)) != EOF) 
    { 
     if ( (ch == LF) 
      && (prev_ch != CR) 
      && (putc(CR, out) == EOF) 
      || (putc(ch, out) == EOF) 
      ) 
     { 
      rval = TRUE; 
      break; 
     } 
     prev_ch= ch ; 
    } 

    if (fclose (in) == EOF) 
    { 
     rval = TRUE; 
    } 
    if (fclose (out) == EOF) 
    { 
     rval = TRUE; 
    } 
    ut_buf.actime = s_buf.st_atime; 
    ut_buf.modtime = s_buf.st_mtime; 
    if (_utime (temppath, &ut_buf) == -1) 
     rval = TRUE; 
    if (unlink (path) == -1) 
     rval = TRUE; 
    if (rval) 
    { 
     unlink (temppath); 
     return TRUE; 
    } 
    if (rename (temppath,path) == -1) 
    { 
     fprintf (stderr, "Unix2Dos: Problems renaming '%s' to '%s'.\n", temppath, path); 
     fprintf (stderr, "   However, file '%s' remains.\n", temppath); 
     exit (1); 
    } 
    unlink (temppath); 
    return FALSE; 
} 

void main (argc, argv) 
int argc; 
char **argv; 
{ 
    char *path; 
    while (--argc>0) 
    { 
     if (stat (path=*++argv, &s_buf) != -1) 
     { 
      printf ("Unix2Dos: Processing file %s ...\n", path); 
      if (u2dos (path)) 
      { 
       fprintf (stderr, "Unix2Dos: Problems processing file %s.\n", path); 
       exit (1); 
      } 
     } 
     else 
     { 
      fprintf (stderr, "Unix2Dos: Can't stat '%s'.\n", path); 
      exit (1); 
     } 
    } 
} 

我不能相信我已經離題了「向我發送DA codez」人無我已經成長爲鄙視,但現在看來這樣的一個是我最好的選擇。

我現在要去我的頭埋頭了。謝謝你的時間。

編輯

雖然暗示,我想我應該做的問題明顯。你能否提供一些幫助來修改這個程序來在Windows環境中接受通配符變量?

+2

我會說這不是「發送給我codez」,因爲在發佈之前你已經做了一些研究工作 – Flexo

+2

在這裏你會發現一些win32解決方案: http://stackoverflow.com/questions/1269480/globbing-in-cc-on-windows –

+0

這是否意味着在Windows或Unix上運行? –

回答

3

你必須重新發明一大堆輪子才能在文件名中使用通配符,甚至更多的是爲了移植它。嘗試使用cygwin的bashunix2dos

+0

這將是理想的,但我想在Win 2003 Server上自動執行此過程。如果它比你說的難,那麼我可能需要找到一個不同的解決方案,比如先將文件名改爲常量,然後運行該程序。 ..但這也可能有一些影響。感謝您的幫助+1 – Ryan

+0

+1 bash腳本可以用相對容易的自動化方式在cygwin中運行。我們已經在一些服務器上運行了cygwin,bash與windows shell和批處理文件相比是一個驚人的進步。 – asc99c

4

* nix shell(例如bash)自動擴展通配符參數。 Windows外殼不。但是Microsoft Visual C運行時庫確實有一個選項可以在程序啓動時自動執行。如何做到這一點解釋here。基本上你需要鏈接到setargv.obj。

4

由於這可以在Windows上運行,因此可以使用FindFirstFile,FindNextFile和FindClose。這裏有一個例子:http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx

+1

...因爲Windows API非常直觀,易於使用,當你對C語言有很少或沒有經驗的時候:)但是,的確,這些是在搜索和列出文件時使用的函數。 – Lundin

0

爲什麼不按照原樣使用程序,並讓一些(。)bat(ch)文件的魔術做擴展通配符?

關於如何做到這一點,請在這裏看到:http://support.microsoft.com/kb/68268/en-us

雖然你會開始對每個文件的新工藝進行轉換,這是寧靜昂貴,它會用語言讓你從周圍的黑客你似乎不喜歡...... ;-)