#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    5
    Rep Power
    0

    Read strings howto??


    Hello!

    Im kinda new at this and i got this small problem to which i havent found any simple explanation nor description to.

    Im doing this small program as a way to learn some of the more interesting things about C programming, this is btw. in pure C.

    I need to reed a string which is inputted from user ie.

    char name[40];

    printf(" Name (firstname lastname):";
    ...code to read string...

    Yeah well anyway, i want the answers to be as simple as possible because i got ALOT of answer to this question from my friends, some say use fgets or sscanf and so on but i can't get it to work at all, so please.
    Last edited by XSS; June 23rd, 2003 at 09:58 AM.
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Here is as sample program to read input from the user:
    Code:
    #include <stdio.h>
    
    int main() {
    	char temp[50];
    
    	printf("Enter your first name please: ");
    	fgets(temp, 49, stdin);
    
    	printf("\nYou entered %s as your first name.\n",temp);
    }
    This was compiled using gcc compiler.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    5
    Rep Power
    0
    Thanks for the help, no one ever showed me real code of this, and it worked but not in my program though, i got this string thing in a 'while loop' and now it skips the entering of 'firstname' string, any help on that topic?
  6. #4
  7. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Please show the code you are having problems in.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    5
    Rep Power
    0
    anyone know how to save .map and open .map in my program and how to do it?
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    5
    Rep Power
    0
    ....other code...


    while (menu_choice != 5)
    {
    menu_choice = phonebook_menu();

    if (menu_choice == 1)
    {
    ...not interesting...
    }
    else if (menu_choice == 2)
    {
    if ( (file = fopen("pb.db","w")) == NULL)
    {
    printf("Can't open %s\n", file);
    exit(1);
    }
    else
    {
    printf(" Firstname: ");
    fgets(firstname, 29, stdin);
    printf("%s", firstname);
    }
    }
    }
    }

    That's about it, i have seen this problem before, ie. the program skips the entering stage and just keeps running..
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0

    sample codes


    here is sample code for your question about reading strings:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    void main(void)
    {
    char name[80]; // static variable , could be dynamic
    printf("\nEnter first name:");
    // first method and the best according to me, works perfect
    gets(name);
    //second method, not recomended
    scanf("%s",&name);
    printf("\nYou entered %s",name);
    }
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    5
    Rep Power
    0
    Well, thanks for the reply but it's not what i expected.

    First of all, gets is vulnerable to buffer overflows afaik, and my compiler gcc is warning me about it, i think it doesnt even let me compile with gets.

    Second of all, scanf does the job but it doesnt tolerate whitespaces, thats space, tabs and such so i can't read a string with those in it.

    Third.
    I also needed help with the program skipping the input from user, i've solved the string problem this far but it skips the input part of the program, you can see the code further up this thread, i've changed the while loop to a switch case which also work as it should but still it skips input from user, it prints:
    " Firstname: "
    "bash% "

    So it just quits without pausing for input.
    Last edited by XSS; June 24th, 2003 at 09:21 AM.
  16. #9
  17. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Here is a little sample code that gets input from the user and writes it to a file, and when the file exits it prints the file to the screen. Written in a linux environment...
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stddef.h>
    #include <stdlib.h>
    
    int phonebook_menu();
    void write_file(char *line);
    
    int main() {
    	FILE *fp;
    	int menu_choice = 0;
    	char entry[50];
    	char line[50];
    
    	while(5 != (menu_choice = phonebook_menu())) {
    		switch(menu_choice) {
    			case 1:
    				printf("Enter your first name:");
    				fgets(entry, 49, stdin);
    				write_file(&entry[0]);
    				break;
    			case 2:
    				printf("Enter your phone number:");
    				fgets(entry, 49, stdin);
    				write_file(&entry[0]);
    				break;
    			case 3:
    				printf("Enter your age:");
    				fgets(entry, 49, stdin);
    				write_file(&entry[0]);
    				break;
    			case 5:
    				printf("You have selected to quit.\n");
    				break;
    			default:
    				printf("You entered %lu\n\n",menu_choice);
    				break;
    		}
    	}
    	fp = fopen("pb.db", "r");
    	if (fp != NULL) {
    		while (!feof(fp)) {
    			bzero(&line[0], sizeof(line));
    			fgets(line, 80, fp);
    			printf("%s", line);
    		}
    		printf("End of file.\n");
    		fclose(fp);
    	} else {
    		printf("Failed to print file contents.\n");
    	}
    }
    
    int phonebook_menu() {
    	char entry[50];
    
    	system("clear");
    	printf("   Phonebook Menu\n");
    	printf("----------------------\n");
    	printf("1. Record Name\n");
    	printf("2. Record Phone Number\n");
    	printf("3. Record Age\n");
    	printf("\n");
    	printf("5. Quit\n");
    	printf("Enter an option:");
    	fgets(entry, 49, stdin);
    	return atoi(entry);
    }
    
    void write_file(char *line) {
    	FILE *fp;
    
    	fp = fopen("pb.db", "a");
    	if (fp != NULL) {
    		fprintf(fp, "%s", line);
    		fclose(fp);
    	}
    }
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    5
    Rep Power
    0
    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo