Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2006, 11:26 PM   #1
novkhan
Newbie
 
Join Date: Apr 2006
Posts: 3
Rep Power: 0 novkhan is on a distinguished road
simple fputs question.

Gooday

A real beginner of c. So hope to get some help.

here goes
If user key in "5"
create a file name "raw.txt"
prompt user to key in "A" or "B".
Example is "A" is selected. raw.txt inside must reflect "a"
if user then select "B"
raw.txt must reflect "b" on newline. :banana:
novkhan is offline   Reply With Quote
Old Apr 19th, 2006, 11:59 PM   #2
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
This is not that hard. Use some if/else statements in your code for controlling user input.
Check this out for fputs();
http://irc.essex.ac.uk/www.iota-six....putc_fputs.asp

Please consider reading the "How to post a question" thread. Thank you.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Apr 20th, 2006, 4:04 AM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
@Prm753: Using gets, is a very bad idea.

@OP: Welcome to the forums, please read the "How to post a question" thread at the top of the C/C++ forum. A quick question: Why are you beginning with C instead of C++?
Anyway, here's some code, if you don't understand it read your man pages, manual or search for the functions on internet, you can find them here.
If you do want to use C++, you can find a tutorial here, the stuff on pointers isn't exactly good or accurate last time I checked, but you can find that in DaWei's profile.
#include <stdio.h>

int main()
{
	int i = 0, gotit = 0;
	char c = 0;
	FILE * fp;

	do
	{
		printf("Enter number 5: ");
		gotit = scanf ("%d", &i);
		rewind(stdin);
	}
	while((gotit != 1) || (i != 5));

	if((fp = fopen("raw.txt", "wb")) == NULL)
	{
		fprintf(stderr, "Cannot create/open file\n");
		return 1;
	}

	do
	{
		printf("Enter 'A' or 'B' (capital letters): ");
		gotit = scanf ("%c", &c);
		rewind(stdin);
	}
	while((gotit != 1) || ((c != 'A') && (c != 'B')));

	if(c == 'A')
		fputs("a", fp);
	else
		fputs("b", fp);

	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 20th, 2006, 4:15 AM   #4
novkhan
Newbie
 
Join Date: Apr 2006
Posts: 3
Rep Power: 0 novkhan is on a distinguished road
Nnixonnnnnn...................

Hey NNIXON REAL THANKS
i still have the last part. Hope you can you help.
Big Thankyou!!!!!!

PART A (You have solved!) :banana:
If user key in "5"
create a file name "raw.txt"
prompt user to select "A" or "B".
Example is "A" is selected. raw.txt inside must reflect "a"
if user then select "B"
raw.txt must reflect "b" on newline.


PART B (Hope you could help! Thanks)
now we have either "a\n, b\n" or "b\n,a\n" in raw.txt
compare with "match.txt"
In "match.txt" it will indicate rules. If "a\n, b\n" print "Apple Pie" or "b\n, a\n" print "Chicken Pie"
print the results
novkhan is offline   Reply With Quote
Old Apr 20th, 2006, 4:21 AM   #5
novkhan
Newbie
 
Join Date: Apr 2006
Posts: 3
Rep Power: 0 novkhan is on a distinguished road
By the way I am learning C. not C++...
novkhan is offline   Reply With Quote
Old Apr 20th, 2006, 5:18 AM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Is it some homework assignment?
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 20th, 2006, 5:29 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by novkhan
By the way I am learning C. not C++...
Yeah you told us, but WHY? Why not C++? C is OLD. :p

You can read the file. (Hint: use fscanf or fgets.)
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 20th, 2006, 7:27 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
C is OLD. :p
Bite me, Ruben, C is young....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 20th, 2006, 7:02 PM   #9
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Quote:
Originally Posted by DaWei
Bite me, Ruben, C is young....
My boss suddenly have a crush on PL/I. Three poor sools from the shop were chosen to learn it from a book. His point was that PL/I is better than what we use now(COBOL). I have to agree with him, PL/I is newer than COBOL. It was introduced in 1960, COBOL in 1959.
Did I mention he is 66+ years old. I'm sure PL/I was pretty fly when he was in college.
OpenLoop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC