C读取csv文件将结构数组与int变量错误“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 文件

enter image description here

这是我得到的错误

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)
              ^~~~~~

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

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,0 ) ;

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

Segmentation fault

您运行损坏的代码是因为它们是警告而不是错误。所有编译器都有将警告视为错误的开关(警告通常是语义错误而不是语法错误 - 即编译器可以生成代码,但代码不太可能符合您的意图),您应该使用该开关( -Werror 在 GCC 和 Clang 中,\WX 在 Microsoft 的编译器中)。有错误的代码不会运行,因为它不会编译 - 因此将警告转化为错误可以防止您运行语义损坏的代码。