我对一些数据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 and store那里的数据只是给出了地址
/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);
}
答案 0 :(得分:2)
你有main()
的参数错误。适当的原型是:
int main(int argc, char *argv[]);
其中argc
是给定参数的数量,argv
是包含每个参数的向量。第一个参数(在argv[0]
中)通常是程序的名称。
答案 1 :(得分:1)
未测试。玩得开心。
#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;
}
答案 2 :(得分:0)
void main(char *dir)
就是问题所在。
Main需要2个参数int/void main(int argc, char *argv[])
所以/a.out你好“看着我”会解析为