忽略一条线 [英] ignore a line

查看:75
本文介绍了忽略一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

int file_copy(char * oldname,char * newname);


int main()

{

char source [80],destination [80];


printf(" \ nEnter source file:") ;

得到(来源);

printf(" \ nEnter目的地文件:");

得到(目的地);


if(file_copy(source,destination)== 0)

puts(复制操作成功);

else

fprintf(stderr,复制操作期间出错);

system(PAUSE);

return(0);

}


int file_copy(char * oldname,char * newname)

{

FILE * fold,* fnew;

int c;

char tempString [150];


if((fold = fopen( oldname," r"))== NULL)

{

printf(源文件未打开\ n);

返回-1;

}

if((fnew = fope n(newname," w"))== NULL)

{

printf("目标文件未打开\ nn);

fclose(fold);

返回-1;

}


而(1)

{

/ *我想忽略一行* /

fgets(tempString,130,折叠);


c = fgetc(fold);


if(!feof(fold))

fputc(c,fnew);

其他

休息;

}

fclose(fnew);

fclose(fold);


返回0;

}


在上面的程序中,我想打开一个文件并复制到一个新文件。

新文件与旧文件相同,但第一行被忽略

来自旧文件。

我该怎么办?那个?


上面的程序可以很好地将旧文件复制到新文件中如果我

删除这一行(我想忽略第一行但是失败了):


/ *我想忽略一行* /

fgets(tempS) tring,130,fold);


我该怎么做?

thx !!

解决方案
" FrancisC" < FR ********** @ hong-kong.crosswinds.net>写道:

#include< stdio.h>

int file_copy(char * oldname,char * newname);

int main()
{char source [80],destination [80];


幻数80.文件名可能超过79 + 1个字符
printf(" \ nEnter source file:");
gets(source );


警告:可能的缓冲区溢出。

永远不会使用获取,如果用户输入
$ b,它会让你陷入严重的麻烦$ ba字符串大于你提供的缓冲区。

请阅读clc-faq http://www.eskimo.com/~scs/C-faq/top.html

第12.23节关于此问题。

printf(" \\\
Enter目标文件:");
获取(目的地);


见上文。

if(file_copy(source,destination)== 0)
puts(复制操作成功);
其他
fprintf(stderr,复制操作期间出错);
系统(暂停);
返回(0);
}
int file_copy(char * oldname,char * newname)
{
FILE * fold,* fnew;
int c;
char tempString [150];


幻数150.您尝试读入tempString的输入线

可能会超过149 + 1的长度。

if((fold = fopen(oldname," r"))== NULL)
{/> printf(源文件未打开\ n);
返回-1;
}
if((fnew = fopen(newname," w"))== NULL)
{/ / printf("目标文件未打开) \\ n");
fclose(fold);
返回-1;
}

而(1)


提供一个具有适当的

条件的循环语句是可以挽救的(除了
必须运行的循环除外),这是一个很好的编码实践。永远,但那是另一回事。

{
/ *我想忽略一行* /
fgets(tempString,130,fold);


为什么魔法数字130在这里,而不是上面的150?

如果您正在阅读的行超过130个字符的限制怎么办?

为什么使用fgets()(带有可能不合适的大小缓冲区)当你需要b / b
按字符方式执行复制时?


现在为RealProblem [tm]:

你在/每个/单个字符之前跳过一行输入

你复制,这绝对不是你想要的。
< br => c = fgetc(fold);

if(!feof(fold))
fputc(c,fnew);
其他
break;
}


如何用以下代码替换上面的循环:


/ *忽略第一行:* /

while((c = fgetc(fold))!= EOF)

if(c ==''\ n'')

break;


/ *复制剩余内容:* /

while((c = fgetc(fold))!= EOF)

fputc(c, fnew);


/ *我强烈建议使用ferror函数检查两个
流的错误指示。 * /

fclose(fnew);
fclose(fold);

返回0;
}

上面的程序,我想打开一个文件并复制到一个新文件。
新文件与旧文件相同,只是从旧文件中忽略第一行。
如何我能这样做吗?


见上文。

如果我删除此行(我想要的话),上述程序只能将旧文件复制到新文件中。忽略第一行却失败了):

/ *我想忽略一行* /
fgets(tempString,130,fold);

我该怎么办?去做?


见上文。

thx !!




欢迎你。


问候

-

Irrwahn

(ir ******* @ freenet。 de)




" Irrwahn Grausewitz" < IR ******* @ freenet.de> ????

新闻:16 ******************************** @ 4ax.com ??? ...

" FrancisC" < FR ********** @ hong-kong.crosswinds.net>写道:

#include< stdio.h>

int file_copy(char * oldname,char * newname);

int main()
{char source [80],destination [80];
幻数80.文件名可能超过79 + 1个字符


printf(" \ nEnter source file:");
获取(来源);



警告:可能的缓冲区溢出。
永远不会使用获取,如果用户输入的字符串大于您提供的缓冲区,它会让您陷入严重的麻烦。
请阅读clc-faq http://www.eskimo.com/~scs/C-faq/top.html ,关于这个问题的第12.23节。

printf(" \ nEnter目标文件:");
获取(目的地);



见上文。


if(file_c opy(source,destination)== 0)
puts(复制操作成功);
其他
fprintf(stderr,复制操作期间出错);
system(PAUSE);
return(0);
}
int file_copy(char * oldname,char * newname)
{
FILE * fold,* fnew;
int c;
char tempString [150];



幻数150.您尝试读入tempString的输入行
可能会超过149 + 1的长度。


if((fold = fopen(oldname," r"))== NULL)
{
printf(源文件未打开\ n);
返回-1;
}
if((fnew = fopen(newname,") ; w))== NULL)
{/> printf(目标文件未打开\ n);
fclose(fold);
返回-1;
}

while(1)


提供一个具有适当条件的循环语句是一个很好的编码实践,可以挽救(除了必须运行的循环)永远,但那是另一回事。

{
/ *我想忽略一行* /
fgets(tempString,130,fold );



为什么魔法数字130在这里,而不是上面的150?
如果您正在阅读的行超过130个字符的限制怎么办?
为什么要使用当你以字符方式执行复制时,fgets()(可能是不合适的缓冲区)?

现在对于RealProblem [tm]:
你正在跳过一行输入之前/每个/单个字符
你复制,这绝对不是你想要的。


c = fgetc(fold);

if(!feof(fold))
fputc(c,fnew);
其他
休息;
}



如何用以下代码替换上面的循环:

/ *忽略第一行:* /
while((c = fgetc(fold) )!= EOF)
如果(c ==''\ n'')
打破;

/ *复制剩余内容:* /
而( (c = fgetc(fold))!= EOF)
fputc(c,fnew);

/ *我强烈建议用pyror检查两个
流的错误指标功能在这里。 * /




如果我想复制第二行的第70个字符,并将其存储为

整数(例如,6 ;为了以后写入一个新文件,那么如何插入

代码?

atoi()函数似乎只适用于字符串到整数但不是单个

字符到整数


thx!

fclose(fnew);
fclose(fold );

返回0;
}

在上面的程序中,我想打开一个文件并复制到一个新文件。
新的文件与旧文件相同,但旧文件中忽略了第一行。
我该怎么办?



见上文。 />

如果我删除这一行(我想忽略第一行但失败了),上面的程序只能将旧文件复制到新文件:

/ *我想忽略一行* /
fgets(tempString,130,fold);

我该怎么做?



见上文。

thx !!


欢迎你。

问候
-
Irrwahn
(ir ******* @ freenet.de)



" FrancisC" < FR ********** @ hong-kong.crosswinds.net>写道:

" Irrwahn Grausewitz" < IR ******* @ freenet.de> ????
新闻:16 ******************************** @ 4ax.com ??? ..
< SNIP>


/ *将此行放在程序的顶部:* /


#include < ctype.h>

/ *将这些行放在函数的顶部:* /


int line = 1;

int pos = 1;

int val70 = 0;


< SNIP>

< blockquote class =post_quotes>
/ *忽略第一行:* /
while((c = fgetc(fold))!= EOF)
if(c ==''\ n'')
{

++ line;打破;
}

/ *复制剩余内容:* /
while((c = fgetc(fold))!= EOF)
{

/ *保存数字在L2:C70在val70:* /

if(line == 2&& pos == 70&& isdigit(c))

val70 = c - ''0'';

if(c ==''\ n'')

{

++ line_count;

pos = 1;

}

++ pos; fputc(c,fnew);
}
/ *我强烈建议在这里使用ferror功能检查两个
流的错误指示。 * /



如果我想复制第二行的第70个字符,并将其存储为
整数(例如,6到6)以便以后写入一个新文件,然后如何插入代码?




见上文,请注意以下内容:


1.我添加的代码假设您仍然希望将第二行的内容复制到新文件中。


2.代码没有检查第二个

行上的第七十个字符是否存在。


3.我没有测试代码,它可能不工作。


4.我使用了不好的编码风格,因为我使用魔术数字并继续携带

并增加不再需要的计数器。 br />

所以,随意改进它。 :)

atoi()函数似乎只适用于字符串到整数而不是单个
字符到整数




嗯,你可以自由地包裹一个字符串中的单个字符并将这个传递给atoi(),但我认为在这种情况下这有点矫枉过正。


< SNIP>


问候

-

Irrwahn

(ir ******* @ freenet .de)


#include <stdio.h>
int file_copy( char *oldname, char *newname );

int main()
{
char source[80], destination[80];

printf("\nEnter source file: ");
gets(source);
printf("\nEnter destination file: ");
gets(destination);

if ( file_copy(source, destination )==0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
system("PAUSE");
return(0);
}

int file_copy( char *oldname, char *newname)
{
FILE *fold, *fnew;
int c;
char tempString[150];

if ( ( fold = fopen( oldname, "r")) == NULL)
{
printf("Source file is not open\n");
return -1;
}
if ((fnew = fopen(newname, "w")) == NULL)
{
printf("Destination file is not open\n");
fclose(fold);
return -1;
}

while(1)
{
/* I want to ignore a line */
fgets(tempString, 130, fold);

c=fgetc(fold);

if(!feof(fold))
fputc(c, fnew);
else
break;
}
fclose(fnew);
fclose(fold);

return 0;
}

In the above program, I want to open a file and copy to a new file.
The new file is the same as the old file except the first line is ignored
from the old file.
How can I do that?

The above program works well of just copying old file to new file if I
delete this line(which I want to ignore the first line but failed):

/* I want to ignore a line */
fgets(tempString, 130, fold);

how can I do that?
thx!!

解决方案

"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:

#include <stdio.h>
int file_copy( char *oldname, char *newname );

int main()
{
char source[80], destination[80];
Magic number 80. A filename may well exceed 79+1 characters
printf("\nEnter source file: ");
gets(source);
Warning: possible buffer overrun.
Never ever use gets, it''ll get you into severe trouble if a user enters
a string larger than the buffer you provide.
Please read the c.l.c-faq http://www.eskimo.com/~scs/C-faq/top.html ,
section 12.23 about this issue.
printf("\nEnter destination file: ");
gets(destination);
See above.

if ( file_copy(source, destination )==0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
system("PAUSE");
return(0);
}

int file_copy( char *oldname, char *newname)
{
FILE *fold, *fnew;
int c;
char tempString[150];
Magic number 150. The line of input you attempt to read into tempString
may well exceed a length of 149+1.

if ( ( fold = fopen( oldname, "r")) == NULL)
{
printf("Source file is not open\n");
return -1;
}
if ((fnew = fopen(newname, "w")) == NULL)
{
printf("Destination file is not open\n");
fclose(fold);
return -1;
}

while(1)
It''s good coding pratice to provide a loop statement with a proper
condition that makes it possible to bail out (except for loops that
have to run "forever", but that''s a different matter).
{
/* I want to ignore a line */
fgets(tempString, 130, fold);
Why magic number 130 here, instead of 150 as above?
What if the line you are reading exceeds the limit of 130 chars?
Why use fgets() (with a possibly inappropriate sized buffer) when you
perform the copying itself character-wise?

Now for the RealProblem[tm]:
You are skipping one line of input before /every/ single character
you copy, this is definitely not what you want.

c=fgetc(fold);

if(!feof(fold))
fputc(c, fnew);
else
break;
}
What about replacing the above loop with:

/* ignore first line: */
while ( ( c = fgetc( fold ) ) != EOF )
if ( c == ''\n'' )
break;

/* copy remaining content: */
while ( ( c = fgetc( fold ) ) != EOF )
fputc( c, fnew );

/* I strongly suggest checking the error indicators for both
streams with the ferror function here. */
fclose(fnew);
fclose(fold);

return 0;
}

In the above program, I want to open a file and copy to a new file.
The new file is the same as the old file except the first line is ignored
from the old file.
How can I do that?
See above.
The above program works well of just copying old file to new file if I
delete this line(which I want to ignore the first line but failed):

/* I want to ignore a line */
fgets(tempString, 130, fold);

how can I do that?
See above.
thx!!



You''re welcome.

Regards
--
Irrwahn
(ir*******@freenet.de)



"Irrwahn Grausewitz" <ir*******@freenet.de> ???
news:16********************************@4ax.com ???...

"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:

#include <stdio.h>
int file_copy( char *oldname, char *newname );

int main()
{
char source[80], destination[80];
Magic number 80. A filename may well exceed 79+1 characters


printf("\nEnter source file: ");
gets(source);



Warning: possible buffer overrun.
Never ever use gets, it''ll get you into severe trouble if a user enters
a string larger than the buffer you provide.
Please read the c.l.c-faq http://www.eskimo.com/~scs/C-faq/top.html ,
section 12.23 about this issue.

printf("\nEnter destination file: ");
gets(destination);



See above.


if ( file_copy(source, destination )==0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
system("PAUSE");
return(0);
}

int file_copy( char *oldname, char *newname)
{
FILE *fold, *fnew;
int c;
char tempString[150];



Magic number 150. The line of input you attempt to read into tempString
may well exceed a length of 149+1.


if ( ( fold = fopen( oldname, "r")) == NULL)
{
printf("Source file is not open\n");
return -1;
}
if ((fnew = fopen(newname, "w")) == NULL)
{
printf("Destination file is not open\n");
fclose(fold);
return -1;
}

while(1)



It''s good coding pratice to provide a loop statement with a proper
condition that makes it possible to bail out (except for loops that
have to run "forever", but that''s a different matter).

{
/* I want to ignore a line */
fgets(tempString, 130, fold);



Why magic number 130 here, instead of 150 as above?
What if the line you are reading exceeds the limit of 130 chars?
Why use fgets() (with a possibly inappropriate sized buffer) when you
perform the copying itself character-wise?

Now for the RealProblem[tm]:
You are skipping one line of input before /every/ single character
you copy, this is definitely not what you want.


c=fgetc(fold);

if(!feof(fold))
fputc(c, fnew);
else
break;
}



What about replacing the above loop with:

/* ignore first line: */
while ( ( c = fgetc( fold ) ) != EOF )
if ( c == ''\n'' )
break;

/* copy remaining content: */
while ( ( c = fgetc( fold ) ) != EOF )
fputc( c, fnew );

/* I strongly suggest checking the error indicators for both
streams with the ferror function here. */



If I want to copy the 70th character of the second line, and store it as
integer(eg, "6" to 6) for later writing to a new file, then how can I insert
the code?
the atoi() function seems only works with string to integer but not single
character to integer

thx!

fclose(fnew);
fclose(fold);

return 0;
}

In the above program, I want to open a file and copy to a new file.
The new file is the same as the old file except the first line is ignored
from the old file.
How can I do that?



See above.

The above program works well of just copying old file to new file if I
delete this line(which I want to ignore the first line but failed):

/* I want to ignore a line */
fgets(tempString, 130, fold);

how can I do that?



See above.

thx!!



You''re welcome.

Regards
--
Irrwahn
(ir*******@freenet.de)



"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:

"Irrwahn Grausewitz" <ir*******@freenet.de> ???
news:16********************************@4ax.com ???... <SNIP>

/* Put this line at the top of your program: */

#include <ctype.h>
/* Put these lines at the top of your function: */

int line = 1;
int pos = 1;
int val70 = 0;

<SNIP>


/* ignore first line: */
while ( ( c = fgetc( fold ) ) != EOF )
if ( c == ''\n'' ) {
++line; break; }

/* copy remaining content: */
while ( ( c = fgetc( fold ) ) != EOF ) {
/* save digit at L2:C70 in val70: */
if ( line == 2 && pos == 70 && isdigit(c) )
val70 = c - ''0'';
if ( c == ''\n'' )
{
++line_count;
pos = 1;
}
++pos; fputc( c, fnew ); }
/* I strongly suggest checking the error indicators for both
streams with the ferror function here. */



If I want to copy the 70th character of the second line, and store it as
integer(eg, "6" to 6) for later writing to a new file, then how can I insert
the code?



See above, and please note the following:

1. The code I added assumes that you still want to copy the contents of
the second line to your new file.

2. The code doesn''t check if the seventieth character on the second
line even exists.

3. I didn''t test the code, it may not work.

4. I used bad coding style in that I use magic numbers and keep carrying
around and incrementing counters that are no longer needed.

So, feel free to improve it. :)
the atoi() function seems only works with string to integer but not single
character to integer



Well, you are free to "wrap" a single character in a string and pass
this to atoi(), but I consider this to be overkill in this case.

<SNIP>

Regards
--
Irrwahn
(ir*******@freenet.de)


这篇关于忽略一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆