|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在linux上做C和指针的习题输入以下代码运行会报错 Segmentation fault (core dumped)
在Windows运行的好好的
上网查了解决办法,看完感觉一头雾水
- /*从文件一行一行读入字符串,若有[color=Red]连续[/color]两行及以上的字符串重复则输出*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 128
- int main()
- {
- char s[MAX], cp[MAX]; /*s用于从文件读入一行字符串,cp用于复制s并对比下一行的s是否一样*/
- int count = 0;
- FILE *p = NULL;
- p = fopen("test.txt","r");
- fgets( s, MAX, p );
- strcpy( cp, s );
- while( fgets( s, MAX, p ) ) /*从文件一行一行读入字符串*/
- {
- if( !strcmp( cp, s ) && count == 0 ) /*第一次重复行*/
- {
- printf("%s",s);
- count++;
- }
- else if( strcmp( cp, s ) ) /*两行不重复*/
- {
- strcpy( cp, s );
- count = 0;
- }
- else /*连续重复行*/
- {
- count++;
- }
- }
- return 0;
- }
复制代码
我用的test.txt内容:
This is the first line.
Another line.
And another.
And another.
And another.
And another.
Still more.
Almost done now --
Almost done now --
Another line.
Still more.
Finished! |
|