- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
程序:
#include<stdio.h>
#include<string.h>
char *f_gets(char *s, int n, FILE *iop)
{
int c=0;
char *cs;
cs = s;
while (--n > 0 && (c = getc(iop)) != EOF)
{
if ((*cs++ = c) == '\n')
break;
}
*cs = '\0';
return (c == EOF && cs == s) ? NULL : s;
}
main(int argc, char *argv[])
{
FILE *fp1,*fp2;
char s2[100],s1[100];
if (argc <= 2 )
printf("2 argument needed \n");
else
if((fp1=fopen(argv[1],"r"))== NULL && (fp2=fopen(argv[2],"r"))==NULL)
printf("cat: can't open The file\n");
else
{
while(1)
{
f_gets(s1,100,fp1); // 1st iteration
f_gets(s2,100,fp2); // 2nd iteration
if(!strcmp(s1,s2))
printf("%s %s",s1,s2);
}
fclose(fp1);
fclose(fp2);
}
}
输出:
$ ./a.out a b
Segmentation fault (core dumped)
$
在上面的程序中,当我们第二次调用 f_gets 时发生了段错误。即使我两次检查程序也很难找到问题所在。有没有人解释为什么它会产生问题。
最佳答案
您调用电话时您的第二个文件未打开。
问题是您从短路的路径调用 fopen
:
if((fp1=fopen(argv[1],"r"))== NULL && (fp2=fopen(argv[2],"r"))==NULL)
由于您的代码中存在错误,当fp1
正常打开时,fp2
将始终保持关闭状态。这是因为 (fp1=fopen(argv[1],"r"))== NULL
将评估为 0
,并确保 (fp2=fopen (argv[2],"r"))==NULL
永远不会被调用。
您可以通过将 &&
替换为 ||
来解决此问题,但更好的方法是一次打开一个文件。
关于c - getc 函数产生段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36496299/
根据“未定义行为”的现代解释,编译器有权假设不会发生导致未定义行为“不可避免”的事件链,并且可以消除仅适用于以下情况的代码:将执行未定义的行为;这可能会导致未定义行为的影响及时倒退,并使原本可以观察到
在阅读 getc 手册页时,我遇到了: may be implemented as a macro which evaluates stream more than once. 那么,getc 也可以
程序有什么问题? #include #include #include main( ) { char * buf="robot.c"; char c;int i=0; FILE*f
在下面的代码中,我尝试存储文件中的所有字符(包括换行符)。如果读取换行符,变量“i”应该递增,“j”重置为 0,但这种情况不会发生。通过从我的数组打印到控制台,我已经确认换行符实际上正在被读取和存储。
它只是一个程序,我试图读取文件中作为参数传递的单词的出现次数,该文件也作为下一个参数传递。 代码如下所示: #include extern void exit(int); void main(int
我正在用 C 开发一个链表。我正在从一个 txt 文件中获取数据。但是当我尝试运行该程序时,它给我一个 getc() 的段错误这是代码, #include #include struct node{
程序: #include #include char *f_gets(char *s, int n, FILE *iop) { int c=0; char *cs; cs =
我试图找到 getc 和 fgetc 之间的区别。当时我看到这样的说法: The difference between getc and fgetc is that getc can be imple
我正在使用 Perl 6 模块 Term::termios . #!/usr/bin/env perl6 use v6; use Term::termios; my $saved_termios :=
我有一个函数读取 unsigned long long 中的每一位并将值存储在数组中,现在我想将此函数转换为返回数字中的下一位并将数据处理留给调用者的函数. 理想情况下,它的工作方式与 getc()
我有一个函数读取 unsigned long long 中的每一位并将值存储在数组中,现在我想将此函数转换为返回数字中的下一位并将数据处理留给调用者的函数. 理想情况下,它的工作方式与 getc()
我有一个函数 getNum(),它从文件中获取一个数字并返回它。当我回到 getNum() 时,我丢失了指针,它再次从文件的开始处开始。我想知道如何获取 getc 所在的位置,然后返回到那个地方。我在
所以我开始实现霍夫曼树,为此,我尝试从标准输入或输入文件获取字符值。输入文件(只是一个字符串“cheese”)被添加到数组 freqcounts 中,其中添加的 freqcounts 索引是它读取的字
我遇到的问题是在这一行: int tok = getc(fp); getc 返回 -1。为什么?提前致谢。 #include #include #include "file_reader.h" /
printf("hello2"); int i = 0; int done = 0; while (!done) { char c; printf("hello3"); c =
我正在阅读 Jim Trevor 等人所著的Cyclone:C 的安全方言。一切都是为了编程语言类(class)。作者表示,如果调用 getc(null) 可能会导致段错误,因为 C 标准没有指定如何
当我尝试从名为“file1”的文件中读取输入时,我的程序正确显示文件中的字符数,但采用无法识别的字符格式。下面是代码 #include #include void db_sp(FILE*); in
// Program to remove the comments and the spaces from the given input file #include #include using
这是我的代码。 #include #include int main(int argc,char** argv) { char a; a=9; FILE * fp; f
我正在尝试用 C 编写一个简单的“猫”克隆。我正在运行 Windows 7 并使用 MinGW 编译器。但是,每当我运行该程序时,它都会返回文本文件,但每个字符都替换为“☺”字符。提前谢谢你。 #in
我是一名优秀的程序员,十分优秀!