我有一个文本文件。文本文件包含字符串。我想使用 C 编程从句末删除 space
I've a text file. The text file contains strings. I wanna remove space from end of the sentence by using C programming
关于我的问题有一些问题,但我无法从这些相关问题中找到确切的答案。这就是我问的原因。请尝试理解并为我提供一个功能,通过它我将能够得到正确的答案。
我有一个文本文件。说是 "filename.txt"
该文件包含一些字符串。并且在每句话的末尾都有一个额外的space。
例如:输入文件:
high blood sugar levels (space)
type 2 diabetes definition (space)
high blood sugar symptoms
glucose tolerance test
symptoms of high blood sugar
这里每句话后面都有一个额外的space。我想删除那些 spaces.
我想生成这样的输出文件:
high blood sugar levels,
type 2 diabetes definition,
high blood sugar symptoms,
glucose tolerance test,
symptoms of high blood sugar
我试过了,但是 space 仍然在逗号 (,) 之前。
#include <stdio.h>
#include <stdlib.h>
int main()
{
fopen("out.txt", "w", "stdout");
char ch, keywords[25];
FILE *fp;
fp = fopen("keywords.txt","r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", "keywords.txt");
while( ( ch = fgetc(fp) ) != EOF )
{
if(ch=='\n') ch=',';
printf("%c",ch);
}
fclose(fp);
return 0;
}
也请指导我如何将我的结果打印到输出文件。我将使用这个简单的程序来完成我的个人任务。这就是为什么我试图找到结果。请帮我。
您没有对尾随的白色 space 执行任何操作(出现在句子结束之后和换行符“\n”之前的那些)。您所做的只是错误地尝试(因为您只使用 printf
不会写入文件,所以无论如何都不会更改您的 txt 文件)试图用逗号替换 '\n'
,如果完成正确将导致以下输出仅包含一行且所有 space 都完好无损:
high blood sugar levels (space),type 2 diabetes definition (space),high blood sugar symptoms,glucose tolerance test,symptoms of high blood sugar,
为了摆脱尾随的 spaces,你必须在每次看到它们时计算 spaces 的数量,如果你得到一个单词而不是换行符,则重置计数.否则,如果您在计算 whitespace 时得到一个换行符,请使用该计数值打印那么多 backspaces '\b'
,然后打印一个逗号。请注意,除了添加逗号的位置外,您没有删除任何白色spaces,因此每行的换行符在尾随白色spaces 后仍位于同一位置。棘手的部分是没有尾随 space 的极端情况,即 '\n'
紧跟在句子的最后一个字符之后。在最后一个字符和换行符之间插入一个逗号,将需要在文件中移动数据以使该逗号成为 space。因此,最好的方法是使用临时文件,在删除尾随 spaces 并添加逗号后从输入文件逐行构建它。您可能想使用 fgets
和 fputs
这是我建议您可以做的事情
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char ch;
int length, index;
char *tgt = NULL;
// open sourse file
fp = fopen("keywords.txt","r");
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
// determine length of the source file
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
fseek(fp, 0L, SEEK_SET);
// allocate memory buffer
tgt = malloc( length );
if( tgt == NULL )
{
perror("Error while allocating memory.\n");
exit(EXIT_FAILURE);
}
// initialize memory buffer
memset( tgt, 0, length );
// read file to memory buffer
fread( tgt, 1, length, fp );
// close sourse file
fclose(fp);
// handle contents of the source file
printf("The contents of %s file are :\n", "keywords.txt");
index = 0;
while( index < length && tgt[index] != '[=10=]' )
{
if( tgt[index] == ' ' && index + 1 < length
&& tgt[index + 1] == '\n' )
{
printf( ",\n" );
index += 2;
}
else
{
printf( "%c", tgt[index] );
index += 1;
}
}
// release memory
free( tgt );
getchar();
return 0;
}
基本思路非常简单 - 您将源文件读入内存缓冲区,然后您可以自由地使用它做任何您想做的事。
您可以逐行而不是逐字符读取文件。
然后我们可以检查该行以检查 space 后跟换行符条件并将 space 替换为 , (您仍然需要换行符,以您的示例为例)。
我会这样做:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
FILE *in ,*out;
ssize_t linelength;
size_t alloc_size;
char *line = NULL;
if (NULL == (in = fopen("keywords.txt", "r"))){
perror("failed to open input file\n");
return 1;
}
if (NULL == (out = fopen("out.txt", "w"))){
perror("failed to open outputfile\n");
return 1;
}
while (-1 != (linelength= getline(&line, &alloc_size, in))){
/* line contains the whole line ('[=10=] terminated), including final '\n'
the length( as a string) is linelength */
if (linelength < 2) continue; /* too short line */
if (line[linelength -1] == '\n' && line[linelength-2] == ' '){
line[linelength-2] = ',';
}
fprintf(out, "%s",line);
}
fclose(in);
fclose(out);
free(line);
return 0;
}
关于我的问题有一些问题,但我无法从这些相关问题中找到确切的答案。这就是我问的原因。请尝试理解并为我提供一个功能,通过它我将能够得到正确的答案。
我有一个文本文件。说是 "filename.txt" 该文件包含一些字符串。并且在每句话的末尾都有一个额外的space。
例如:输入文件:
high blood sugar levels (space)
type 2 diabetes definition (space)
high blood sugar symptoms
glucose tolerance test
symptoms of high blood sugar
这里每句话后面都有一个额外的space。我想删除那些 spaces.
我想生成这样的输出文件:
high blood sugar levels,
type 2 diabetes definition,
high blood sugar symptoms,
glucose tolerance test,
symptoms of high blood sugar
我试过了,但是 space 仍然在逗号 (,) 之前。
#include <stdio.h>
#include <stdlib.h>
int main()
{
fopen("out.txt", "w", "stdout");
char ch, keywords[25];
FILE *fp;
fp = fopen("keywords.txt","r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", "keywords.txt");
while( ( ch = fgetc(fp) ) != EOF )
{
if(ch=='\n') ch=',';
printf("%c",ch);
}
fclose(fp);
return 0;
}
也请指导我如何将我的结果打印到输出文件。我将使用这个简单的程序来完成我的个人任务。这就是为什么我试图找到结果。请帮我。
您没有对尾随的白色 space 执行任何操作(出现在句子结束之后和换行符“\n”之前的那些)。您所做的只是错误地尝试(因为您只使用 printf
不会写入文件,所以无论如何都不会更改您的 txt 文件)试图用逗号替换 '\n'
,如果完成正确将导致以下输出仅包含一行且所有 space 都完好无损:
high blood sugar levels (space),type 2 diabetes definition (space),high blood sugar symptoms,glucose tolerance test,symptoms of high blood sugar,
为了摆脱尾随的 spaces,你必须在每次看到它们时计算 spaces 的数量,如果你得到一个单词而不是换行符,则重置计数.否则,如果您在计算 whitespace 时得到一个换行符,请使用该计数值打印那么多 backspaces '\b'
,然后打印一个逗号。请注意,除了添加逗号的位置外,您没有删除任何白色spaces,因此每行的换行符在尾随白色spaces 后仍位于同一位置。棘手的部分是没有尾随 space 的极端情况,即 '\n'
紧跟在句子的最后一个字符之后。在最后一个字符和换行符之间插入一个逗号,将需要在文件中移动数据以使该逗号成为 space。因此,最好的方法是使用临时文件,在删除尾随 spaces 并添加逗号后从输入文件逐行构建它。您可能想使用 fgets
和 fputs
这是我建议您可以做的事情
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char ch;
int length, index;
char *tgt = NULL;
// open sourse file
fp = fopen("keywords.txt","r");
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
// determine length of the source file
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
fseek(fp, 0L, SEEK_SET);
// allocate memory buffer
tgt = malloc( length );
if( tgt == NULL )
{
perror("Error while allocating memory.\n");
exit(EXIT_FAILURE);
}
// initialize memory buffer
memset( tgt, 0, length );
// read file to memory buffer
fread( tgt, 1, length, fp );
// close sourse file
fclose(fp);
// handle contents of the source file
printf("The contents of %s file are :\n", "keywords.txt");
index = 0;
while( index < length && tgt[index] != '[=10=]' )
{
if( tgt[index] == ' ' && index + 1 < length
&& tgt[index + 1] == '\n' )
{
printf( ",\n" );
index += 2;
}
else
{
printf( "%c", tgt[index] );
index += 1;
}
}
// release memory
free( tgt );
getchar();
return 0;
}
基本思路非常简单 - 您将源文件读入内存缓冲区,然后您可以自由地使用它做任何您想做的事。
您可以逐行而不是逐字符读取文件。 然后我们可以检查该行以检查 space 后跟换行符条件并将 space 替换为 , (您仍然需要换行符,以您的示例为例)。
我会这样做:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
FILE *in ,*out;
ssize_t linelength;
size_t alloc_size;
char *line = NULL;
if (NULL == (in = fopen("keywords.txt", "r"))){
perror("failed to open input file\n");
return 1;
}
if (NULL == (out = fopen("out.txt", "w"))){
perror("failed to open outputfile\n");
return 1;
}
while (-1 != (linelength= getline(&line, &alloc_size, in))){
/* line contains the whole line ('[=10=] terminated), including final '\n'
the length( as a string) is linelength */
if (linelength < 2) continue; /* too short line */
if (line[linelength -1] == '\n' && line[linelength-2] == ' '){
line[linelength-2] = ',';
}
fprintf(out, "%s",line);
}
fclose(in);
fclose(out);
free(line);
return 0;
}