好吧,這是令人尷尬的,儘管我討厭,我沒有別的選擇。我不認識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環境中接受通配符變量?
我會說這不是「發送給我codez」,因爲在發佈之前你已經做了一些研究工作 – Flexo
在這裏你會發現一些win32解決方案: http://stackoverflow.com/questions/1269480/globbing-in-cc-on-windows –
這是否意味着在Windows或Unix上運行? –