Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数调用使用字符串文字,但不使用字符串变量C_C_Arrays_String - Fatal编程技术网

函数调用使用字符串文字,但不使用字符串变量C

函数调用使用字符串文字,但不使用字符串变量C,c,arrays,string,C,Arrays,String,我正试图从服务器上为file1.txt中的不同id代码获取一些数据。如果我使用字符串literald.id=“ABC”调用fetchData(&d)效果很好,但如果我从文件中读取每个id后使用变量数组[idx]则不起作用。有人能告诉我我做错了什么(不知道fetchData()的内部结构)吗?我正在学C,请耐心点 file1.txt的内容如下所示: ABC DEF ... 我的代码如下: /* #include "myapi.h" */ //fetchData #include <

我正试图从服务器上为
file1.txt
中的不同
id
代码获取一些数据。如果我使用字符串literal
d.id=“ABC”
调用
fetchData(&d)
效果很好,但如果我从文件中读取每个
id
后使用变量
数组[idx]
则不起作用。有人能告诉我我做错了什么(不知道
fetchData()
的内部结构)吗?我正在学C,请耐心点

file1.txt的内容如下所示:

ABC
DEF
...
我的代码如下:

/*    #include "myapi.h" */ //fetchData

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

typedef struct param {
        char *id;
    } param_t;

int countlines(char filename[])
{
  // count the number of lines in the file called filename                                    
  FILE *fp = fopen(filename,"r"); 
  int ch=0;
  int lines=0;

  while(!feof(fp))
    {
      ch = fgetc(fp);
      if(ch == '\n')
    {
      lines++;
    }
    }

  fclose(fp);
  return lines;
}

int main()
{

  char filename[] = "file1.txt";
  int N = countlines(filename);

  // open the file for reading
  FILE *file = fopen(filename, "r");

  // make sure the file opened properly
  if(NULL == file)
    {
      fprintf(stderr, "Cannot open file: %s\n", filename);
      return 1;
    }

  size_t buffer_size = 10;

  /* create an array of char pointers */
  char **array = malloc(N * buffer_size * sizeof(char*));

  /* allocate space for each string: */
  int line_number = 0; 
  for (line_number = 0; line_number < N; ++line_number) {
    array[line_number] = (char *)malloc(buffer_size+1);
  }

  // read each line into the string array
  line_number = 0;
  while(-1 != getline(&(array[line_number]), &buffer_size, file))
    ++line_number;

  fclose(file);

  param_t d, dempty;

  memset(&d, 0, sizeof d);
  memset(&dempty, 0, sizeof dempty);

  int idx;
  for (idx=0; idx<N; idx++)
    {

      if(!(d.id = malloc(strlen(array[idx]) + 1))) 
    {
      //allocation failed
    }

      /* if initialized with string literals the data request works */
      d.id= "ABC";

      /* if d is initialized with a variable the data request doesn't work */
      // strcpy(d.id, array[idx]);

      fetchData(&d);

      /* reset structure*/
      d = dempty; 

    } 

  /*free memory */
    for (;idx>=0;idx--)
        free(array[idx]);
    free(array);

  return 0;
}
/*#包括“myapi.h”*///fetchData
#包括
#包括
#包括
类型定义结构参数{
字符*id;
}参数;
int countlines(字符文件名[])
{
//计算名为filename的文件中的行数
FILE*fp=fopen(文件名,“r”);
int ch=0;
int行=0;
而(!feof(fp))
{
ch=fgetc(fp);
如果(ch='\n')
{
行++;
}
}
fclose(fp);
回流线;
}
int main()
{
char filename[]=“file1.txt”;
int N=计数行(文件名);
//打开文件进行读取
FILE*FILE=fopen(文件名,“r”);
//确保文件已正确打开
if(NULL==文件)
{
fprintf(stderr,“无法打开文件:%s\n”,文件名);
返回1;
}
大小缓冲区大小=10;
/*创建一个字符指针数组*/
字符**数组=malloc(N*缓冲区大小*sizeof(字符*);
/*为每个字符串分配空间:*/
int line_number=0;
用于(线号=0;线号对于(idx=0;idx我的问题在删除每个
数组[idx]

上的
\n
字符后得到解决,请考虑将您的解决方案作为答案发布,以便问题得到解决。