0

Im still new to C, and Im a bit confused on how to work with strings at the moment. right now I have two functions: get_field(), and get_line().

typedef char f_string[MAX_CHARS+1] ;
typedef struct {
        int nfields ;                        
        f_string field[MAX_FIELDS] ;            
} csv_line ;



csv_line get_line() {
        csv_line toBe;
        f_string sField;
        toBe.nfields = 0;
        int r;
        while(r != '\n'){
                r = get_field(sField);
                printf("sField: %d\n", *sField);
                //toBe.field += *sField;
                if(r != EOF){
                        toBe.nfields += 1;
                }
                //sField = *"";
        }
        return toBe;

}


int get_field(f_string field) {
        char ch;
        ch = getchar();
        while(is_end_of_field(ch) == 0){
                field += ch;
                ch = getchar();
        }
        field += '\0';
        return ch;

What I am trying to do with it is parse through a line from standard input until it gets to an end of field condition (either ',' '\n' or EOF) then take that string and add it to the "field" which I believe is an array of these fstrings. get_field() seems to run fine but when I try to print out the sField, which I believe is being edited from within get_field, I only get a 0. What am I doing wrong here? MAX_FIELDS is set to 15 and MAX_CHARS is set to 20. The Errors I get when I try to compile with the currently commented out lines are...

error: invalid operands to binary + (have ‘char[15][21]’ and ‘int’)
error: incompatible types when assigning to type ‘f_string’ from type ‘char’
4
  • What is your intention with field += ch;? Commented Apr 17, 2014 at 3:18
  • I want to add the character to the field string which will later be combined with the toBe.field array. Commented Apr 17, 2014 at 3:26
  • The you should use something like field[i] = ch;, because in get_field() field is a pointer to char. Commented Apr 17, 2014 at 3:28
  • I'd advise to not use array typedefs while learning, they can be unintuitive and act as an obstacle to learning. Commented Apr 17, 2014 at 4:00

1 Answer 1

2
  1. You are using r before you initialized it at while(r != '\n'){

  2. toBe.field += *sField; should be replaced by something like strcpy(toBe.field[i], sField);

  3. field += ch; should be replaced by something like field[i] = ch;, because in get_field() field is a pointer to char; field += '\0'; should be fixed similarly.

By the way, there are many potential buffer overflow problems in your code, you may also want to fix them.

Sign up to request clarification or add additional context in comments.

2 Comments

Ive done this but now I have a few other issues, whenever I try to print out "ch" it only gives me the number value of it, also the values of my "sField" strings which should contain an entire field only contains the first character.
@BLU How do you print ch? You should use something like printf("%c", ch); instead of printf("%d", c);, that latter form you give you the ASCII value of ch.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.