【问题标题】:Opening a file and printing to a file in c [closed]在c中打开文件并打印到文件[关闭]
【发布时间】:2017-05-02 13:44:06
【问题描述】:

我正在尝试创建一个文件并通过命令行打印一系列整数。基本上我想要的是argv[1] 是第一个打印的数字,argv[2] 是最后一个打印的数字,以及介于两者之间的系列。 argv[3] 将是文件的名称。我的代码如下所示,如果有人能指出任何错误,那就太好了。假设这个例子接收到的参数总是四个。第一次使用文件功能,所以对于任何非常简单的错误,我深表歉意。接收到非法数组,指针错误。

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int array[100];
    int first, last, c;
    first=atoi(argv[1]);
    last=atoi(argv[2]);
    c=last-first;
    for (int a=0; a<c; a++) {
        array[a]=first+a;
    }
    FILE *fp;

    fp = fopen(argv[3], "w");

    if (fp == NULL) {
       for (int i=0; i<last; i++) {
       fprintf(fp, "%d\n", array[i]);
    }
    }
    fclose(fp);
    return 0;
}

我的输入如下所示。

./number.c 20 25 5.txt

我输入 cat "file_name.txt" 时的预期输出如下所示。

cat number.c
20
21
22
23
24
25

但我收到的输出是

ASAN:DEADLY SIGNAL

runtime error-illegal array, pointer or other operation

编辑:问题已解决。

【问题讨论】:

  • 哇,对不起。甚至没有意识到有这样的网站。下次我一定会提到哪里出了问题。
  • 提示:当有 I/O 的代码出现问题时,发布使用的输入、看到的输出和预期的输出。
  • @Toby 这个问题与代码审查无关,将在那里结束。阅读散文的最后一句话,以及代码审查帮助中心。
  • @Toby 哦,哎呀,是的。我没有看时间戳。不过,我的评论(除了指责您没有阅读问题的那一点)目前是正确的;您可以删除您的以防止混淆吗?

标签: c arrays file


【解决方案1】:
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]) {
    int first, last;
    FILE *fp;

    if (argc < 3) { // Check for correct number of args
        // print an error message?
        return -1; 
    }

    first = atoi(argv[1]);
    last = atoi(argv[2]);    
    fp = fopen(argv[3], "w");

    if (fp != NULL) {
       for (int i = 0; i <= (first - last); i++) {
            fprintf(fp, "%d\n", i + first);           
        }
        fclose(fp);
    } else {
         //print an error message?
    }
    return 0;
}
  • 已删除数组,因为不需要它。
  • 将循环条件从&lt; 更改为&lt;=,因为:

    我希望 argv[1] 成为打印的第一个数字,而 argv[2] 是最后一个打印的数字以及介于两者之间的系列

  • 将 fclose() 移到 if 内,因为:https://stackoverflow.com/a/32674233/2394967

您现在可以添加的是错误处理(检查文件是否成功打开并检查用户输入),但这里可能不需要。

【讨论】:

  • 你应该在使用argv之前检查argc.....至少......
【解决方案2】:
 if (fp == NULL) 

应该改为

 if (fp != NULL) 

【讨论】:

  • 这是唯一的错误吗?
  • 此外,您应该添加一些检查,例如 - 检查用户是否真的在命令行上使用 3 个参数,如果最后一个 > 第一个,如果最后一个 - 第一个小于 100
  • 这听起来真的很愚蠢,但是说我在输入命令之前不知道最后一个和第一个之间有多少个数字,无论如何要让它如此 fprintf,不打印任何未初始化的值。
【解决方案3】:

您在 fclose 之前缺少一个 '}' 和一个 ';'在 fopen 之后只是为了让东西编译

您的写入迭代范围也是错误的。你应该改变

for (int i=0; i<last; i++)

for (int i=0; i<c; i++) {

【讨论】:

  • 抱歉,输入 ; 是一个简单的错误。但感谢您的更正。除此之外还可以吗?
【解决方案4】:

您应该在开始读取或写入文件之前检查您的打开是否成功。它将避免很多次试图了解什么不起作用。我并不是说它会导致您的代码出现问题。但可能。

试试这样的:

file = fopen(path, mode);
if (file == NULL)
  fprintf(stderr, "Could not open file corresponding to this path : %s\n", path);
else {
  // code
}

另外,我会避免在不使用数组大小​​作为循环停止条件的情况下遍历数组。您可能会遇到分段错误。

尝试类似的方法可能是:

for (int i = 0; i < array.size()  1; i++) {
   fprintf(fp, "%d\n", array[i]);
}

在 C 中:

for (int i = 0; i < sizeof(array)  1; i++) {
   fprintf(fp, "%d\n", array[i]);
}

更多关于如何在 C here 中迭代数组的细节。

【讨论】:

  • array.size() 是否在纯 c 中工作,因为我所有的搜索都指向 c++?
  • 在 c++ 中不起作用抱歉,已编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 2015-08-12
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 2021-03-01
  • 1970-01-01
相关资源
最近更新 更多