将数据保存到文件中:文件的地址由用户指定

发布于 2024-11-05 05:09:54 字数 1092 浏览 0 评论 0原文

我对一些数据 y1、y2、..yn 进行了模拟并生成向量 w、mu。在每次模拟时,这些结果都会存储到一个文件中(通常 w 和 mu 是非常长的向量,有 10,000 个条目)

/home/carlos/Documents/Results/w.txt
/home/carlos/Documents/Results/mu.txt

但是如果我想用其他数据集运行我的算法,并且不想丢失以前的结果,我必须直接进入我的 C 代码并更改(或将 w.txt、mu.txt 移动到其他文件)

/home/carlos/Documents/Results/OtherData/w.txt
/home/carlos/Documents/Results/OtherData/mu.txt

我不想每次都进入我的 C 代码更改地址(或一次又一次移动 w.txt ,mu.txt),我想只需创建一个名为:OtherData 的新文件夹并将数据存储在那里,只需提供地址

/home/carlos/Documents/Results/OtherData/

作为代码的输入

我做了一个非常简化的示例,但它不起作用,有人可以帮我吗?

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

void main(char *dir){
  char dir_happy[100] = *dir, dir_sad[100]=*dir;
  FILE *ffile_happy, *ffile_sad;

  strcat(dir_happy, "/happy.txt");
  strcat(dir_sad, "/sad.txt");

  ffile_happy = fopen("dir_happy.txt", "w");
  ffile_sad = fopen("dir_sad.txt", "w");

  fprintf(ffile_happy, "Hello!, happy world\n");
  fprintf(ffile_sad, "Hello!, sad world\n");

  fclose(ffile_happy);
  fclose(ffile_sad);
}

I ran a simulation for some data y1, y2,..yn and generate vectors w, mu. At each simulation these results are stored into a file, let us say (normally w and mu are very long vectors 10,000 entries)

/home/carlos/Documents/Results/w.txt
/home/carlos/Documents/Results/mu.txt

But if I want to run my algorithm with other data set, and do not want to lose the previous results, I have to go directly into my C code and change (or move the w.txt, mu.txt to other file)

/home/carlos/Documents/Results/OtherData/w.txt
/home/carlos/Documents/Results/OtherData/mu.txt

I do not want to go every time into my C code to change the address(or move again and again w.txt, mu.txt), I would like to just create a new folder with a name: OtherData and store the data there just giving the address

/home/carlos/Documents/Results/OtherData/

as an input for the code

I did a very simplified example but it does not work, could somebody give me a hand?

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

void main(char *dir){
  char dir_happy[100] = *dir, dir_sad[100]=*dir;
  FILE *ffile_happy, *ffile_sad;

  strcat(dir_happy, "/happy.txt");
  strcat(dir_sad, "/sad.txt");

  ffile_happy = fopen("dir_happy.txt", "w");
  ffile_sad = fopen("dir_sad.txt", "w");

  fprintf(ffile_happy, "Hello!, happy world\n");
  fprintf(ffile_sad, "Hello!, sad world\n");

  fclose(ffile_happy);
  fclose(ffile_sad);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执妄 2024-11-12 05:09:54

main() 的参数错误。正确的原型是:

int main(int argc, char *argv[]);

其中 argc 是给定参数的数量,argv 是保存每个参数的向量。第一个参数(在 argv[0] 中)通常是程序的名称。

You have the arguments to main() wrong. The proper prototype is:

int main(int argc, char *argv[]);

Where argc is the number of arguments given, and argv is a vector holding each argument. The first argument (in argv[0]) is generally the program's name.

骄傲 2024-11-12 05:09:54

未经测试。玩得开心。

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

#define MAX_FILENAME_LENGTH 100
#define DEFAULT_DIR "."
#define HAPPY_NAME "/happy.txt"
#define SAD_NAME "/sad.txt"

int main(int argc, char **argv) {
    char name1[MAX_FILENAME_LENGTH+1], name2[MAX_FILENAME_LENGTH+1];
    size_t dirlen;
    char *dir = DEFAULT_DIR;
    FILE *ffile_happy, *ffile_sad;

    if (argc == 2) {
        dir = argv[1];
    }
    dirlen = strlen(dir);
    if (len + strlen(HAPPY_NAME) > MAX_FILENAME_LENGTH) {
        fprintf(stderr, "Directory name too long. Program aborted.\n");
        exit(EXIT_FAILURE);
    }
    if (len + strlen(SAD_NAME) > MAX_FILENAME_LENGTH) {
        fprintf(stderr, "Directory name too long. Program aborted.\n");
        exit(EXIT_FAILURE);
    }
    strcpy(name1, dir); strcat(name1, HAPPY_NAME);
    strcpy(name2, dir); strcat(name2, SAD_NAME);

    ffile_happy = fopen(name1, "w");
    if (ffile_happy == NULL) {
        fprintf(stderr, "Unable to open file \"%s\" for writing. Program aborted.\n", name1);
        exit(EXIT_FAILURE);
    }
    ffile_sad = fopen(name2, "w");
    if (ffile_sad == NULL) {
        fprintf(stderr, "Unable to open file \"%s\" for writing. Program aborted.\n", name2);
        fclose(ffile_happy);
        exit(EXIT_FAILURE);
    }

    /* use files */

    fclose(ffile_happy);
    fclose(ffile_sad);
    return 0;
}

Untested. Have fun.

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

#define MAX_FILENAME_LENGTH 100
#define DEFAULT_DIR "."
#define HAPPY_NAME "/happy.txt"
#define SAD_NAME "/sad.txt"

int main(int argc, char **argv) {
    char name1[MAX_FILENAME_LENGTH+1], name2[MAX_FILENAME_LENGTH+1];
    size_t dirlen;
    char *dir = DEFAULT_DIR;
    FILE *ffile_happy, *ffile_sad;

    if (argc == 2) {
        dir = argv[1];
    }
    dirlen = strlen(dir);
    if (len + strlen(HAPPY_NAME) > MAX_FILENAME_LENGTH) {
        fprintf(stderr, "Directory name too long. Program aborted.\n");
        exit(EXIT_FAILURE);
    }
    if (len + strlen(SAD_NAME) > MAX_FILENAME_LENGTH) {
        fprintf(stderr, "Directory name too long. Program aborted.\n");
        exit(EXIT_FAILURE);
    }
    strcpy(name1, dir); strcat(name1, HAPPY_NAME);
    strcpy(name2, dir); strcat(name2, SAD_NAME);

    ffile_happy = fopen(name1, "w");
    if (ffile_happy == NULL) {
        fprintf(stderr, "Unable to open file \"%s\" for writing. Program aborted.\n", name1);
        exit(EXIT_FAILURE);
    }
    ffile_sad = fopen(name2, "w");
    if (ffile_sad == NULL) {
        fprintf(stderr, "Unable to open file \"%s\" for writing. Program aborted.\n", name2);
        fclose(ffile_happy);
        exit(EXIT_FAILURE);
    }

    /* use files */

    fclose(ffile_happy);
    fclose(ffile_sad);
    return 0;
}
乞讨 2024-11-12 05:09:54

void main(char *dir) 是问题所在。

Main 需要 2 个参数 int/void main(int argc, char *argv[])

  • argc 是可执行文件的参数数量。
  • argv[0] 是可执行文件的文件名。
  • argv[1..n] 是传递的参数(通常以空格分隔,允许使用引号)

因此 /a.out Hello "Look at me" 将解析为

  • argv[0] => './a.out'
  • argv[1] => '你好'
  • argv[2] => '看着我'

void main(char *dir) is the problem.

Main takes 2 args int/void main(int argc, char *argv[])

  • argc is the number of arguments to the executable.
  • argv[0] is the filename of the executable.
  • argv[1..n] are the arguments passed (normally space separated, with quotes allowed)

So /a.out Hello "Look at me" would parse as

  • argv[0] => './a.out'
  • argv[1] => 'Hello'
  • argv[2] => 'Look at me'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文