0

I am very new to C. I writing some code to try and separate a stdin string by commas, but I am not sure how to best do this.

Basically, I have some data that will be typed in by the user in the following format:

UserID (char[]), 00:00:00 (for time,(char[]), MeasurementID (char[]), readingValue (double)

I need to separate out these values so that I can assign them each to a variable. I have some code written where I have attempted to do this, however, I know that it is not quite right.

char buffer [1024];
char input[];
scanf("%s", input);


int temp = 0;
int timekeeper = 0;
char userID[];
char timestamp[];
char measurementID[];
double value;

for(temp = 0; temp < sizeof(input); temp++){
    char val[1024];
    while(input[temp] != ','){
        val += input[temp];
    }
    if(timekeeper == 0){
        userID = val;
    }
    else if(timekeeper == 1){
        timestamp = val;
    }
    else if(timekeeper == 2){
        measurementID = val;
    }
    else if(timekeeper == 3){
        value = (double)val;
    }
    timekeeper++;
    
}

These are the errors I am getting, if they are any help.

exp.c:87:7: error: invalid operands to binary + (have ‘char[1024]’ and ‘int’)

exp.c:90:6: error: incompatible types when assigning to type ‘char[1024]’ from type ‘char *’

exp.c:93:13: error: incompatible types when assigning to type ‘char[1024]’ from type ‘char *’

exp.c:96:8: error: incompatible types when assigning to type ‘char[1024]’ from type ‘char *’

exp.c:99:3: error: pointer value used where a floating point value was expected

Community
  • 1
  • 1
  • Never ever use `scanf("%s", input);` Use `fgets` instead. – Support Ukraine Mar 18 '17 at 09:50
  • Here `char input[];` array size is missing. – Support Ukraine Mar 18 '17 at 09:54
  • You cannot extend character strings using the + in C. – David Hoelzer Mar 18 '17 at 10:30
  • regarding this line: `scanf("%s", input);` 1) always check the returned value (not the parameter value) to assure the operation was successful. 2) when using the `%s` input/conversion specifier, always include a MAX CHARACTERS modifier so the user cannot overrun the input buffer. Such overrun is undefined behavior and can/will lead to a seg fault event. Note: the `scanf()` will stop inputting characters at the first occurrence of a `white space char (for instance a space), so the whole line would not be input. Suggest using `fgets()` or `getline()` – user3629249 Mar 18 '17 at 16:13
  • this kind of statement: `char userID[];` will define a pointer to char, with the pointer not being initialized to point to anything in particular. I.E. change that declaration to something like `char userID[ 20 ];` – user3629249 Mar 18 '17 at 16:17
  • suggest learning how to use `strtok()` – user3629249 Mar 18 '17 at 16:20
  • this kind of line: `val += input[temp];` will not work, however you might try:` char val[1024]; val[0] = '\0';` along with: `val[ strlen(val)] = input[temp]l and finally `val[ strlen(val)] = '\0';` – user3629249 Mar 18 '17 at 16:26

2 Answers2

2

It seems you have misunderstood how strings work in C.

Doing something like

val += input[temp];

isn't valid C code. You don't add characters to a C string like that. There are special function for manipulating strings, e.g. strncat, strncpy and more.

Also definitions like

char userID[];

are invalid as there is no array size.

Notice that

sizeof(input);

will give you the size of the variable but you probably want the length of the string, i.e. use strlen(input) instead.

Further you can't convert a string to a double by

value = (double)val;

Instead look up the atof function.

This part

while(input[temp] != ','){
    val += input[temp];
}

will give you an endless loop as temp (and thereby input[temp]) doesn't change.

I'll recommend that you take a look at this answer Split string with delimiters in C to learn how you split a string into a number of strings.

Community
  • 1
  • 1
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

You cannot concatenate char* and char[] (the "|") or any permutation of using +. Use strncat() or snprintf() instead and ensure the destination buffer has enough memory to store the final string.

Ashwin Golani
  • 498
  • 3
  • 10
  • So would that look something like this? strncat(val, (char)input[temp], 1024); – star_sh00ter Mar 18 '17 at 10:04
  • Here is a example....http://stackoverflow.com/questions/14219418/error-invalid-operands-to-binary-char – Ashwin Golani Mar 18 '17 at 10:08
  • @star_sh00ter, this expression: `(char)input[temp]` has a couple of problems. 1) `input[temp]` is already a single character so no need to cast. 2) the second parameter to `strncat()` must be a pointer, not a char (though it could be a pointer to an array of char) – user3629249 Mar 19 '17 at 01:58