C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 23rd, 2003, 08:56 AM
XSS XSS is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 5 XSS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to XSS
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.

Reply With Quote
  #2  
Old June 23rd, 2003, 09:24 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 27 m 47 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
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.

Reply With Quote
  #3  
Old June 23rd, 2003, 11:06 AM
XSS XSS is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 5 XSS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to XSS
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?

Reply With Quote
  #4  
Old June 23rd, 2003, 11:29 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 27 m 47 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
Please show the code you are having problems in.

Reply With Quote
  #5  
Old June 23rd, 2003, 11:34 AM
Milky_Destrecto Milky_Destrecto is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 5 Milky_Destrecto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
anyone know how to save .map and open .map in my program and how to do it?

Reply With Quote
  #6  
Old June 23rd, 2003, 12:01 PM
XSS XSS is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 5 XSS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to XSS
....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..

Reply With Quote
  #7  
Old June 23rd, 2003, 11:56 PM
HubGoblin HubGoblin is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 2 HubGoblin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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);
}

Reply With Quote
  #8  
Old June 24th, 2003, 08:16 AM
XSS XSS is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 5 XSS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to XSS
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.

Reply With Quote
  #9  
Old June 24th, 2003, 09:14 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 27 m 47 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
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);
	}
}

Reply With Quote
  #10  
Old June 24th, 2003, 05:29 PM
XSS XSS is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 5 XSS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to XSS
Thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Read strings howto??


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway