|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
C++ Type text editor not working..Code inside!
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||||
|
Security Sleuth
|
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;
}
Currently the first fstream line is highlighted..Any ideas? Thankyou guys! +REP for any help or advice!
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
Last edited by Pooping^fish : 11-13-07 at 12:23 AM. |
||||||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||
|
is Stig's seasonal cousin
|
can't you just set "file" equal to "text"?
__________________
480 GTX Club BlockHeads: the thread OCN Headphones Club4.0ghz Cpu-Z Validated ![]() ESI Juli@ -> Soloz Audio Reference RCA -> TCA Gizmo -> Onix SL-1 wire -> AV123 ELT525M's
|
||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||||
|
Security Sleuth
|
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:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
Programmer
|
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()); Code:
getline(cin, text); 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;
}
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
Last edited by kdbolt70 : 11-13-07 at 12:56 AM. |
|||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||||
|
Security Sleuth
|
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:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#6 (permalink) | ||||||||||||||
|
Programmer
|
Quote:
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.
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
Last edited by kdbolt70 : 11-13-07 at 01:08 AM. |
||||||||||||||
|
|
|
|
#7 (permalink) | ||||||||||||||
|
Security Sleuth
|
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:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#8 (permalink) | ||||||||||||||
|
Photography nut
![]() |
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:
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
|
||||||||||||||
|
|
|
|
#9 (permalink) | ||||||||||||||
|
Programmer
|
Quote:
Also 'fish, the line Code:
system("PAUSE");
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
Last edited by kdbolt70 : 11-13-07 at 12:00 PM. |
||||||||||||||
|
|
|
|
#10 (permalink) | ||||||||||||
|
Programmer
|
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!
|
||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|