C 使用FGET在同一行上获取两行

C 使用FGET在同一行上获取两行,c,C,我试图在同一行上打印文件中的每两行 文件看起来像: Supposedly there are over one million words in the English Language We trimmed some fat to take away really odd words and determiners 我想得到: Supposedly there are over one, million words in the English Language We trimmed som

我试图在同一行上打印文件中的每两行

文件看起来像:

Supposedly there are over one
million words in the English Language
We trimmed some fat to take
away really odd words and determiners
我想得到:

Supposedly there are over one, million words in the English Language
We trimmed some fat to take, away really odd words and determiners   
到目前为止,我使用fgets()函数来显示.txt文件中的数据,但我不知道如何在同一行中获得.txt文件的每两行数据。我得到了相同的输出

char line[110]; // max numbers of characters in line
while (fgets(line, 109, stdin)) {
    printf("%s", line);
}
return 0;
我希望输出像我前面提到的那样:

Supposedly there are over one, million words in the English Language
We trimmed some fat to take, away really odd words and determiners

您需要从奇数行号的缓冲区中删除尾随的
\n
,然后打印它

  int lineNumber = 1;
  char line[110]; // max numbers of characters in line

  while(fgets(line, sizeof line, stdin))
  {
          if (lineNumber&1) {

               char *pos;
               if ((pos=strchr(line, '\n')) != NULL) {
                   *pos = '\0';
                    printf("%s ",line);
               }
           }
           else {
               printf (" %s", line);
           }
           lineNumber++;
   }
#包括
#包括
int main(){
字符行[110];//行中的最大字符数
对于(int i=0;fgets(第109行,标准输入法);i=(i+1)%2)
{
int l=strlen(直线);
如果(l>0)行[l-1]=0;
如果(i==0){
printf(“%s”,第行);
}否则{
printf(“%s\n”,第行);
}
返回0;
}
}

如果只看实际输出,一个问题应该很容易猜测。请阅读更多关于它的内容,以及它给缓冲区增加了什么。每秒钟、每三次、每四次等等都要做一些事情。我认为,时间是任何教科书、教程或课堂都应该很早就做的练习。关于模运算符
%
,您知道些什么?你以前用过吗?如果不是的话,我建议你读一下。我可以问你,有没有代码可以把每两行存储成一个字符串?
#include <stdio.h>
#include <string.h>

int main() {
    char line[110]; // max numbers of characters in line

    for(int i = 0; fgets(line, 109, stdin); i = (i+1) % 2)
    {
        int l = strlen(line);
        if (l > 0) line[l - 1] = 0;
        if (i == 0) {
            printf("%s, ",line);
        } else {
            printf("%s\n", line);
        }
        return 0;
    }
}
FILE *testData = fopen("C:\\path\\file.txt", "r+");
lineCounter = 0;
char *lineFileTest = new char[1024];
char *lineFileTestTemp = new char[1024];
while( fgets(lineFileTestTemp, 1024, testData) != NULL ) {
    if((lineCounter&1)==0?1:0){
        lineFileTest = lineFileTestTemp;
    }
    else{
        lineFileTest += lineFileTestTemp;
        printf("%s", lineFileTest);
    }
    lineCounter++;
}
fclose(testData);
if(lineFileTest || lineFileTestTemp){
    free(lineFileTest);
    free(lineFileTestTemp);
}