Thread: reading data from strings

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    reading data from strings

    Hi guys, I'm doing a final year project that requires some programming in C language.

    +CMTI : "SM",2

    +CMGR : "REC UNREAD", "+6512345678", "08/05/27"
    How do I read the value "2" and the mobile phone number, +6512345678", from the data received?

    The data is received serially from a GSM modem.

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    87
    Check out reading formatted input with the scanf family of functions:
    http://www.opengroup.org/onlinepubs/...ns/fscanf.html

    Here are some examples to get you thinking in the right direction:

    Code:
    #include <stdio.h>
    
    int main() {
      char *str1 = "\"SM\"";
      char *str2 = "val1,val2";
      char buf1[10];
      char buf2[10];
      char buf3[10];
    
      /* read everything from an opening quote up to an end quote */
      sscanf(str1, "\"&#37;[^\"]s", buf1);
      printf("%s\n",buf1);
      
      /* break up the input string by the comma character */
      sscanf(str2, "%[^,],%s", buf2, buf3);
      printf("buf2=%s\tbuf3=%s\n", buf2, buf3);
    
      return 0;
    }
    Specifically, read the scanf documentation around character sets [...] and [^...].

    That should get you going in the right direction.

    Cheers,
    Jason

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    4
    Thanks but I still don't understand how to get values from the string.

    Maybe you could use my quotes and use it as an example?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jason_m View Post
    Code:
    #include <stdio.h>
    
    int main() {
      char *str1 = "\"SM\"";
      char *str2 = "val1,val2";
      char buf1[10];
      char buf2[10];
      char buf3[10];
    
      /* read everything from an opening quote up to an end quote */
      sscanf(str1, "\"%[^\"]s", buf1);
      printf("%s\n",buf1);
      
      /* break up the input string by the comma character */
      sscanf(str2, "%[^,],%s", buf2, buf3);
      printf("buf2=%s\tbuf3=%s\n", buf2, buf3);
    
      return 0;
    }
    I'm sorry, but this code fails too:
    It's buffer overrun prone: http://cpwiki.sourceforge.net/Buffer_overrun
    It uses char where const char should be used: http://cpwiki.sourceforge.net/Common...kes_and_errors
    And finally a presentation about C security: http://www.cse.scu.edu/~tschwarz/COE...es/Strings.ppt

    Winston Hong: If you do not understand this simple code, then I'm afraid you need to study a bit harder. C is not exactly trivial, but we cannot directly give our the answer because you wouldn't learn.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    A final year project? Your final year of school? What are you studying?

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by rags_to_riches View Post
    A final year project? Your final year of school? What are you studying?
    does it matter?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    87
    Quote Originally Posted by Elysia View Post
    I'm sorry, but this code fails too:
    It's buffer overrun prone: http://cpwiki.sourceforge.net/Buffer_overrun
    It uses char where const char should be used: http://cpwiki.sourceforge.net/Common...kes_and_errors
    And finally a presentation about C security: http://www.cse.scu.edu/~tschwarz/COE...es/Strings.ppt
    You are right. This was a lazy, late night attempt on my part to quickly introduce the the conversion specifiers and parsing with scanf. The code works as is just fine, but could lead readers astray.

    Maybe tonight after work I'll toss up an example that is a little more secure.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I'm curious about a "final year project." I'm not sure if that means the final year of a degree program, or if this is the final project of his first year.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    4
    Well, its not that I don't understand what he's doing but..I still don't really see how it relates to my project. I think I need to look into the code more thoroughly.

    And FYI, I'm a student doing a project for my final year of a degree.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, so it may not be THE best example, but if you know how strings are composed and how to use them, then you should have no trouble with this at all.
    Possibly some understanding of pointers, as well.
    You know where inside that string the information you want it stored.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    87
    In general, reading messages like this I find to be difficult, or at at least tedious. You need to have a full understanding of the protocol...the language that is being spoken across the medium. Once you know that, the general algorithm that you follow might be something like:

    1. Read enough characters to know what type of message I'm dealing with.
    2. The protocol will tell me how messages of that type are formatted. If I know the format of the message now completely, then read in the rest of the message, storing it in a buffer. Then parse the message using scanf(). If I don't know fully the format of the message, the protocol must tell me at least what to expect next, so go back to step 1, read a little more, decide what to do. From there, either read a little more or finally parse the string. Etc. etc..

    The intension of my code was to introduce the &#37;[^...] and indirectly, the %[...] conversion specifiers. These will come in handy if you need to read characters belonging to a certain set, or if you need to stop when you encounter a character from a certain delimiting set (for example, an end quote, a comma, etc. - these are the cases my examples address.) There is a lot of good stuff in the documentation file I linked to in my post above. If you read through it all, you'll be a pro at parsing strings.

    The critique of my code was that it arbitrarily picks storage buffers of length 10 and goes on to read data into them without doing any bounds checking.

    C does not enforce the end of an array. If you try to write beyond it, the language will let you, and you'll probably end up messing up all sorts of stuff. You could write over other variables or even write over your stack (including the return pointer!!) This leads to buffer overflows exploits and embarrassed programmers.

    You can limit the number of characters scanf will read into your string, I'll leave it up to you to read through the documentation and see how that is done.
    Last edited by jason_m; 05-28-2008 at 06:50 PM. Reason: need to get better at proofreading

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    4
    Thanks for the link jason_m. It was really helpful and I've gotten what I wanted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  4. reading in file data
    By sickofc in forum C Programming
    Replies: 2
    Last Post: 03-01-2006, 05:16 PM
  5. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM