C读取csv文件放入带有int变量错误的结构数组"strcpy"-

C read csv file put struct array with int variable error"strcpy"-

我需要读取一个 .csv 文件。然后我需要放入“值”结构数组,但是当我放入整数变量时,我得到这个 "expected ‘char * restrict’ but argument is of type ‘int’" error.

这是我的代码:

#include <stdio.h>
#include <string.h>
#define DATA_FILE "students.csv"
typedef struct record {
 int id;
 int grade;
char name[15];
char surname[15];
char email[26];

 }dict;

int main()
 {

FILE *fp;
struct record Myrecord;
fp=fopen("students.csv","r");
 if(!fp){
   printf("Error occured");
   return 0;

 }
 char buff[102400];
 int row_count=0;
 int field_count=0;
 dict values[99900];
 int i=0;
 while(fgets(buff,102400,fp))
 {
  field_count=0;
  row_count++;
  if(row_count == 1){
    continue;
  }
 
  
  char *field=strtok(buff,";");
  
  while (field)
  {
   if(field_count==0){
        
         strcpy(values[i].id,field);
    }
    if(field_count==1){
    strcpy(values[i].name,field);
    }
    if(field_count==2){
    strcpy(values[i].surname,field);
    }
    if(field_count==3){
    strcpy(values[i].email,field);
    }
    if(field_count==4){
    strcpy(values[i].grade,field);

    }
    printf("%s  ",field);
    field=strtok(NULL,";");
    field_count++;

  }    
  i++;
  
  
}   



return 0;
}

这是一个示例 csv 文件:

这是我遇到的错误:

week3.c: In function ‘main’:
week3.c:44:30: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
              strcpy(values[i].id,field);
                     ~~~~~~~~~^~~
In file included from week3.c:2:
/usr/include/string.h:121:14: note: expected ‘char * restrict’ but argument is of type ‘int’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
week3.c:56:18: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
  strcpy(values[i].grade,field);
         ~~~~~~~~~^~~~~~
In file included from week3.c:2:
/usr/include/string.h:121:14: note: expected ‘char * restrict’ but argument is of type ‘int’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
Segmentation fault

[Done] exited with code=139 in 0.174 seconds

查看错误信息。 strcpy 接受两个 char * 参数,但在 strcpy(values[i].id,field) 中,values[i].idint.

strcpy 的原型包含在编译器输出中:

extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
week3.c:44:30: warning: passing argument 1 of ‘strcpy’ makes
pointer from integer without a cast [-Wint-conversion]
              strcpy(values[i].id,field);
                     ~~~~~~~~~^~~ 

id 是一个整数,field 是一个字符串 - 您不能将字符串复制为整数 - 您必须_解释_字符串:

values[i].id = (int)strtol( field, NULL, 0 ) ;

尽管 id 很可能在任何情况下都应该是一个字符串,即使它仅包含数字 - 毕竟它不是您要对其执行算术运算的对象。

In file included from week3.c:2: /usr/include/string.h:121:14: note:
expected ‘char * restrict’ but argument is of type ‘int’  extern char
*strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~

只是前一个错误的结果。始终阅读并修复顶部的错误,如果消息看起来像上一期的“运行-on”,则重新编译。

week3.c:56:18: warning: passing argument 1 of ‘strcpy’ makes pointer
from integer without a cast [-Wint-conversion]  
strcpy(values[i].grade,field);
         ~~~~~~~~~^~~~~~ In file included from week3.c:2: /usr/include/string.h:121:14: note: expected ‘char * restrict’ but
argument is of type ‘int’  extern char *strcpy (char *__restrict
__dest, const char *__restrict __src)
              ^~~~~~ 

同上

values[i].grade = (int)strtol( field, NULL, 0 ) ;

虽然这里很清楚,grade 是一个整数是有道理的。

Segmentation fault

你 运行 损坏的代码,因为它们是警告而不是错误。所有编译器都有将警告视为错误的开关(警告通常是语义错误而不是语法错误 - 即编译器可以生成代码,但代码不太可能意味着你想要的),你应该使用那个开关( -Werror 在 GCC 和 Clang 中,\WX 在微软的编译器中)。有错误的代码不会 运行 因为它不会编译 - 所以将警告变成错误可以防止你 运行 宁语义损坏的代码。