问题描述
我已经制作了一个简单的程序来读取文件的内容。文件
指针在最开始时返回NULL,尽管文件是
present.Now if我修改我的程序并使用命令行参数
代替,我得到了所需的输出。现在我运行这个程序
with argv;然后我转回到没有命令行参数的那个
然后运行它,它可以工作。为什么它在开始时不起作用?
我正在使用Turbo C ++(DOS)。我在devc ++中尝试了它,但它根本没有
工作。
I have made a simple program to read the contents of a file.The FILE
pointer returns NULL at the very beginning eventhough the file is
present.Now if I modify my program and use command line arguments
instead, I get the desired output.Now after I have run this program
with argv; and I shift back to the one without command line arguments
and run it ,it works.Why doesn''t it work in the beginning itself?
I am using Turbo C++ (DOS).I tried it in devc++ but there it doesn''t
work at all.
推荐答案
你的错误可能在第42行。
请提供一些代码,我们很难猜到你的是什么
代码看起来像。除此之外,听起来你要么想要打开一个你没有权限打开文件的
,要么你给了b $ b fopen()一个不正确的第一个和/或第二个论点。
hbn
Your error is probably in line 42.
Please provide some code, we have a very hard to guessing what your
code looks like. Other than that it sounds like you are either trying
to open a file you don''t have permissions to open or you are giving
fopen() an incorrect first and/or second argument.
hbn
if((inf = fopen(filename," r"))== NULL)
{
fprintf(stderr,不能打开文件\%s\%s \ n,文件名,
strerror(错误));
退出(1);
}
注1:打印文件名很方便因为它可能不是那个
你认为它是。例如,你最后可能会有\ n。
注2:strerror()依赖于fprintf()设置errno。按照标准,这不是*
guarenteed
,但仍适用于许多实现。 (例如,
我遇到的每个unix和windows)
-
Nick Keighley
if ((inf = fopen (filename, "r")) == NULL)
{
fprintf (stderr, "can''t open file \"%s\" %s\n", filename,
strerror (errno));
exit (1);
}
note 1: printing the filename is handy because it may not be the one
you think it is. You may have \n on the end for instance.
note 2: strerror() relies on fprintf() setting errno. This is *not*
guarenteed
by the standard, but nevertheless works on many implementations. (eg.
every unix I''ve come across and windows)
--
Nick Keighley
好吧,我的通灵读者有点夸张,但它告诉我
问题是你忘记了,为了在
字符串文字中包含反斜杠,你需要使用两个反斜杠。
换句话说,你不能使用:
char filename [] =" c:\ filename.txt" ;;
而是:
char filename [] =" c:\\ filename.txt" ;;
或者,只需使用更好的形式:
char filename [] =" c:/filename.txt" ;;
要么是这样,要么第42行出错。在这种情况下,请
包含一些实际的源代码。
-
+ -------- ----------------- + -------------------- + ----------- ------------ +
| Kenneth J. Brody | | #include |
| kenbrody / at\spamcop.net | | < std_disclaimer.h |
+ ------------------------- + --------- ----------- + ----------------------- +
不要 - 邮寄给我:< mailto:Th ************* @ gmail.com>
Well, my psychic reader is a little flakey, but it''s telling me that
the problem is you forgot that, in order to include a backslash in a
string literal, you need to use two backslashes.
In other words, you cannot use:
char filename[] = "c:\filename.txt";
but rather:
char filename[] = "c:\\filename.txt";
Or, simply use the "better" form of:
char filename[] = "c:/filename.txt";
Either that, or there is an error on line 42. In that case, please
include some actual source code.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>
这篇关于文件指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!