将数据保存到文件中:文件的地址由用户指定
我对一些数据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
main()
的参数错误。正确的原型是:其中 argc 是给定参数的数量,argv 是保存每个参数的向量。第一个参数(在
argv[0]
中)通常是程序的名称。You have the arguments to
main()
wrong. The proper prototype is:Where
argc
is the number of arguments given, andargv
is a vector holding each argument. The first argument (inargv[0]
) is generally the program's name.未经测试。玩得开心。
Untested. Have fun.
void main(char *dir)
是问题所在。Main 需要 2 个参数
int/void main(int argc, char *argv[])
因此 /a.out Hello "Look at me" 将解析为
void main(char *dir)
is the problem.Main takes 2 args
int/void main(int argc, char *argv[])
So /a.out Hello "Look at me" would parse as