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 11-13-07   #1 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: oklahoma
Posts: 907

Rep: 44 Pooping^fish is acknowledged by some
Unique Rep: 40
Trader Rating: 2
Default C++ Type text editor not working..Code inside!

Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
   cout<<"Enter File Name: ";
   string file_name;
   cin>>file_name;
   cout<<"Enter text to be written inside the file: ";
   string text;
   cin>>text;
   fstream file(file_name, ios::out);
   file<<text<<endl;
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
Its supposed to have them enter the name of the file, then it asks what they want inside the file.
Currently the first fstream line is highlighted..Any ideas?

Thankyou guys! +REP for any help or advice!
__________________
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
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
Arch Linux, XP for games
Monitor
24" Westy

Last edited by Pooping^fish : 11-13-07 at 12:23 AM.
Pooping^fish is offline   Reply With Quote
Old 11-13-07   #2 (permalink)
is Stig's seasonal cousin
 
tehpwnerofn00bs's Avatar
 
intel nvidia

Join Date: Aug 2007
Location: N.W. Washington
Posts: 3,474

Rep: 300 tehpwnerofn00bs is a proven membertehpwnerofn00bs is a proven membertehpwnerofn00bs is a proven membertehpwnerofn00bs is a proven member
Unique Rep: 233
Folding Team Rank: 197
Trader Rating: 8
Default

can't you just set "file" equal to "text"?
__________________


480 GTX Club BlockHeads: the thread OCN Headphones Club
4.0ghz Cpu-Z Validated
ESI Juli@ -> Soloz Audio Reference RCA -> TCA Gizmo -> Onix SL-1 wire -> AV123 ELT525M's
Quote:
Originally Posted by Syrillian View Post
The persistance of a human may never win against the persistance of time, but the fruits of the labors are wonderous to behold, even in our brevity.

System: Dark Helmet
CPU
Q6600 G0 @ 4.0ghz, 1.61v
Motherboard
DFI LP UT P35-T2R
Memory
2x2gb G.Skill PC8000
Graphics Card
Evga 8800GTX @ 648/999
Hard Drive
WD Raptor 150gb, Samsung 500gb, Samsung 1tb
Sound Card
ESI Juli@
Power Supply
Silverstone OP850
Case
Lian Li PC-G75
CPU cooling
D-tek Fuzion
OS
Windows 7 x86, Vista Ultimate x64
Monitor
Samsung 206bw
tehpwnerofn00bs is online now I fold for Overclock.net Overclocked Account   Reply With Quote
Old 11-13-07   #3 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: oklahoma
Posts: 907

Rep: 44 Pooping^fish is acknowledged by some
Unique Rep: 40
Trader Rating: 2
Default

What the first fstream line there is suposed to do is get the specified name from the user and use that to title or name the file, the next with "text" adds the text the user enters into the file to be saved. So I dont think thatd work..=(
__________________
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
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
Arch Linux, XP for games
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 11-13-07   #4 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,119

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 284
Trader Rating: 1
Default

One thing you need to do is set up the ofstream properly, and you need to pass the filename as a c-string.

Code:
ofstream file;
file.open(file_name.c_str());
Also, cin >> text will delimit at the first space, so if you wanted to input "Happy Birthday", it will only write "Happy" to file. You can try using the


Code:
getline(cin, text);
function.

There may be a better way of doing this, but I just threw this together real quick so you can see what I did. Notice calling the getline twice, because it needs to flush the input buffer:

Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
   cout<<"Enter File Name: ";
   string file_name;
   cin>>file_name;
   ofstream file;
   file.open(file_name.c_str());//opens file with c-string cast
   cout<<"Enter text to be written inside the file: ";
   string text;
   getline(cin, text); //flushes newline from previous read
   getline(cin, text);
   
   file<<text<<endl;
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
Edit: if you want to read more than just one line to the file, you may want to use a while loop to keep reading and writing.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"

Last edited by kdbolt70 : 11-13-07 at 12:56 AM.
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 11-13-07   #5 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: oklahoma
Posts: 907

Rep: 44 Pooping^fish is acknowledged by some
Unique Rep: 40
Trader Rating: 2
Default

I messed with it some and it compiled, got me to enter name, then when it asked for text it added push any key to close right after..
Can you put it in and give me the full code example so I know what this should look like or where the lines actually go or what they replace? Still very new.
__________________
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
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
Arch Linux, XP for games
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 11-13-07   #6 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,119

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 284
Trader Rating: 1
Default

Quote:
Originally Posted by Pooping^fish View Post
I messed with it some and it compiled, got me to enter name, then when it asked for text it added push any key to close right after..
Can you put it in and give me the full code example so I know what this should look like or where the lines actually go or what they replace? Still very new.
Just did that as an edit, hope that helps. Its a bit confusing with the psuedo-cast to a c string, and the two getline calls. The reason it takes a c-string rather than a c++ string is because the string class is not implemented in C, so trying to open a file with a string filename would make no sense.

The reason it is not recognizing the getline is because the "enter" (aka newline '\n') you put into the input buffer is not read on the initial cin when you get filename. Every time you input something and hit enter, a newline character gets put into the input stream. If a function delimits on that newline, which is what cin does, it will leave the newline in the input stream. The next thing to read from the input will suddenly get a newline right away, because it is the first thing there. So it is sitting in the input buffer, and when you call getline from the cin stream, it writes that newline to text and thinks it has done its job. so doing it twice clears that newline you input earlier, and reads the next stuff from the input buffer.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"

Last edited by kdbolt70 : 11-13-07 at 01:08 AM.
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 11-13-07   #7 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: oklahoma
Posts: 907

Rep: 44 Pooping^fish is acknowledged by some
Unique Rep: 40
Trader Rating: 2
Default

Thanks man, it worked! Trying to look at it to see what alls been done here.
Most of what you said went right over my head though. But I'll learn!
__________________
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
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
Arch Linux, XP for games
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 11-13-07   #8 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,484

FAQs Submitted: 7
Folding Team Rank: 451
Trader Rating: 0
Default

Cool I learned two new things too! (*crawls back to java wishing he knew c++ better*)
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn

Try out the latest Programming Challenge
Quote:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

CPU-Z Validation
@ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15


Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64 & Slackware 12.1
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 11-13-07   #9 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,119

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 284
Trader Rating: 1
Default

Quote:
Originally Posted by dangerousHobo View Post
Cool I learned two new things too! (*crawls back to java wishing he knew c++ better*)
Heh, I'm glad! Don't you worry. I'll be calling on you in a few days when I have to write my first interactive Java GUI using swing **shudders and runs back to C++**


Also 'fish, the line

Code:
system("PAUSE");
tells me that you are using the Dev-C++ Editor by Bloodshed, am I correct? You may want to remove it from your final program, as it is technically not needed. The geniuses that made Dev-C++ (I use the term loosely) decided if main returns, they should automatically close the console, which is somewhat counterintuitive. Just letting you know.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"

Last edited by kdbolt70 : 11-13-07 at 12:00 PM.
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 11-13-07   #10 (permalink)
Programmer
 
intel nvidia

Join Date: Jan 2007
Posts: 101

Rep: 8 Kirmie Unknown
Unique Rep: 6
Folding Team Rank: 778
Trader Rating: 0
Default

The reason that the console applications close immediately after the program finishes main is that if you run this program from a command line the program immediately goes back to the command line. It would be one big pain if you had to explicitly tell your program to exit and go back to a command line since most programs run in the console typically don't require much, if any, user input.
__________________
From American Dad:
I touched her hand... her hand touched her boob. By the transitive property, I got some boob! Algebra's awesome!

System: My giant
CPU
E6600
Motherboard
Asus P5n32-E 680i
Memory
2x1GB Corsair XMS2 PC6400
Graphics Card
GeFroce 7950 GT 256MB
Hard Drive
WDJS SATA-II 160GB + WD80GB + WDAAKS Raid0 320GB
Power Supply
Apevia DarkSide 600W
Case
NZXT Zero
CPU cooling
Stock
GPU cooling
Stock
OS
XP Media Center
Monitor
19" LCD
Kirmie is offline I fold for Overclock.net   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 02:33 AM.


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.45938 seconds with 8 queries