Converting char array to double in C?

How to accept string with space in char array in C?

  • accepting string of characters that have spaces eg,(c is a good programming language) in a single character array (char arr[100]) works fine,but when i try to store the same in a char array that is a structure variable doesn't works eg. struct flleinfo { char file[20]; char description[100]; } ; main() { struct fileinfo fl[10]; int i; for(i=0;i<=9;i++) { printf("enter file name\n"); scanf("%s",fl[i].file); printf("enter description\n"); scanf("%s",fl[i].description); } } don't bother about the programming errors just tell me how do i store the description for individual file in a structure.description of individual file does contain spaces in between.i even tried to do with gets() function i.e gets(fl[i].description) but it is not accepting any values.

  • Answer:

    - Avoid using scanf with "%s". - Avoid gets(). See these links for more information: http://c-faq.com/stdio/getsvsfgets.html http://c-faq.com/stdio/scanfprobs.html #include <stdio.h> static int readline(char *s, size_t size) {         int c;         size--;         while ((c = getchar()) != EOF && c != '\n')                 if (size) {                         *s++ = c;                         size--;                 }         *s = '\0';         return c == EOF; } struct fileinfo {         char file[20];         char description[100]; }; int main(void) {         struct fileinfo fl[10];         size_t i;         size_t n;         for (i = 0; i < sizeof fl / sizeof fl[0]; i++) {                 printf("enter file name\n");                 if (readline(fl[i].file, sizeof fl[0].file))                         break;                 printf("enter description\n");                 if (readline(fl[i].description, sizeof fl[0].description))                         break;         }         for (n = 0; n < i; n++) {                 puts(fl[n].file);                 puts(fl[n].description)…‡         }         return 0; } edit: Enrolling Indian students in courses on C is like releasing a swarm of moths into a room of burning candles. I don't see how concepts like the danger of gets() is difficult to grasp. You'd think that the fact that it was deprecated in C11 would lead to a reduced use of it, but Indians are clearly ignorant of C standards; they consider the behaviour of their beloved, outdated Turbo C to be the same across all implementations of C - from "sizeof (int)" to the availability of "conio.h".

arc at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Henni's got the right idea. There is indeed no safe way to use gets(). But, you can use scanf safely. Few do (see the earlier answer) because it's a nusiance, but it can be done. In your program, use: printf("enter file name\n"); scanf("%19[^\n]",fl[i].file); printf("enter description\n"); scanf("%99[^\n]",fl[i].description); This is similar to James Bond's answer, except for the maximum lengths above. The %[list] scanf format is similar to %s, but only accept characters that are in the list. (If you want to include a ] as a valid character, make it first. %[]abc] accepts ']', 'a', 'b', or 'c', for example. The %[^list] format scans and accepts all characters that are NOT in the list. Both formats can (AND SHOULD) be prefixed with a maximum length. This does not include the trailing 0 string terminator, so it should be one less than the buffer size. Since char file[20] can hold up to a 19-character string, scan it with %19s or %19[list] or %19[^list]. Using the negative format %19[^\n] scans up to 19 characters that are not newline (\n) characters. That includes spaces, tabs, other control characters (except \n), and all printable characters. The nuisance factor is having to type the length in decimal in every scanf() format that reads into a particular array. If the array size changes, you must change the formats. For a large project, with lots of different places to scan into different string buffers, something like Henni's approach is correct. For small projects, I prefer getting the most use out of the safe part of the Standard C Library...which is most of it. Avoiding scanf() entirely because of buffer overruns caused by plain %s and %[] is throwing a lot of baby out with very little bathwater.

husoski

char x[80]; scanf("%[^\n]", x); // this read a line full of text which contains spaces also scanf("%[^g]", x); // this reads character till we enter g

James Bond

To read a character array with spaces you better use 'gets' command rather than scanf for(i=0;i<10;i++) { printf("enter file name\n"); gets(fl[i].file); printf("enter description\n"); gets(fl[i].description); }

mounika

if you will use struct then you can not accept string with space in char array in C use this program for accept string with space in char array in C this program also works for accept string with lines. main() { char a,b,c,d; scanf("%c%c%c%c",&a,&b,&c,&d); printf("%c%c%c%c",a,b,c,d); } you will see the output i compiled it my input was a<space><space>f out put was a<space><space>f and if you want add for loop then add it by your self because i am lazy to type a big program lol

Xx

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.