delimiter-terminated getline?

This is a discussion on delimiter-terminated getline? within the C Programming forums, part of the General Programming Boards category; i have two problems actually: 1) in c++, you can use the 'getline' function to read in a line that ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    delimiter-terminated getline?

    i have two problems actually:

    1) in c++, you can use the 'getline' function to read in a line that is terminated with a specified delimiter..is this at all possible in c? I am assuming that i'd have to create a loop and use getc (this is all reading input in from a file).

    2) this line of code does not seem to compile...
    sarray[index]=(struct student*)malloc(sizeof(struct student));

    student is a struct, sarray is an array of students and index is initialed to 0 at this point...any input as to what i am doing wrong? the error is saying that is an assignment mismatch.

    doomo
    eat, drink and be merry. for tomorrow: we party.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Use fgetc to implement your getline function. You can then easily implement the delimiter feature.

    sarray is an array of students while your malloc function is returning a pointer to students. That's why you're getting a mismatch. Perhaps you should create an array of pointers.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    28
    thanks for the fgetc, but i was looking for something that did the work for me (of course, i am extremely lazy)...but i will be forced to deal with my own coding inadequacies....

    as for the malloc function...i think i see my error....let's try it out though...

    doomo.
    eat, drink and be merry. for tomorrow: we party.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    28
    another fgets question, this line of code chokes when i use it:

    >fgets(sarray[index].name, 25, fname);

    at this point: index=0, sarray is a name of students, name is a character array in the student structure and fname is a pointer to a file name...the error states that argument 3 (fname) is incompatible with prototype...why???

    doomo again.
    eat, drink and be merry. for tomorrow: we party.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    28

    spoke too soon...

    i figured it out...i needed to use a FILE pointer for the third argument of fgets...jeez, i was stuck on that for the longest time....ugh.....


    doomo
    eat, drink and be merry. for tomorrow: we party.

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Here's a quick implementation of getline:

    Code:
    char* GetLine(char *buffer, int size, char delimiter, FILE *stream)
    {
       int i, c;
    
       for(i = 0; i < size - 1; i++)
       {
          c = fgetc(stream);
          if( (c == EOF) || ((char)c == delimiter) )
          {
             if(ferror(stream))
                return NULL;
             break;
          }
          buffer[i] = (char)c;
       }
       buffer[i] = '\0';
    
       return buffer;
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you don't care if you overflow your buffer potentially, you could always use fscanf with the "%[]" operator to specify your delimits, since you're lazy.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I had a quick question. Say if you used fgets and passed in size 5. Would it use all 5 slots to store the characters or would that fifth slot store the null character?
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Cshot
    I had a quick question. Say if you used fgets and passed in size 5. Would it use all 5 slots to store the characters or would that fifth slot store the null character?
    To quote fgets:
    "fgets() reads in at most one less than size characters
    from stream and stores them into the buffer pointed to by
    s. Reading stops after an EOF or a newline. If a newline
    is read, it is stored into the buffer. A '\0' is stored
    after the last character in the buffer."
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline with a dual delimiter?
    By j0hnb in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2004, 02:58 AM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21