reading strings with spaces

This is a discussion on reading strings with spaces within the C Programming forums, part of the General Programming Boards category; hi guys im having trouble allocating strings (with spaces) to an array and then printing them out to screen. here ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    reading strings with spaces

    hi guys im having trouble allocating strings (with spaces) to an array and then printing them out to screen. here is an example prog that I created hopefully you can understand what im trying to do.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char *names[30];
    	fgets(&names[0],30,stdin);
    	fflush(stdin);
    	fgets(&names[1],30,stdin);
    	fflush(stdin);
    	printf("%s   %s", &names[0], &names[1]);
    	return 0;
    }
    I want the program output to look like this

    eldorado 09
    eldorado 92

    would appreciate the help thanx in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    See that FAQ as to:
    Code:
    fflush(stdin);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You haven't allocated any memory for those pointers (you have an array of pointers that point somewhere random...).

    There is a good reason why you've seg' fault'ed, it is in fact valid code (upto n = something). Allocate space at each of these pointers, and use their address (what they store), not the address of each array element.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    sorry about that I didnt mean to put the fflush(stdin) in just something someone told me to try tried and forgot to get rid of because there was no change, so the program is as below:
    #include <stdio.h>
    Code:
    int main()
    {
    	char *names[30];
    	fgets(&names[0],30,stdin);
    	fgets(&names[1],30,stdin);
    	printf("&#37;s   %s", &names[0], &names[1]);
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    sorry zacs7 could you modify my code to what you said would help me to understand what you are saying better
    thanx

  6. #6
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    This is working code.

    Also depending on what you are trying to do you may need to remove the newline character that fgets() will usually take at the end of the input stream.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void Remove_Newline(char * data);
    
    int main()
    {
    	char *names[30];
    	names[0] = (char*)malloc(sizeof(char) * 31);
    	if (!names[0]) return 0;
    	
    	names[1] = (char*)malloc(sizeof(char) * 31);
    	if (!names[1]) {
    		free(names[0]);
    		return 0;
    	}
    	
    	fgets(names[0], 30, stdin);
    	fgets(names[1], 30, stdin);
    	
    	printf("&#37;s\t%s", names[0], names[1]);
    	
    	free (names[0]);
    	free (names[1]);
    	return 0;
    }

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Have a look at using pointers without allocating first.
    http://cpwiki.sourceforge.net/Common...kes_and_errors
    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.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    Yer ok thats cool, the program is for someone else who I am helping in an intro to c class that i beleive has not yet learnt dynamic memory allocation, so my question is, is ther any other way this can be done without it? if not they must have learnt it already. thanx for the help guys

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Yes, make a fixed array.
    char buf[50][10];
    fgets(bud[0], sizeof(buf[0]), stdin);
    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.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Please don't tell me you're actually teaching someone C and you didn't know this!?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Quote Originally Posted by zacs7 View Post
    Please don't tell me you're actually teaching someone C and you didn't know this!?
    ...
    Well, zacs sure has a point there.
    If you cannot even do this yourself, then you should not be teaching others... yet, at least.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick check on reading in + using strings
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 12-08-2008, 09:12 PM
  2. Help with reading strings and opening files PLEASE
    By green2black in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 04:46 PM
  3. reading from a file + list of strings
    By stewie1986 in forum C Programming
    Replies: 2
    Last Post: 12-06-2007, 10:59 PM
  4. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 04:18 AM
  5. Removing spaces from strings
    By PunkyBunny300 in forum C Programming
    Replies: 6
    Last Post: 02-21-2003, 01:37 PM

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