gpt4 book ai didi

c - 低级 IO read() 和 write()

转载 作者:行者123 更新时间:2023-11-30 14:45:26 29 4
gpt4 key购买 nike

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

          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.

编辑:processingWord 是一个想法,在检查元音之前,我必须检查我是否位于行尾。但逻辑不成立,输出全是胡言乱语。我当前的输出文件如下所示:

Itray

isray

theray

Zuccaray

Gigantopithecus,ray

orray

Greatray

Pumpkin,ray

Charlieray

Brown.ray

ray

最佳答案

这是一个应该有效的实现:

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

仍然有一堆未使用的东西,例如字节变量,并且事情当然可以更干净,但这应该按照您需要的方式工作。尝试运行它并让我知道它进展如何,如果我还在这里,今晚我会更新任何错误

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

好的,我制作了一个本地字符串来使用 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支付,哈莉岛罗恩湾。

关于c - 低级 IO read() 和 write(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53055946/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com