Search code examples
ccsvstrcpy

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


I need to read a .csv file. Then I need to put on "value" struct array, but when I put integer variable I'm getting this "expected ‘char * restrict’ but argument is of type ‘int’" error.

This is my code:

#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;
}

This is a sample csv file:

enter image description here

This is the error I am getting:

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

Solution

  • 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 is an integer, field is a string - you cannot copy a string to an integer - you have to _interpret _ the string:

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

    Although quite possibly the id should be a string in any case, even if it contains only digits - it is not an object you are going to perform arithmetic on after all.

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

    Is simply a consequence of the previous error. Always read and fix errors from the top, and recompile if the message looks like a "run-on" from a previous issue.

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

    Same as above.

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

    Though clearly here, grade being an integer makes sense.

    Segmentation fault
    

    You ran the broken code because they were warnings not errors. All compilers have switches to regard warnings as errors (a warning is normally a semantic error rather then a syntactic error - that is the compiler can generated code, but it is unlikely that the code means what you intended), you should use that switch (-Werror in GCC and Clang, \WX in Microsoft's compiler). Code with errors won't run because it won't compile - so making warnings into errors prevents you from running semantically broken code.