从标准输入读取文件
Reading a file from stdin
我已经好几年没有用 C 编程了,所以我一直在努力做一个简单的 "get filename & path from stdin, read file, print file to stdout" 任务,我知道这不应该那么难,但是你。这是我的代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void) {
int c;
FILE *file;
//scanf("%s", filename);
char *filename;
filename = (char *)malloc(200 * sizeof(char));
read(STDIN_FILENO, filename, 200);
printf("%s", filename);
file = fopen(filename, "r");
if (file) {
while ((c = getc(file)) != EOF)
putchar(c);
fclose(file);
} else {
printf("File not found.");
}
printf("\n");
return(0);
}
我的代码继续简单地打印出 File not found.
,当我确实知道我的文件路径和所有内容都是正确的(不仅因为我真的把它从我的文件夹中删除并通过 Mac OSX El Capitan - 多么可爱的功能,而且)因为我有一个使用 scanf
的这个程序的不同版本,它找到了文件并且读取它非常好,(如你所见我已经在我的代码中注释掉了它)。
我正在编写的另一个程序只使用了这个程序,我去掉了 scanf
因为我认为它对该程序中的其他内容产生了负面影响,所以我希望能够使用 read()
如果有人对我如何解决这个问题或为什么它不起作用有任何建议,我将不胜感激,因为我已经在这里工作了几个小时,并且非常想继续我的 实际 我需要编码的程序!
多谢
您必须删除正在读取并存储到 filename
缓冲区中的 '\n'
换行符。
其中一个是包含 string.h
并在读取文件名后
char *newline = strchr(filename, '\n');
if (newline != NULL)
*newline = '[=10=]';
另外,使用 fgets()
而不是 read()
因为这样程序更便携。更重要的是,read()
不会添加 null
终止符,这对于将缓冲区用作字符串非常重要——例如 将其传递给 fopen()
— 正确。如果你想使用 read 试试这样的东西
ssize_t length;
char filename[200];
length = read(STDIN_FILENO, filename, sizeof(filename) - 1);
if (length <= 0)
return -1; // No input or input error
if (filename[length] == '\n')
filename[--length] = '[=11=]';
else
filename[length] = '[=11=]';
但除此之外,试试这个更简单的
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *file;
char filename[200];
char *newline;
if (fgets(filename, sizeof(filename), stdin) == NULL)
return -1; // Input error / EOF
newline = strchr(filename, '\n');
if (newline) // ? is a newline present?
*newline = '[=12=]';
printf("**%s**\n", filename); // ** will help checking for
// the presence of white spaces.
file = fopen(filename, "r");
if (file) {
int chr;
while ((chr = fgetc(file)) != EOF)
fputc(chr, stdout);
fclose(file);
} else {
printf("File not found.");
}
printf("\n");
return 0;
}
我已经好几年没有用 C 编程了,所以我一直在努力做一个简单的 "get filename & path from stdin, read file, print file to stdout" 任务,我知道这不应该那么难,但是你。这是我的代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void) {
int c;
FILE *file;
//scanf("%s", filename);
char *filename;
filename = (char *)malloc(200 * sizeof(char));
read(STDIN_FILENO, filename, 200);
printf("%s", filename);
file = fopen(filename, "r");
if (file) {
while ((c = getc(file)) != EOF)
putchar(c);
fclose(file);
} else {
printf("File not found.");
}
printf("\n");
return(0);
}
我的代码继续简单地打印出 File not found.
,当我确实知道我的文件路径和所有内容都是正确的(不仅因为我真的把它从我的文件夹中删除并通过 Mac OSX El Capitan - 多么可爱的功能,而且)因为我有一个使用 scanf
的这个程序的不同版本,它找到了文件并且读取它非常好,(如你所见我已经在我的代码中注释掉了它)。
我正在编写的另一个程序只使用了这个程序,我去掉了 scanf
因为我认为它对该程序中的其他内容产生了负面影响,所以我希望能够使用 read()
如果有人对我如何解决这个问题或为什么它不起作用有任何建议,我将不胜感激,因为我已经在这里工作了几个小时,并且非常想继续我的 实际 我需要编码的程序!
多谢
您必须删除正在读取并存储到 filename
缓冲区中的 '\n'
换行符。
其中一个是包含 string.h
并在读取文件名后
char *newline = strchr(filename, '\n');
if (newline != NULL)
*newline = '[=10=]';
另外,使用 fgets()
而不是 read()
因为这样程序更便携。更重要的是,read()
不会添加 null
终止符,这对于将缓冲区用作字符串非常重要——例如 将其传递给 fopen()
— 正确。如果你想使用 read 试试这样的东西
ssize_t length;
char filename[200];
length = read(STDIN_FILENO, filename, sizeof(filename) - 1);
if (length <= 0)
return -1; // No input or input error
if (filename[length] == '\n')
filename[--length] = '[=11=]';
else
filename[length] = '[=11=]';
但除此之外,试试这个更简单的
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *file;
char filename[200];
char *newline;
if (fgets(filename, sizeof(filename), stdin) == NULL)
return -1; // Input error / EOF
newline = strchr(filename, '\n');
if (newline) // ? is a newline present?
*newline = '[=12=]';
printf("**%s**\n", filename); // ** will help checking for
// the presence of white spaces.
file = fopen(filename, "r");
if (file) {
int chr;
while ((chr = fgetc(file)) != EOF)
fputc(chr, stdout);
fclose(file);
} else {
printf("File not found.");
}
printf("\n");
return 0;
}