#include <stdio.h>
int main ()
{
FILE * pFile;
char ch;
pFile = fopen ("G:\\ IJLAL.txt","w+");
while((ch = getchar())!=EOF)
putc(ch,pFile);
fclose(pFile);
return 0;
}
can you specify the problem??? I am totally new to fopen function. and please help me with this thing
你能指出这个问题吗???我对fopen函数完全陌生。请帮我做这件事。
3 个解决方案
#1
2
The getchar()
function returns an int
value, not a char
. By forcing it into a char
variable, you are making it impossible to detect the EOF.
函数的作用是:返回一个int值,而不是char。通过将其强制转换为char变量,就无法检测到EOF。
Just change char ch
to int ch
and it will work fine.*
只要把焦炭换成整型就行了。
- Actually, you will probably still have problems with the file name. Are you sure it should contain a space? And are you aware that
\\
is encoded as a single backslash character? - 实际上,您可能仍然会遇到文件名的问题。你确定它应该包含一个空间吗?您是否知道\\被编码为一个反斜杠字符?
#2
1
According to the C99 standard (7.19.1.3):
根据C99标准(7.19.1.3):
EOF which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;
EOF扩展到一个整数常量表达式,带有类型int和一个负值,由多个函数返回来表示文件结束,即不再从流中输入;
The point is that EOF is out of the range of unsigned char
. I am sure this was done intentionally so that all 256 basic and extended ASCII characters can still be returned, and are distinct from EOF
关键是,EOF不在无符号字符的范围内。我确信这是故意的,这样所有256个基本和扩展的ASCII字符仍然可以返回,并且与EOF不同。
#3
0
You can try this.
你可以试试这个。
1 - Check fopen() return values -> print errno value (perror)
1 -检查fopen()返回值->打印errno值(perror)
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int c;
bool run;
FILE *fd;
run = true;
fd = fopen("/tmp/file.txt", "w+");
if (fd == NULL)
{
perror("fopen");
return (1);
}
while (run == true)
{
c = getchar(); // Press ctrl + D for exit
run = (!feof(stdin) && !ferror(stdin));
if (run == true)
putc(c, fd);
}
fclose(fd);
}
And if you want you can see this link.
如果你希望你能看到这个链接。
#1
2
The getchar()
function returns an int
value, not a char
. By forcing it into a char
variable, you are making it impossible to detect the EOF.
函数的作用是:返回一个int值,而不是char。通过将其强制转换为char变量,就无法检测到EOF。
Just change char ch
to int ch
and it will work fine.*
只要把焦炭换成整型就行了。
- Actually, you will probably still have problems with the file name. Are you sure it should contain a space? And are you aware that
\\
is encoded as a single backslash character? - 实际上,您可能仍然会遇到文件名的问题。你确定它应该包含一个空间吗?您是否知道\\被编码为一个反斜杠字符?
#2
1
According to the C99 standard (7.19.1.3):
根据C99标准(7.19.1.3):
EOF which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;
EOF扩展到一个整数常量表达式,带有类型int和一个负值,由多个函数返回来表示文件结束,即不再从流中输入;
The point is that EOF is out of the range of unsigned char
. I am sure this was done intentionally so that all 256 basic and extended ASCII characters can still be returned, and are distinct from EOF
关键是,EOF不在无符号字符的范围内。我确信这是故意的,这样所有256个基本和扩展的ASCII字符仍然可以返回,并且与EOF不同。
#3
0
You can try this.
你可以试试这个。
1 - Check fopen() return values -> print errno value (perror)
1 -检查fopen()返回值->打印errno值(perror)
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int c;
bool run;
FILE *fd;
run = true;
fd = fopen("/tmp/file.txt", "w+");
if (fd == NULL)
{
perror("fopen");
return (1);
}
while (run == true)
{
c = getchar(); // Press ctrl + D for exit
run = (!feof(stdin) && !ferror(stdin));
if (run == true)
putc(c, fd);
}
fclose(fd);
}
And if you want you can see this link.
如果你希望你能看到这个链接。