-1
int main(int argc, char *argv[])    
 {
    char *line, buffer[1024];
    char *token, *setValue, *pointer;


    FILE *fp = fopen("file", "r");
    if(fp == NULL)
    {
        printf("File was unable to be opened.\n");

    }

     fgets(buffer,1024,fp);  
      printf("%s\n", buffer);
     while(fgets(buffer,1024,fp) != NULL)
  {
    strcpy(token, strsep(&buffer, ","));
    printf("%s\n", token);
  }



 return 0;
 }

I'm having a bit of trouble understanding how strsep works.. I've looked up tutorials for it, but when I try different methods, it keeps just not being able to compile.. It'd be appreciated if someone helped me understand the syntax and the way it works. Thank you.

**EDIT: 'Buffer' contains "I,was,in,the,school"

****EDIT x2: I'm trying to parse a csv file, and using the basic 'Buffer' I created on my desktop as an example. I want to separate the different words by the respective comma.

ancd3ea4
  • 195
  • 1
  • 1
  • 14
  • 1
    What exactly do you want to do? Please be specific in your questions. – nbkhope Feb 07 '18 at 00:04
  • 3
    If it won't compile it will give you a reason (error code/message or some sort) what is the one you're getting? – Steve Byrne Feb 07 '18 at 00:06
  • Please take the [tour], read [Ask] and [MCVE]. – jwdonahue Feb 07 '18 at 00:09
  • @user3386109 I changed it, but I'm getting the error message: '[warning] passing argument 2 of strcpy makes pointer from integer without a cast – ancd3ea4 Feb 07 '18 at 00:34
  • @stevenByrne, It's getting the same error message ' '[warning] passing argument 2 of strcpy makes pointer from integer without a cast' – ancd3ea4 Feb 07 '18 at 00:34
  • If I take the strcpy out and set token = strsep(&buffer, ","); I get the error message- "[warning] assignment makes pointer from integer without a cast – ancd3ea4 Feb 07 '18 at 00:36
  • The issue is it's trying to make a pointer (an address in memory) from an integer (whole number) without being told to convert it (cast) – Steve Byrne Feb 07 '18 at 00:37
  • I suspect the real problem is there's no prototype for `strsep` and the compiler is assuming it returns int. What compiler are you using? – jwdonahue Feb 07 '18 at 00:41
  • You should enable all warnings btw. That would have helped you to resolve this problem on your own. – jwdonahue Feb 07 '18 at 00:42
  • You do need to supply and MCVE. What header files are you including? – jwdonahue Feb 07 '18 at 00:44
  • @jwdonahue I'm using dev++ at the moment. And I didn't know that my warnings weren't all enabled – ancd3ea4 Feb 07 '18 at 00:44
  • Dev++ is an IDE. Is it mingw or gcc? – jwdonahue Feb 07 '18 at 00:45
  • @jwdonahue I'm using The dev++ I downloaded is 'Dev-Cpp 5.11 TDM-GCC 4.9.2 setup.exe' So I'm assuming it's gcc – ancd3ea4 Feb 07 '18 at 00:48
  • Depends on what OS you installed it on. Programmers don't assume, they verify everything or they fail as you are doing. What version of the compiler and standard libraries are you using? Go read your docs. Learn how to enable all warnings and recompile. Replace `strsep` with `strtok` and see if it doesn't start to compile. – jwdonahue Feb 07 '18 at 00:51

1 Answers1

0

regarding:

strcpy(token, strsep(&buffer, ","));

the variable token is ONLY a pointer, it has not been set to point to any memory that the application owns. Therefore, it will contain what ever trash was on the stack at the location of the variable.

The result is undefined behavior and it can lead to a seg fault event.

Suggest declare token as

char token[ 1024 ];

so it is large enough to hold a maximum length string (I.E. the length of buffer[]

as it the above wasn't bad enough:

the posted code is missing the statement: #include <string.h> so as to expose the prototype for the function strsep() so the compiler will make the assumption that the parameters and returned value are int rather than their actual types.

The posted code is also missing the statement: #include <stdio.h> so the parameter and return types for the functions: fopen(), fgets(), printf()and even the struct type forFILEare assumed to beint` rather than their actual types.

user3629249
  • 16,402
  • 1
  • 16
  • 17