Ç - 与工作的fopen和fclose的fputc等 [英] C - Working with fopen, fclose, fputc etc
问题描述
我这有code终于在我的命令行一个参数工作,即一个文件为它一起工作,虽然我设计了$ C $ c。与它的无限数量的工作理念的文件。它所做的是利用含有空格隔开的单词的文本文件的一些X数字,并用替换空间\\ n
从而创造一个单词列表。虽然,它成功完成了第一个参数,它只是忽略了第二位。
另一个小问题似乎还打印出在年底一些垃圾信件,A Y带两个点它上面;我认为一些EOF符号,但我似乎无法阻止这种情况发生!
INT主(INT ARGC,字符** argv的){
FILE * fpIn,* fpOut;
INT I,J;
J = 1;
焦炭℃;
焦炭的myString [256]; 的printf(%D的argc);
的printf(\\ n);
的printf(以下参数传递给main());
对于(i = 1; I< ARGC,我++)的printf(%S的argv [I]);
的printf(\\ n); 而(argc--){
对于(i = 1; I< ARGC,我++){
fpIn = FOPEN(的argv [J],RB);
的snprintf(MyString的,256,%S〜[%D]。的argv [J],I);
fpOut = FOPEN(MyString的,WB);
而(C!= EOF){
C =龟etc(fpIn);
如果(isspace为(c))的
C ='\\ n';
的fputc(C,fpOut);
}
J ++;
}
}
返回0;
}
的的getchar()
, GETC()
和龟etc()
函数(或宏)返回一个 INT
,而不是字符
。
您必须使用:
INT℃;而((C =龟etc(fpIn))!= EOF)
{
如果(isspace为(c))的
C ='\\ n';
的fputc(C,fpOut);
}
想想吧;该功能必须能够返回任何有效的字符
的和的EOF(这是任何有效截然不同字符
价值,因此,根据定义,返回值不能是字符
...
如果您使用会发生什么字符
?
- 您原来的code没有初始化
C
之前对其进行测试(因此该循环可能会提早结束)。 - 您$ C $ C没有测试
C
阅读EOF后立即(所以它可能会打印垃圾字符,经常Y,拉丁小写字母Y带分音符,U + 00FF)。 - 如果你的
字符
类型是无符号的,你永远不会看到EOF。 - 如果你的
字符
类型有符号,一些有效的字符(经常再次Y))将作为PTED EOF misinter $ P $。
我似乎仍不能得到它的工作为多个参数虽然。
块引用>有你已经运行双回路问题:
INT I,J;
J = 1;而(argc--)
{
对于(i = 1; I< ARGC,我++)
{
fpIn = FOPEN(的argv [J],RB);
...过程fpIn ...
J ++;
}
}让我们假设你调用与两个文件名的命令;那么
的argc == 3
。在第一时间过去
,而
循环,的argc == 2后
。那么你做A
取值1;打开为
循环我的argv [1]
(因为Ĵ== 1
)。您处理的文件;然后增加Ĵ
2,之前也递增I
,也为2周围的<$ C $第二次C>为循环,I
== 2一样ARGC
,所以为
循环终止。该2 while循环递减
。但是,ARGC
再次1,但测试!= 0为
环套I = 1
,然后终止,因为我== ARGC
。在,而
循环递减ARGC
1,并重复。您可以使用
,而
环或为
循环,但你并不需要两个。所以,无论是:
为(I = I; I&LT; ARGC,我++)
{
...进程的argv [I] ...
}或者
而(--argc大于0)
{
...进程* argv的++ ...
}我会使用
为
循环。I've got this code finally working with a single argument on my command line, i.e. one file for it to work with, although I designed the code with the concept of it working with an unlimited number of files. What it does is take some X number of text files containing words separated by spaces, and replaces spaces with
\n
thus creating a list of words. Though, it successfully completes the first argument, it just ignores the 2nd.Another minor problem seems that it also prints out some garbage letter at the end, a Y with two dots above it; I assume some EOF symbol, yet I can't seem to stop that happening!
int main(int argc, char** argv) { FILE *fpIn, *fpOut; int i, j; j = 1; char c; char myString[256]; printf("%d", argc); printf("\n"); printf("The following arguments were passed to main(): "); for(i=1; i<argc; i++) printf("%s ", argv[i]); printf("\n"); while(argc--) { for(i = 1; i < argc; i++) { fpIn = fopen(argv[j], "rb"); snprintf(myString, 256, "%s~[%d]", argv[j], i); fpOut= fopen(myString, "wb"); while (c != EOF) { c = fgetc(fpIn); if (isspace(c)) c = '\n'; fputc(c, fpOut ); } j++; } } return 0; }
解决方案The
getchar()
,getc()
andfgetc()
functions (or macros) return anint
, not achar
.You must use:
int c; while ((c = fgetc(fpIn)) != EOF) { if (isspace(c)) c = '\n'; fputc(c, fpOut); }
Think about it; the functions must be able to return any valid
char
and EOF (which is distinct from any validchar
value. So, by definition, the return value can't be achar
...What happens if you use
char
?
- Your original code didn't initialize
c
before testing it (so the loop might break early).- Your code didn't test
c
immediately after reading EOF (so it might print a garbage character, often ÿ, LATIN SMALL LETTER Y WITH DIAERESIS, U+00FF).- If your
char
type is unsigned, you'd never see EOF.- If your
char
type is signed, some valid characters (often ÿ again)) will be misinterpreted as EOF.
I still can't seem to get it working for multiple arguments though.
The problem there is the double loop you have running:
int i, j; j = 1; while (argc--) { for (i = 1; i < argc; i++) { fpIn = fopen(argv[j], "rb"); ...process fpIn... j++; } }
Let us suppose you invoke the command with two file names; then
argc == 3
.After the first time past the
while
loop,argc == 2
. You then do afor
loop withi
taking the value 1; you openargv[1]
(becausej == 1
). You process that file; then incrementj
to 2, before also incrementingi
, also to 2. The second time around thefor
loop,i
== 2 as doesargc
, so thefor
loop terminates. The while loop decrementsargc
again to 1, but tests that2 != 0
. However, thefor
loop setsi = 1
and then terminates becausei == argc
. Thewhile
loop decrementsargc
to 1, and repeats.You can use a
while
loop or afor
loop, but you don't need both.So, either:
for (i = i; i < argc; i++) { ...process argv[i]... }
Or:
while (--argc > 0) { ...process *++argv... }
I'd use the
for
loop.这篇关于Ç - 与工作的fopen和fclose的fputc等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!