gpt4 book ai didi

c - 逐字读取文件并输出空格

转载 作者:太空宇宙 更新时间:2023-11-04 05:53:24 24 4
gpt4 key购买 nike

输入的文本文件是这样的:

Hello my
name is
mark.
and
im
going
to
love
c!

代码:

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

int main(int argc, char *argv[]){
FILE *pFile;
char x[60];

pFile = fopen("test0.txt","r");

if(pFile != NULL){
while(fscanf(pFile, " %60s", x) == 1){
printf("%s",x);
}

}
}

输出文本文件为:

Hellomynameismark.andimgoingtolovec!

我希望输出如下:

Hello my name is mark. and im going to love c!

非常新的 C 程序员,所以只知道基础知识。

编辑----

   int main(int argc, char *argv[]){
FILE *pFile;
char x[60],line[60];

pFile = fopen("test0.txt","r");

while(!feof(pFile)){

fgets(line, 60, pFile);
strtok(line, "\r\n");
printf("%s", line );
}
fclose(pFile);

输出:

 Hello myname is mark.andim goingtolovec!

这不会在新行之间留下空格。但是,如果我取出 strtok 行,输出将是这样的:

Hello my
name is
mark.
and
im
going
to
love
c!

--编辑

 .sp 2
.ce
This is an example file for formatting.
.sp 1
The above line
was formatted using a .ce 1 command, which means 'centre
the following line',
so it should appear in the
centre of the page.
The paragraph was separated from the heading using
a .sp 1 command to create a single blank line.
There should also be two blank lines above the centred heading to make reading it slightly easier.

最佳答案

简单的答案是:

while(fscanf(pFile, " %59[^\n]%*c", x) == 1)

这里 %[^\n] 使用字符类 [stuff] 读取所有内容直到换行符。 %*c 只是读取并丢弃换行符,而不将其添加到 fscanf 的匹配计数中。

但是对于面向行的输入,您应该真正使用标准库提供的面向行函数之一(例如fgets或 POSIX getline)。

使用 fgets 和 strtok

正如您从评论中了解到的那样,feof 的使用只会让您感到悲伤。您将只想使用 fgets 的返回来确定文件结尾。这是一个将拼图的所有部分放在一起的示例:

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

#define MAXWDS 20
#define MAXCHR 60

int main (int argc, char **argv) {

char line[MAXCHR] = {0};
char *words[MAXWDS] = {NULL};
FILE *pFile = NULL;
size_t i, index = 0;

/* open file for reading (if provided), or read from stdin */
if (!(pFile = argc > 1 ? fopen (argv[1], "r") : stdin)) {
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}

while (fgets (line, 60, pFile))
{
char *p = line;

/* split line into tokens, stored in words[] */
for (p = strtok (p, " \r\n"); p; p = strtok (NULL, " \r\n")) {
words[index++] = strdup (p); /* allocate & copy */

if (index == MAXWDS) /* check pointer limit */
break;
}

}
if (pFile != stdin) fclose (pFile);

/* output in a single line */
for (i = 0; i < index; i++) {
printf (" %s", words[i]);
free (words[i]); /* free allocated memory */
}

putchar ('\n');

return 0;
}

编译

gcc -Wall -Wextra -o bin/fgets_strtok fgets_strtok.c 

输出

$ ./bin/fgets_strtok dat/hellomark.txt
Hello my name is mark. and im going to love c!

注意:简单地打印出单词之间有空格的行,只要每行中的每个单词之间已经有一个空格,就没有理由去麻烦要将每一行分成单独的单词,您可以简单地以空格分隔的方式打印出每一行的内容。您在使用 fgets 时遇到的唯一问题是它还会读取 newline(或 carriage return, newline)作为字符串的一部分。这很容易删除。您可以将整个读取循环替换为:

while (fgets (line, 60, pFile)) 
{
size_t len = strlen (line);

/* strip trailing newline (or carriage return newline ) */
while (len && (line[len-1] == '\n' || line[len-1] == '\r'))
line[--len] = 0; /* overwrite with null-terminating char */

words[index++] = strdup (line); /* allocate & copy */

if (index == MAXWDS) /* check pointer limit */
break;
}

输出

$ ./bin/fgets_mark <dat/hellomark.txt
Hello my name is mark. and im going to love c!

仅从文件(不是文件或标准输入)读取的标准方法

我很抱歉让您有点超前,包括打开文件(如果在命令行上提供)或从 stdin 读取(如果未提供文件名)的方法。标准方法是首先检查命令行上提供的参数数量是否正确,然后打开提供的文件名,验证它是否打开,然后处理输入。我所做的是将 三元运算符 放入 fopen 命令中。

pFile = argc > 1 ? fopen (argv[1], "r") : stdin

'=' 符号的右边是一个三元运算符,它只是if -> then -> else 的简写。它的作用是询问 argc > 1?如果测试 true,则 pFile = fopen (argv[1], "r");。如果 argc > 1 测试 false,则 pFile = stdin;

看看标准方式是否更有意义:

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

#define MAXWDS 20
#define MAXCHR 60

int main (int argc, char **argv) {

char line[MAXCHR] = {0};
char *words[MAXWDS] = {NULL};
FILE *pFile = NULL;
size_t i, index = 0;

/* validate sufficient input */
if (argc < 2 ) {
fprintf (stderr, "error: insufficient input, usage: %s filename\n", argv[0]);
return 1;
}

/* open file provided on command line for reading */
pFile = fopen (argv[1], "r");
if (!pFile) {
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}

while (fgets (line, 60, pFile)) /* read each line in file */
{
size_t len = strlen (line);

/* strip trailing newline (or carriage return newline ) */
while (len && (line[len-1] == '\n' || line[len-1] == '\r'))
line[--len] = 0; /* overwrite with null-terminating char */

words[index++] = strdup (line); /* allocate & copy */

if (index == MAXWDS) /* check pointer limit */
break;
}
if (pFile != stdin) fclose (pFile);

/* output in a single line */
for (i = 0; i < index; i++) {
printf (" %s", words[i]);
free (words[i]); /* free allocated memory */
}

putchar ('\n');

return 0;
}

关于c - 逐字读取文件并输出空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33490286/

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