使用 C 将 char * 传递到 fopen

发布于 2024-08-25 07:50:03 字数 1347 浏览 5 评论 0原文

我正在编写一个程序,将数据从文件传递到数组中,但我在使用 fopen () 时遇到问题。当我将文件路径硬编码到参数中时(例如 fopen ("data/1.dat", "r");),它似乎工作正常,但是当我将它作为指针传递时,它会返回无效的。

请注意,如果从命令行输入,第 142 行将打印“data/1.dat”,因此 parse_args () 似乎正在工作。

132 int
133 main(int argc, char **argv)
134 {
135   FILE *in_file;
136   int *nextItem = (int *) malloc (sizeof (int));
137   set_t *dictionary;
138
139   /* Parse Arguments */
140   clo_t *iopts = parse_args(argc, argv);
141
142   printf ("INPUT FILE: %s.\n", iopts->input_file); /* This prints correct path */
143   /* Initialise dictionary */
144     dictionary = set_create (SET_INITAL_SIZE);
145
146   /* Use fscanf to read all data values into new set_t */
147   if ((in_file = fopen (iopts->input_file, "r")) == NULL)
148   {
149     printf ("File not found...\n");
150     return 0;
151   }

谢谢! Rhys

更多:如果我在运行 set_create() (ln 144) 后尝试打印字符串,则该字符串不会打印。 (但是函数中根本没有任何对字符串的引用......)

 47 set_t *
 48 set_create(int size)
 49 {
 50   set_t *set;
 51
 52   /* set set_t members */
 53   set->items = 0;
 54   set->n_max = size;
 55   set->lock = FALSE;
 56
 57   /* allocate memory for dictionary input */
 58   set->data = (int *) malloc (size * sizeof (int));
 59
 60   return set;
 61 }

如果我在 fopen() 之后调用这个函数,它确实可以工作。 不过,我看不出这对文件名有何影响...

再次感谢。

I'm writing a program that passes data from a file into an array, but I'm having trouble with fopen (). It seems to work fine when I hardcode the file path into the parameters (eg fopen ("data/1.dat", "r");) but when I pass it as a pointer, it returns NULL.

Note that line 142 will print "data/1.dat" if entered from command line so parse_args () appears to be working.

132 int
133 main(int argc, char **argv)
134 {
135   FILE *in_file;
136   int *nextItem = (int *) malloc (sizeof (int));
137   set_t *dictionary;
138
139   /* Parse Arguments */
140   clo_t *iopts = parse_args(argc, argv);
141
142   printf ("INPUT FILE: %s.\n", iopts->input_file); /* This prints correct path */
143   /* Initialise dictionary */
144     dictionary = set_create (SET_INITAL_SIZE);
145
146   /* Use fscanf to read all data values into new set_t */
147   if ((in_file = fopen (iopts->input_file, "r")) == NULL)
148   {
149     printf ("File not found...\n");
150     return 0;
151   }

Thanks!
Rhys

MORE: If I try to print the string after I run set_create() (ln 144), the string doesn't print. (But there isn't any reference to the string in the function at all...)

 47 set_t *
 48 set_create(int size)
 49 {
 50   set_t *set;
 51
 52   /* set set_t members */
 53   set->items = 0;
 54   set->n_max = size;
 55   set->lock = FALSE;
 56
 57   /* allocate memory for dictionary input */
 58   set->data = (int *) malloc (size * sizeof (int));
 59
 60   return set;
 61 }

It does work if I call this function after fopen ().
I can't see how this is affecting the filename though...

Thanks again.

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

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

发布评论

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

评论(3

¢好甜 2024-09-01 07:50:03

您的新代码显示您正在写入无效内存。 set 是一个指针,但您从未初始化它。您覆盖了一些随机内存,从而破坏了传递给 fopen() 的字符串指针。

Your new code shows that you are writing to invalid memory. set is a pointer but you never initialize it. You're overwriting some random memory and thereby destroying the pointer to the string that you're passing to fopen().

握住你手 2024-09-01 07:50:03

您确定 parse_args 工作正常吗?例如,如果它返回指向局部变量(或包含此类指针的结构)的指针,则像 iopts->input_file 这样的值很容易被后续函数调用破坏。

Are you sure parse_args works correctly? If it, for example, returns a pointer to a local variable (or a struct that contains such pointers), the values like iopts->input_file would easily be destroyed by subsequent function calls.

是伱的 2024-09-01 07:50:03

第二部分是你的问题。设置未初始化。

澄清一下:您正在修改您无意的内容,导致 fopen() 失败。

That second part is your problem. set is not initialized.

To clarify: you're modifying stuff that you don't mean to, causing the fopen() to fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文