no string type in C?

This is a discussion on no string type in C? within the C Programming forums, part of the General Programming Boards category; Since there is no string type in C how do read in a string? For example: I want to prompt ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    43

    no string type in C?

    Since there is no string type in C how do read in a string? For example:

    I want to prompt the user for the name of a file. Using the name of the file I want to open the file.
    Since I won't know the name of the file in advance I can't store the characters in an array because I wont know the size of the string. Is there a function that does this that I don't know about?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > Since I won't know the name of the file in advance I can't store the characters in an array because I wont know the size of the string
    Most systems impose limits on the upper length of a filename, which shouldn't be a problem.

    > Is there a function that does this that I don't know about?
    I dunno, do you know about "malloc" and "realloc"?
    Well now you do
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Thx for the responsse Salem,but is there an easier way to do this? I'm not that advanced in my skills yet.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you want strings, use C++; otherwise you'll have to create char arrays that are big enough to do what you want.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by mesmer View Post
    Thx for the responsse Salem,but is there an easier way to do this? I'm not that advanced in my skills yet.
    In C, no.
    In C++, yes (as cpjust kindly points out).
    And no, there is no "string type" in C, because it's... well, C. There is an array of chars, which are used to make C-strings, but they are not strings. It is just the kind of thing you have to work with when working with a low-level language like C.
    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.

  6. #6
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Quote Originally Posted by mesmer View Post
    Thx for the responsse Salem,but is there an easier way to do this? I'm not that advanced in my skills yet.
    Utilizing the upper limit for paths is arguably the easiest way to do it. The PATH_MAX constant, while not standard to C, is quite convenient as it is defined both in Windows in windows.h and in POSIX environments (Linux, Mac OSX) in limits.h.
    Use it as the size for a char array in your declarations.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    To limit the input in scanf() and prevent the overflow of a buffer you'll need to put a number between % and s:

    Code:
    char buf[1024];
    
    scanf("%1023s",&buf); //Make space for a '\0'
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Better to use fgets. And you should not use & before buf, because that is a pointer to an char array and these functions expect a pointer to char.
    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.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Oh yea.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

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