|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 08:58 AM. |
|
#2
|
||||
|
||||
|
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);
}
|
|
#3
|
|||
|
|||
|
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?
|
|
#4
|
||||
|
||||
|
Please show the code you are having problems in.
|
|
#5
|
|||
|
|||
|
anyone know how to save .map and open .map in my program and how to do it?
|
|
#6
|
|||
|
|||
|
....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.. |
|
#7
|
|||
|
|||
|
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); } |
|
#8
|
|||
|
|||
|
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 08:21 AM. |
|
#9
|
||||
|
||||
|
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);
}
}
|
|
#10
|
|||
|
|||
|
Thanks!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Read strings howto?? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|