【问题标题】:Low level IO read() and write()低级 IO read() 和 write()
【发布时间】:2019-04-03 00:02:12
【问题描述】:

我正在开发一个由多个部分组成的程序,这些部分相互依赖。第一个程序必须从文件中读取并将由空格分割的内容写入新文件。程序二应该使用这个单词并根据它是以元音还是辅音开头的规则添加拉丁语,并根据它的开头在末尾附加一些字符串。我能够打开文件并从内容中读取,但我无法找到并附加正确的规则以打印到新文件。

          int processingWord = 1; //0 is not in the middle of a word and 1 is in the middle
  char c;
  int bytes; //Should be holding the position of the pointer in the file
  while((bytes = read(fileRead, &c, sizeof(c))) > 0) {
    //printf("%c", c);
    if(processingWord == 0) {
      processingWord = 1;
    }
    if(processingWord == 1) {
      //Figure out if a vowel or not
      if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
        //Increment the first counter
        shard1Count++;
      }
      //Get to the end of the string
      if(c == '\n') {
        c = 'r';
        write(fileWrite, &c, sizeof(c));
        c = 'a';
        write(fileWrite, &c, sizeof(c));
        c = 'y';
        write(fileWrite, &c, sizeof(c));
        c = '\n';
        write(fileWrite, &c, sizeof(c));
        processingWord = 0;
      }
    }
    write(fileWrite, &c, sizeof(c));
  }

如果它以元音开头,我正在尝试在单词末尾查找并附加新的“ray”字符串。文本文件如下所示

It
is
the
Zucca
Gigantopithecus,
or
Great
Pumpkin,
Charlie
Brown.

并且输出在新文件中应该是这样的

Itray 
    isray 
    hetay 
    uccaZay 
    igantopithecusGay, 
    orray
    reatGay
    umpkinPay, 
    harlieCay 
    rownBay.

编辑:processsingWord 是一个想法,我必须在检查元音之前检查我是否在行尾,什么不是。但是逻辑没有得到锻炼,输出都是胡言乱语。 我当前的输出文件如下所示:

Itray

isray

theray

Zuccaray

Gigantopithecus,ray

orray

Greatray

Pumpkin,ray

Charlieray

Brown.ray

ray

【问题讨论】:

  • 您的评论说processingWord 应该是 0 或 1,但在您的程序中它始终是 1。您的意思是在某个时候将其设置回零吗?
  • 是的,这就是我最初的逻辑。在检查元音等之前,我有一个 if 语句首先检查我是否在行尾。当我检查我的 temp2.​​data 时,输出全是乱码,带有一堆“?”分数。出于测试目的,我不小心删除了它。
  • 当前输出是什么,看起来 strcat 没有做你认为应该做的事情。要么使用调试器,要么使用 sprintf,尽管如果你使用 strcat,它应该可以正常工作。 stackoverflow.com/questions/2674312/… 还将您的 BUFFER 初始化为 0,如下所示:char BUFFER[250] = {0};
  • 当我使用缓冲区时,我的输出只是随机符号作为输出。我还没有得到像 IIt、iis、thee 等的输出。所以我越来越近了,但我无法正确附加“射线”。我已经更新了上面的代码。
  • Printf strlen 是什么,我怀疑你的字符串不是空终止的,所以 strcat 没有正确附加。初始化为零应该有助于第一次写入“itray”

标签: c string io system


【解决方案1】:

这是一个应该可以工作的实现:

void doStuff(void);
int startsWithVowel(char c);
int isALetter(char c);


void doStuff(){
    int processingWord = 0; 
    int already_latin = 0;
    char c = 0;
    char first_letter = 0;
    while(read(fileRead, &c, sizeof(c)) > 0) {
        if(processingWord == 0) {
            processingWord = 1;
            if(!startsWithVowel(c)){ //append constants to the end of the word in pig latin *EDIT*
                first_letter = c;
                continue;//Here we do not fall through and write
            }
        }
        else{
            if(isALetter(c)){ //This is the general case of just writing the read character
                write(fileWrite, &c, sizeof(c));
            }
            else if(c != '\n'){ //Here is handling for , and . special characters
                if(isALetter(first_letter)){ //we hit a .  or , with a vower word, need to add first letter then "ray"
                    write(fileWrite, &first_letter, sizeof(first_letter));
                }
                write(fileWrite, "ray", sizeof("ray"));
                write(fileWrite, &c, sizeof(c));
                already_latin = 1;
            }
            else if(c == '\n') { //here is the end of the string
                if(isALetter(first_letter)){
                    write(fileWrite, &first_letter, sizeof(first_letter));
                }
                if(!already_latin){
                    write(fileWrite, "ray", sizeof("ray"));
                }
                write(fileWrite, &c, sizeof(c));

                processingWord = 0; //reset all the flags for the next word.
                first_letter = 0;
                already_latin = 0;
            }//end of '\n'
        }//end of if/else block
    }//end of while loop
}//end of function


/* =========================================================
return true (1) if the character is a vowel and 0 otherwise
============================================================ */

int startsWithVowel(char c){
    if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
        return 1;
    }
    return 0;
}

/* =========================================================
return true (1) if the character is a letter and 0 otherwise
============================================================ */
int isALetter(char c){
    if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))){
        return 1;
    }
    return 0;
}

还有一堆未使用的东西,比如字节变量,当然可以更干净,但这应该可以按照您的需要工作。试着运行它,让我知道它是怎么回事,如果我还在这里,今晚我会更新任何错误

编辑 看起来我是倒着做的,只交换元音(而不是常量)。我的猪拉丁文生锈了。

好的,我创建了一个本地字符串来使用 codechef.com/ide 在线测试解析,您可以将其复制并粘贴到那里进行验证。将 printfs 更改为 writes,它模仿 printfs,我认为你很好:

#include <stdio.h>
#include <string.h>

void doStuff(void);
int startsWithVowel(char c);
int isALetter(char c);

char * str = "It\nis\nthe\nZucca\nGigantopithecus,\nor\nGreat\nPumpkin,\nCharlie\nBrown.";

int main(void) {

    doStuff();

    return 0;
}

void doStuff(){
    int processingWord = 0; 
    char c = 0;
    char first_letter = 0;
    int already_latin = 0;
    //while(read(fileRead, &c, sizeof(c)) > 0) {
    while(strlen(str) > 0){        //Made local for testing, no file io here
        c = str[0];
        str++;                    //end of local nonsense you wont have to use
        if(processingWord == 0) {
            processingWord = 1;
            if(!startsWithVowel(c)){
                first_letter = c;
                continue;//Here we don not fall through and write
            }
        }
        if(processingWord == 1) {
            if(isALetter(c)){ //This is the general case of just writing the read character
                //write(fileWrite, &c, sizeof(c));
                printf("%c",c);
                //printf(" SHOULD PRINT FIRST LETTER VOWEL HERE ");
            }
            else if(c != '\n'){ //Here is handling for , and . special characters
                if(isALetter(first_letter)){ //we hit a .  or , with a vower word, need to add first letter then "ray"
                    //write(fileWrite, &first_letter, sizeof(first_letter));
                    printf("%cay%c",first_letter,c);
                }
                else{
                    //write(fileWrite, "ray", sizeof("ray"));
                    //write(fileWrite, &c, sizeof(c));
                    printf("ray%c", c);   
                }
                already_latin = 1;
            }
            else if(c == '\n') { //here is the end of the string
                if(!already_latin){
                    if(isALetter(first_letter)){
                        //write(fileWrite, &first_letter, sizeof(first_letter));
                        printf("%cay",first_letter);
                        //printf(" SHOULD PRINT FIRST LETTER CONSTANT HERE  ");
                    }
                    else{
                        //write(fileWrite, "ray", sizeof("ray"));
                        printf("ray");
                    }
                }
                //write(fileWrite, &c, sizeof(c));
                printf("%c", c);
                processingWord = 0;
                first_letter = 0;
                already_latin = 0;
            }//end of '\n'
        }//end of if/else block
    }//end of while loop
}//end of function


/* =========================================================
return true (1) if the character is a vowel and 0 otherwise
============================================================ */

int startsWithVowel(char c){
    if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
        return 1;
    }
    return 0;
}

/* =========================================================
return true (1) if the character is a letter and 0 otherwise
============================================================ */
int isALetter(char c){
    if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))){
        return 1;
    }
    return 0;
}

输出: 伊特雷 艾瑞 海泰 乌卡扎伊 igantopithecus同性恋, 奥雷 重男轻女 umpkin支付, 哈利凯 罗恩湾。

【讨论】:

  • 感谢您的回复。我实际上正在使它与我的逻辑一起工作,尽管它很混乱而且很长。尽管我能够相对容易地遵循您的代码。非常感谢您的回答,我会在尝试实施后尝试。
  • 一些清理:如果你将continue移到它的if之外,你可以消除最外层的else(即减少缩进)。同样,您可以将continue 添加到同一级别的if
  • 我在工作中使用了 2 个空格缩进,codechef 上的这 4 个空格缩进对我来说看起来很大,哈哈。只要效果不佳,就让 OP 随心所欲地去做,我认为这是他的功课。
  • @SaiPeri 如果您最终使用此工作示例代码,您能否接受此评论作为您问题的答案。
  • 我最终得到了我的实现,因为害怕因学术不诚实而被抓,但对于我们的第二部分,我们必须利用线程,所以我一定会看看你的实现,以便在我们得到下一个时让我的生活更轻松任务。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
相关资源
最近更新 更多