使用linux内核函数在文件中找到最长的字符串并写入第二个文件

时间:2018-03-28 13:56:20

标签: c linux system-calls

嗨我想编写一个使用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] = '\0';
    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] = '\0';
            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);
}

1 个答案:

答案 0 :(得分:1)

这里有几个问题,但你不应该从标准输入中读取。事实上,超过一半的代码是不必要的。

我在这里有一个小例子,它坚持原始问题的措辞,至少显示代码的哪些部分需要削减:

/* 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;
}