且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

替换文本文件中的单词

更新时间:2023-02-23 14:09:04

William Payne写道:
William Payne wrote:
你好,我需要编写一个程序打开一个文本文件并扫描整个文件中的特定行,当找到该行时,该行上的特定单词将被替换。新单词作为程序的参数。我写了一个小的测试程序,因为
strcmp()无法找到匹配的行而无法正常工作。


那是因为fgets没有剥离换行符(假设它在

可用空间中找到一个)。

while(fgets(line,sizeof(line),in_file))
{/ / printf("行读:%s \ n",行);

if(strcmp(line," set imap_user = root")== 0)
Hello, I need to write a program that opens a text file and scans the
entire file for a specific line and when that line is found, a particular
word on that line is to be replaced. The new word is given as an argument
to the program. I wrote a small test program that doesn''t work because
strcmp() fails to find a matching line.
That''s because fgets doesn''t strip the newline (assuming it finds one in the
available space).
while(fgets(line, sizeof(line), in_file))
{
printf("Line read: %s\n", line);

if(strcmp(line, "set imap_user=root") == 0)




我建议你用以下代码替换这行:


if(strcmp(line," set imap_user = root\\\
")== 0)


-

Richard Heathfield: bi****@eton.powernet.co.uk

" Usenet是一个陌生的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等: http://users.powernet.co.uk/eton



I suggest you replace this line with:

if(strcmp(line, "set imap_user=root\n") == 0)

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton





在11/5/2003 2:58 PM,William Payne写道:


On 11/5/2003 2:58 PM, William Payne wrote:
你好,我需要编写一个打开文本文件的程序并扫描整个
文件中的特定行,当找到该行时,该行上的特定单词将被替换。新单词作为
程序的参数给出。我写了一个小的测试程序,因为strcmp()
无法找到匹配的行而无法正常工作。这是代码:


< snip> while(fgets(line,sizeof(line),in_file))
{
printf("行读:%s \ n",行);

if( strcmp(line," set imap_user = root")== 0)
Hello, I need to write a program that opens a text file and scans the entire
file for a specific line and when that line is found, a particular word on
that line is to be replaced. The new word is given as an argument to the
program. I wrote a small test program that doesn''t work because strcmp()
fails to find a matching line. Here''s the code:
<snip> while(fgets(line, sizeof(line), in_file))
{
printf("Line read: %s\n", line);

if(strcmp(line, "set imap_user=root") == 0)



fgets()将换行符char复制到字符串的末尾,所以line ;结束于

换行符,而set imap_user = root没有。


Ed。



fgets() copies the newline char to the end of the string, so "line" ends in a
newline whereas "set imap_user=root" doesn''t.

Ed.




" William Payne" ; &LT; MI ************** @ student.liu.se&GT;在留言中写道

news:bo ********** @ news.island.liu.se ...

"William Payne" <mi**************@student.liu.se> wrote in message
news:bo**********@news.island.liu.se...
你好,我需要写一个程序打开一个文本文件并扫描
整个文件中的特定行,当找到该行时,该行上的特定单词将被替换。新单词作为
程序的参数给出。我写了一个小的测试程序,因为strcmp()
无法找到匹配的行而无法正常工作。这是代码:

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

int main(int argc,char ** argv)
{... FILE * in_file = NULL;
FILE * out_file = NULL;
char * temporary_file_name = NULL;
char line [128];

if(argc< 2)
{f / / fprintf(stderr,你必须提供一个单词。\ n);

返回1;
}

in_file = fopen(" test.txt"," r");

if(in_file == NULL)
{
fprintf(stderr,错误打开test.txt \ n);

返回1;
}

temporary_file_name = tmpnam (NULL);

out_file = fopen(temporary_file_name," w");

while(fgets(line,sizeof(line),in_file))
{
printf(行读:%s \ n,行);

if(strcmp(line," set imap_user = root")== 0)


改变th以上行:


if(strcmp(line," set imap_user = root\\\
")== 0)

{
printf(" Replacing.\\\
);;
strcpy(line," set imap_user =");
strcat(line,argv [1]);


strcat(line," \ n");


/ *你还应提供针对''strcat()'的保护'

溢出数组''行''* /

}

fputs(line,out_file);
}

fclose(in_file);
fflush(out_file);
fclose(out_file);

删除(" test.txt");
rename(temporary_file_name," test.txt");

返回0;
}

这里是原始文件test.txt:
set spoolfile = imap://sysinst.ida.liu.se/INBOX
set folder = imap://sysinst.ida.liu.se/INBOX
set imap_user = root
set ssl_starttls = no
set sendmail =" / usr / sbin / sendmail -oem -oi"

为什么不起作用?
Hello, I need to write a program that opens a text file and scans the entire file for a specific line and when that line is found, a particular word on
that line is to be replaced. The new word is given as an argument to the
program. I wrote a small test program that doesn''t work because strcmp()
fails to find a matching line. Here''s the code:

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

int main(int argc, char** argv)
{
FILE* in_file = NULL;
FILE* out_file = NULL;
char* temporary_file_name = NULL;
char line[128];

if(argc < 2)
{
fprintf(stderr, "You must supply a word.\n");

return 1;
}

in_file = fopen("test.txt", "r");

if(in_file == NULL)
{
fprintf(stderr, "Error opening test.txt\n");

return 1;
}

temporary_file_name = tmpnam(NULL);

out_file = fopen(temporary_file_name, "w");

while(fgets(line, sizeof(line), in_file))
{
printf("Line read: %s\n", line);

if(strcmp(line, "set imap_user=root") == 0)
Change the above line to:

if(strcmp(line, "set imap_user=root\n") == 0)
{
printf("Replacing.\n");
strcpy(line, "set imap_user=");
strcat(line, argv[1]);
strcat(line, "\n");

/* you should also provide protection against ''strcat()''
overflowing the array ''line'' */
}

fputs(line, out_file);
}

fclose(in_file);
fflush(out_file);
fclose(out_file);

remove("test.txt");
rename(temporary_file_name, "test.txt");

return 0;
}

And here''s the original file test.txt:
set spoolfile=imap://sysinst.ida.liu.se/INBOX
set folder=imap://sysinst.ida.liu.se/INBOX
set imap_user=root
set ssl_starttls=no
set sendmail="/usr/sbin/sendmail -oem -oi"

Why doesn''t it work?




''fgets()''存储它读取的换行符(如果有的话)。


我的上述更改并非万无一失,即如果该行阅读

有'尾随空格换行前的's'',或''fgets()''

没有看到换行符,那就不行了。


-Mike



''fgets()'' stores the newline (if any) that it reads.

My above changes aren''t foolproof, i.e. if the line read
has ''trailing spaces'' before the newline, or if ''fgets()''
doesn''t read a newline, then it won''t work.

-Mike