接受通配符打开文件
好吧,这很尴尬,尽管我很讨厌,但我别无选择。我不懂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);
}
}
}
我不敢相信我已经离题到我已经开始鄙视的“向我发送代码”的人之一,但现在看来这是我最好的选择。
我现在要去把头埋进沙子里了。感谢您抽出时间。
编辑
虽然是暗示的,但我认为我应该让问题变得明显。您能否提供一些帮助来修改此程序以在 Windows 环境中接受通配符变量?
OK this is embarrassing and as much as I hate to, I have no other option. I don't know C but I was presented with a problem that I need to solve and although I've done some researching the time it would take me to modify the program myself is just too much so I have to swallow my pride (and I'm guessing some rep pts) to ask for help.
This is a simple program to convert a unix file to dos, the only problem is that I need it to accept wildcards (eg.. c:/>unix2dos *.txt or file*.txt ) Nothing fancy.
Here is the code that I have now..
// 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);
}
}
}
I cant believe I have digressed to one of the "Send me da codez" people I have grown to despise but right now it seems like this is my best option.
I'm going to go burry my head in the sand now. Thanks for your time.
EDIT
Although implied, I thought I should make the question obvious. Can you please provide some assistance in modifying this program to accept wildcard variables in a windows environment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
*nix shell(例如bash)自动扩展通配符参数。 Windows shell 没有。但 Microsoft Visual C 运行时库确实有一个选项可以在程序启动时自动执行此操作。 此处介绍了如何操作。基本上你需要链接到 setargv.obj。
*nix shells, (e.g. bash), automatically expand wildcard arguments. Windows shell does not. But Microsoft Visual C runtime library does have an option to do it automatically on your program's startup. How to do it is explained here. Basically you need to link against setargv.obj.
由于它将在 Windows 上运行,因此您可以使用 FindFirstFile、FindNextFile 和 FindClose。这里有一个示例:http://msdn.microsoft。 com/en-us/library/aa364418(VS.85).aspx
Since this will run on Windows, you can use FindFirstFile, FindNextFile and FindClose. There is an example here: http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx
您必须重新发明一大堆轮子才能在文件名中使用通配符,甚至需要更多的工作才能做到可移植。尝试使用 cygwin 的
bash
和unix2dos
。You'd have to reinvent a whole bunch of wheels to use wildcards in the file names, and even more to do it portably. Try using cygwin's
bash
andunix2dos
.为什么不按原样使用程序,并让一些 (.)bat(ch) 文件魔术来扩展通配符?
有关如何执行此操作的信息,请参阅此处:http://support.microsoft.com/kb/ 68268/en-us
虽然您要为每个要转换的文件启动一个新的进程,这非常昂贵,但它会阻止您使用您似乎不喜欢的语言...... ;-)
Why not take the program as is, and let some (.)bat(ch) file magic do the expansion of wildcards?
On how to do this please see here: http://support.microsoft.com/kb/68268/en-us
Although you'd be starting a new process for each file to be converted, which is quiet expensive, it'll keep you from hacking around using a language you do not seem to like ... ;-)