C 低级别IO读()和写()
我正在编写一个程序,该程序分为多个部分,相互构建。第一个程序必须从文件中读取内容,并将内容按空格分割写入新文件。程序二应该把这些单词加上pig拉丁语,这是基于它是以元音还是辅音开头的规则,并根据它以哪一个开头在末尾加上一些字符串。我可以打开文件并读取内容,但我在查找和附加适当的规则以打印到新文件时遇到困难C 低级别IO读()和写(),c,string,io,system,C,String,Io,System,我正在编写一个程序,该程序分为多个部分,相互构建。第一个程序必须从文件中读取内容,并将内容按空格分割写入新文件。程序二应该把这些单词加上pig拉丁语,这是基于它是以元音还是辅音开头的规则,并根据它以哪一个开头在末尾加上一些字符串。我可以打开文件并读取内容,但我在查找和附加适当的规则以打印到新文件时遇到困难 int processingWord = 1; //0 is not in the middle of a word and 1 is in the middle ch
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
以下是一个应该有效的实现:
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;
}
还有一堆未使用的东西,比如bytes变量,事情肯定会更干净,但这应该按照您需要的方式工作。试着运行它,让我知道它是如何运行的,如果我还在这里,我今晚会更新任何错误
编辑
看起来我是倒着做的,只是交换元音而不是常量。我的猪拉丁语生锈了
好的,我制作了一个本地字符串,用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;
}
输出:
伊特雷
伊斯雷
赫泰
加州大学
伊甘托古猿盖,
奥雷
雷特盖
umpkinPay,
哈利埃
rownBay。您的评论说processingWord应该是0或1,但在您的程序中始终是1。你是想在某个时候把它设回零吗?是的,这就是我最初的逻辑。在检查元音等之前,我有一个if语句首先检查我是否在这行的末尾。当我检查temp2.data时,输出的都是jibberish和一堆?标志。我出于测试目的无意中删除了它。当前的输出是什么,看起来strcat没有做你认为它应该做的事情。使用调试器或sprintf,尽管如果使用strcat,它应该可以正常工作。同样,将缓冲区初始化为0,如下所示:char BUFFER[250]={0};当我使用缓冲区时,我的输出只是作为输出的随机符号。我没有得到像IIt、iis、E等那样的输出。。所以我越来越近了,但是我不能正确地附加光线。我已经更新了上面的代码。Printf strlen是什么,我怀疑您的字符串不是以null结尾的,因此strcat没有正确追加。初始化为零将有助于第一次写入iTray感谢您的响应。事实上,我正在使它与我的逻辑一起工作,尽管它是混乱和漫长的。虽然我能相对容易地理解你的代码。非常感谢您的回答,我将在尝试我的实现后尝试。一些清理:如果您将continue移到If之外,您可以消除后面的最外层,即更少的缩进。同样,你也可以在同一级别添加continue to if。我在工作时使用了2个空格缩进,codechef上的这4个空格缩进在我看来非常大。只要它起作用,我就让OP按照他想要的方式来做,我想这是他的家庭作业。@SaiPeri如果你最终使用了这个工作示例代码,你能接受这个评论作为你问题的答案吗。我最终得到了我的实现,因为我害怕因为学术上的不诚实而被抓住,但在我们的第二部分,我们必须利用线程,所以我一定会看看你的实现,让我的生活更轻松当我们接到下一个任务时。非常感谢你