使用 linux 内核函数在文件中查找最长的字符串并写入第二个文件
find longest string in a file with linux kernel function and write in second file
你好,我想写一个命令,使用 Linux 内核函数从一个文件中读取一个字符串,然后将最大长度的字符串写入第二个文件。/它在 c /
但我有一个问题,首先,程序从输入中获取字符串,而不是从系统中的文件中获取字符串,第二个是它显示输出而不是第二个文件。
我做了很多工作,但如果你能为我更正代码,我就做不到
/* Trivial file copy program using low-level I/O */
#include <fcntl.h>
#include <stdlib.h>
#define BSIZE 16384
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
int fin, fout; /* Input and output handles */
char buf[BSIZE];
int count;
if ((fin = open("foo", O_RDONLY)) < 0) {
perror("foo");
exit(1);
}
if ((fout = open("bar", O_WRONLY | O_CREAT, 0644)) < 0) {
perror("bar");
exit(2);
}
while ((count = read(fin, buf, BSIZE)) > 0)
{
char string[100], word[20], max[20], min[20], c;
int i = 0, j = 0, flag = 0;
printf("Enter string: ");
i = 0;
do
{
fflush(stdin);
c = getchar();
string[i++] = c;
} while (c != '\n');
string[i - 1] = '[=11=]';
for (i = 0; i < strlen(string); i++)
{
while (i < strlen(string) && !isspace(string[i]) && isalnum(string[i]))
{
word[j++] = string[i++];
}
if (j != 0)
{
word[j] = '[=11=]';
if (!flag)
{
flag = !flag;
strcpy(max, word);
strcpy(min, word);
}
if (strlen(word) > strlen(max))
{
strcpy(max, word);
}
if (strlen(word) < strlen(min))
{
strcpy(min, word);
}
j = 0;
}
}
printf("The largest word is '%s' and smallest word is '%s' in '%s'.\n", max, min, string);
return 0;
}
write(fout, buf, count);
close(fin);
close(fout);
}
这里有几个问题,但您基本上是在不应该的情况下从标准输入中读取数据。事实上,一半以上的代码是不必要的。
我这里有一个小例子,它坚持你原来问题的措辞,至少显示了代码的哪一部分需要删减:
/* Find the longest word in a text file (foo) and write that word out to another file (bar) */
/* A word is a group of characters delimited by white space */
/* A word is shorter than 16384 characters */
/* We are trying to use low-level system functions (e.g. open, close, read, write) */
/* instead of standard library functions (e.g. fopen, fclose, fread, fwrite) */
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BSIZE 16384
/* read a single word from a file */
static int read_word(int fd, char *buf)
{
int i = 0;
char c = 0;
int bytes = read(fd, &c, 1); /* extract a single character */
while (isspace(c) && bytes != 0) /* skip whitespace */
bytes = read(fd, &c, 1);
while (!isspace(c) && bytes != 0) {
buf[i++] = c; /* store character */
bytes = read(fd, &c, 1);
}
return i; /* return number of characters read */
}
int main()
{
int fin = open("foo", O_RDONLY);
if (fin < 0) {
perror("foo");
exit(1);
}
int fout = open("bar", O_WRONLY | O_CREAT, 0644);
if (fout < 0) {
perror("bar");
exit(2);
}
char buf[BSIZE] = {0};
char largest[BSIZE] = {0};
int bytes = 0;
int count = 0;
do {
bytes = read_word(fin, buf); /* read next word */
if (bytes > count) {
strncpy(largest, buf, BSIZE - 1); /* preserve largest word seen so far */
count = bytes;
}
memset(buf, 0, BSIZE);
} while (bytes > 0);
write(fout, largest, count); /* write largest word to file */
close(fin);
close(fout);
printf ("\nLargest word found was %s @ %d characters length.\n", largest, count);
return 0;
}
你好,我想写一个命令,使用 Linux 内核函数从一个文件中读取一个字符串,然后将最大长度的字符串写入第二个文件。/它在 c / 但我有一个问题,首先,程序从输入中获取字符串,而不是从系统中的文件中获取字符串,第二个是它显示输出而不是第二个文件。 我做了很多工作,但如果你能为我更正代码,我就做不到
/* Trivial file copy program using low-level I/O */
#include <fcntl.h>
#include <stdlib.h>
#define BSIZE 16384
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
int fin, fout; /* Input and output handles */
char buf[BSIZE];
int count;
if ((fin = open("foo", O_RDONLY)) < 0) {
perror("foo");
exit(1);
}
if ((fout = open("bar", O_WRONLY | O_CREAT, 0644)) < 0) {
perror("bar");
exit(2);
}
while ((count = read(fin, buf, BSIZE)) > 0)
{
char string[100], word[20], max[20], min[20], c;
int i = 0, j = 0, flag = 0;
printf("Enter string: ");
i = 0;
do
{
fflush(stdin);
c = getchar();
string[i++] = c;
} while (c != '\n');
string[i - 1] = '[=11=]';
for (i = 0; i < strlen(string); i++)
{
while (i < strlen(string) && !isspace(string[i]) && isalnum(string[i]))
{
word[j++] = string[i++];
}
if (j != 0)
{
word[j] = '[=11=]';
if (!flag)
{
flag = !flag;
strcpy(max, word);
strcpy(min, word);
}
if (strlen(word) > strlen(max))
{
strcpy(max, word);
}
if (strlen(word) < strlen(min))
{
strcpy(min, word);
}
j = 0;
}
}
printf("The largest word is '%s' and smallest word is '%s' in '%s'.\n", max, min, string);
return 0;
}
write(fout, buf, count);
close(fin);
close(fout);
}
这里有几个问题,但您基本上是在不应该的情况下从标准输入中读取数据。事实上,一半以上的代码是不必要的。
我这里有一个小例子,它坚持你原来问题的措辞,至少显示了代码的哪一部分需要删减:
/* Find the longest word in a text file (foo) and write that word out to another file (bar) */
/* A word is a group of characters delimited by white space */
/* A word is shorter than 16384 characters */
/* We are trying to use low-level system functions (e.g. open, close, read, write) */
/* instead of standard library functions (e.g. fopen, fclose, fread, fwrite) */
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BSIZE 16384
/* read a single word from a file */
static int read_word(int fd, char *buf)
{
int i = 0;
char c = 0;
int bytes = read(fd, &c, 1); /* extract a single character */
while (isspace(c) && bytes != 0) /* skip whitespace */
bytes = read(fd, &c, 1);
while (!isspace(c) && bytes != 0) {
buf[i++] = c; /* store character */
bytes = read(fd, &c, 1);
}
return i; /* return number of characters read */
}
int main()
{
int fin = open("foo", O_RDONLY);
if (fin < 0) {
perror("foo");
exit(1);
}
int fout = open("bar", O_WRONLY | O_CREAT, 0644);
if (fout < 0) {
perror("bar");
exit(2);
}
char buf[BSIZE] = {0};
char largest[BSIZE] = {0};
int bytes = 0;
int count = 0;
do {
bytes = read_word(fin, buf); /* read next word */
if (bytes > count) {
strncpy(largest, buf, BSIZE - 1); /* preserve largest word seen so far */
count = bytes;
}
memset(buf, 0, BSIZE);
} while (bytes > 0);
write(fout, largest, count); /* write largest word to file */
close(fin);
close(fout);
printf ("\nLargest word found was %s @ %d characters length.\n", largest, count);
return 0;
}