Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 03-29-08   #1 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: oklahoma
Posts: 775

Rep: 27 Pooping^fish is acknowledged by some
Unique Rep: 25
Trader Rating: 0
Default Need file write help

Well, after long discussion and bugging of Mr. Hobo (as the usual) we couldn't get this going.
I need to ask the user to input the name of a file, then the app then uses what they entered for the name of the file to be written and saved.
Tried everything from const to string and we it gives an error somewhere.

Help please?

Code:
#include <string>
#include <iostream>
#include <fstream>
using namespace std; 
int main() {
 const char * filename; //variable to store file name?
ofstream the_file; //variable to refer to stream to write to file ?
  
    cout<<"what do you want the name of your webpage to be? NOT the title!";
    cin>>filename; 
// open stream/open file of output?
the_file.open (&filename,ios::out);
return 0;
}
What type of loop would I use?
I need to ask multiple questions and wrap different tags around the users input for each question.
__________________
Quote:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: CSS Pwner
CPU
e6400 @ 3.2
Motherboard
p5n-t 780i
Memory
2gb ocz @ 900
Graphics Card
8800gtx flashed to 621/2ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TR u120 Extreme
GPU cooling
stock
OS
Ubuntu, Vista ult. SP1
Monitor
24" Westy

Last edited by Pooping^fish : 03-29-08 at 01:16 AM.
Pooping^fish is offline   Reply With Quote
Old 03-29-08   #2 (permalink)
*cough* Stock *cough*
 
loony's Avatar
 
amd nvidia

Join Date: Jul 2005
Location: Daisy Hill FTW!!
Posts: 489

Rep: 15 loony Unknown
Unique Rep: 15
Trader Rating: 0
Default

Whats this being done in? VB, C++, C#
__________________

Quote:
Originally Posted by jinja_ninja View Post
I'm sure it may increase games by 1fps if you stand on your head, clap three times and sing Old Mac Donald before playing the game.

System: Sexy Boy
CPU
Athlon XP 2200+ T-Bred (1.8ghz)
Motherboard
FIC AM-37
Memory
2x512mb OCZ
Graphics Card
PoV GeForce FX5200 128MB
Hard Drive
160gig Seagate Barracuda
Sound Card
Crystal SoundFusion
Power Supply
Channel Well 350W
Case
Compaq
CPU cooling
Stock heatsink and fan
GPU cooling
Stock
OS
Windows XP SP3
Monitor
Dell E153FP
loony is offline   Reply With Quote
Old 03-29-08   #3 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,275

Rep: 284 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 217
Folding Team Rank: 281
Hardware Reviews: 1
Trader Rating: 3
Default

A few things wrong I see, assuming its C++.

First, where you declare the filename variable, you have declared it as a constant. When you declare a var as a constant, that means you cant change it anywhere in your program. Notice later you try to read into filename. Also you are probably going to want to make filename an array or a string. Here is what I'd write for your code.

Code:
#include <string>
#include <iostream>
#include <fstream>

using namespace std; 

int main()
{
   char filename[51]; //variable to store file name //declares an array of characters to store the file name, 50 chars long. 
   ofstream the_file; //variable to refer to stream to write to file
  
   cout <<"What do you want the name of your webpage to be? NOT the title!";
   cin  >> filename; //depending on what you want to allow to be as the file name, consider using cin.get?

//open stream/open file of output? //you don't need to have all that other junk in there.
   the_file.open(filename); 
   
   return 0;
}
As for loops, probably a while or dowhile loops, depending on what you want done.
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is offline I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 03-29-08   #4 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: oklahoma
Posts: 775

Rep: 27 Pooping^fish is acknowledged by some
Unique Rep: 25
Trader Rating: 0
Default

Sweet, thankyou. That worked.
I figured out I guess I dont exactly need to use a loop, perhaps a function and string to finish this. It will ask multiple questions requiring user input, and it will put text around (begginning and end) of the input.

Im confused as exactly how to get it to take input from the user and put the specified text around that. Which one side will differ from another.
__________________
Quote:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: CSS Pwner
CPU
e6400 @ 3.2
Motherboard
p5n-t 780i
Memory
2gb ocz @ 900
Graphics Card
8800gtx flashed to 621/2ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TR u120 Extreme
GPU cooling
stock
OS
Ubuntu, Vista ult. SP1
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 03-29-08   #5 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,275

Rep: 284 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 217
Folding Team Rank: 281
Hardware Reviews: 1
Trader Rating: 3
Default

Will the surrounding tags depend on the answers or the questions? If the tags depend on the question you could follow something like:

Write opening tag 1 to file
ask question 1
write answer to question 1 to the file
write closing tag 1.
write opening tag 2
ask question 2 .....

If the tags depend on the answer you would have to find some way to temporarily store the answer, in a string, array, etc. example:

Ask question 1.
store answer 1 in an array
analyze answer 1 to choose which tags to use
write opening tag 1 to file
write answer 1 to file
write closing tag 1.
ask question 2....
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is offline I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 03-29-08   #6 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: oklahoma
Posts: 775

Rep: 27 Pooping^fish is acknowledged by some
Unique Rep: 25
Trader Rating: 0
Default

Code:
#include <string>
#include <iostream>
#include <fstream>

using namespace std; 

int main()
{
   
   char filename[51]; //variable to store file name //declares an array of characters to store the file name, 50 chars long. 
  
   cout <<"What do you want the name of your webpage to be? NOT the title!";
   cin  >> filename; //depending on what you want to allow to be as the file name, consider using cin.get?
   
string titlea = ("<title>");
string titleb = ("</title>");
    string title = ("titlea, title, titleb");
   cout <<"What do you want the title to be?";
   cin >> title;
    ofstream the_file; //variable to refer to stream to write to file
   the_file.open(filename);

   system("PAUSE");
   return 0;
}
I tried adding more to include a title, then wrap the title tags around it.
The file still writes, and the file name is written, but my title isnt included at all?
I tried the_file title; and it just gave an error. Shouldnt I use this to write to the file?
__________________
Quote:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: CSS Pwner
CPU
e6400 @ 3.2
Motherboard
p5n-t 780i
Memory
2gb ocz @ 900
Graphics Card
8800gtx flashed to 621/2ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TR u120 Extreme
GPU cooling
stock
OS
Ubuntu, Vista ult. SP1
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 03-29-08   #7 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: oklahoma
Posts: 775

Rep: 27 Pooping^fish is acknowledged by some
Unique Rep: 25
Trader Rating: 0
Default

Bartender-Yes the tags will depend on the question.
Could I use seperate functions for each or would that be too much of a hassle?
__________________
Quote:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: CSS Pwner
CPU
e6400 @ 3.2
Motherboard
p5n-t 780i
Memory
2gb ocz @ 900
Graphics Card
8800gtx flashed to 621/2ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TR u120 Extreme
GPU cooling
stock
OS
Ubuntu, Vista ult. SP1
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 03-29-08   #8 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,275

Rep: 284 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 217
Folding Team Rank: 281
Hardware Reviews: 1
Trader Rating: 3
Default

Quote:
Originally Posted by Pooping^fish View Post
Code:
#include <string>
#include <iostream>
#include <fstream>

using namespace std; 

int main()
{
   
   char filename[51]; //variable to store file name //declares an array of characters to store the file name, 50 chars long. 
  
   cout <<"What do you want the name of your webpage to be? NOT the title!";
   cin  >> filename; //depending on what you want to allow to be as the file name, consider using cin.get?
   
string titlea = ("<title>");
string titleb = ("</title>");
    string title = ("titlea, title, titleb");
   cout <<"What do you want the title to be?";
   cin >> title;
    ofstream the_file; //variable to refer to stream to write to file
   the_file.open(filename);

   system("PAUSE");
   return 0;
}
I tried adding more to include a title, then wrap the title tags around it.
The file still writes, and the file name is written, but my title isnt included at all?
I tried the_file title; and it just gave an error. Shouldnt I use this to write to the file?
You should use the_file since its associated with the ofstream, but you forgot to include the << operator. By saying ofstream the_file you have created an object called the_file that behaves like cout but for files. Just like cout you have to include an operator. For example you can say the_file << "Hi" or the_file.width(4).

Quote:
Originally Posted by Pooping^fish View Post
Bartender-Yes the tags will depend on the question.
Could I use seperate functions for each or would that be too much of a hassle?
If you want you can use a function, for small things like this I consider it a matter of style.
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is offline I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 03-30-08   #9 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: oklahoma
Posts: 775

Rep: 27 Pooping^fish is acknowledged by some
Unique Rep: 25
Trader Rating: 0
Default

Ive tried this.
What you said kindof confused me.
What do I use to have the variable title written to the file?
No matter what I try nothing biut the filename gets written. It completely ignores the input for the title.
__________________
Quote:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: CSS Pwner
CPU
e6400 @ 3.2
Motherboard
p5n-t 780i
Memory
2gb ocz @ 900
Graphics Card
8800gtx flashed to 621/2ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TR u120 Extreme
GPU cooling
stock
OS
Ubuntu, Vista ult. SP1
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 05-10-08   #10 (permalink)
Gen
Programmer
 
Gen's Avatar
 
amd ati

Join Date: Apr 2008
Location: Oklahoma, USA
Posts: 163

Rep: 7 Gen Unknown
Unique Rep: 6
Trader Rating: 0
Default

I'm probaly resurecting a dead horse but is this what you are after?

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	// Create the varible used to store the filename
	char filename[64];

	// Create the vaibles used to wrap a user input title around some tags
	string TitleStartTag = "<title>";
	string TitleEndTag = "</title>";
	string Title;


	// Get the filename from the user
	cout << "Please enter the filename: ";
	cin >> filename;
	cout << endl << endl;

	// Get the title from the user
	cout << "Please enter the title: ";
	cin >> Title;

	// Create the filestream object
	ofstream out_file;
	// Open the file
	out_file.open(filename);
	// Write the title varibles to the file
	out_file << TitleStartTag << Title << TitleEndTag << endl;
	// Always must ensure the file is closed when were done with it
	out_file.close();


	system("PAUSE");
	return 0;
}
Edit: Added comments
__________________
3DMark06: 15393

System: My System
CPU
AMD Phenom 9850 @ 2800 MHz
Motherboard
ASUS M3A32-MVP Deluxe\Wifi
Memory
4x 1024 MB Corsair Dominator PC8500 @ 1066 MHz
Graphics Card
3x Diamond Radeon HD 3870's 512 MB @ 850\1176 MHz
Hard Drive
2x 500 GB WD Caviar in RAID 1 1x 250 GB WD Caviar
Sound Card
Onboard
Power Supply
1000 Watt Ultra X3
Case
Ultra Aluminus ATX Full Tower
CPU cooling
Zalman CNPS9700
GPU cooling
Stock
OS
Windows Vista Ultimate x32 SP1
Monitor
24" Acer P241W

Last edited by Gen : 05-10-08 at 10:27 PM.
Gen is offline   Reply With Quote
Reply



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



All times are GMT -4. The time now is 03:04 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.21156 seconds with 9 queries