Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 03-13-2003, 07:44 AM   #1 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Angry Omg, it's been a while: fstream help pls.

// Read an entire file into a single string
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

void main() {
ifstream in("FillString.cpp");
string s, line;
while(getline(in, line))
s += line + "\n";
cout << s;
}

This program works. But when running in VC++ 6 IDE the console doesn't show the contents of the FillString.cpp file.
BUt when executed by the exe, it does, though I have to insert cin.get() at the end.

Q1:
Why doesn't the output show in the IDE?

Q2:
cin.get() is ugly.
What was the other method to hold the console, only waiting for a button to close? I totally forgot.

Thx in advance,
Val
__________________
Valmont is offline   Reply With Quote
Old 03-13-2003, 08:49 AM   #2 (permalink)
Travis Dane
Code Monkey
 
Travis Dane's Avatar
 
Join Date: Feb 2003
Location: Netherlands
Posts: 89
Travis Dane is on a distinguished road
Send a message via ICQ to Travis Dane
Use "int main(void)" not "void main()",

#1: Do you mean when the program is executed from within
the VC IDE? It works fine here.

#2: You can use "getch();" from the <conio.h> header.

Code:
#include <string> #include <iostream> #include <fstream> #include <conio.h> using namespace std; int main(void) // Use int main! Not void, Reason is the OS needs a return result from the program { ifstream in("FillString.cpp"); string s, line; while(getline(in, line)) s += line + "\n"; cout << s; getch(); // A cleaner method to hold to program until a key is pressed return 0; }
__________________
OpenGL, DirectX
Travis Dane is offline   Reply With Quote
Old 03-13-2003, 10:33 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Nevermind, I moved the file. I forgot.

Usually I use int main() but I was in a "test" hurry

Thx anywayz!
- Val -
__________________
Valmont is offline   Reply With Quote
Old 03-13-2003, 12:20 PM   #4 (permalink)
alpha
Regular Contributor
 
Join Date: Feb 2003
Posts: 120
alpha is on a distinguished road
Send a message via AIM to alpha
how is cin.get() ugly, it is standard, vs. getch() which isn't standard.
__________________
alpha is offline   Reply With Quote
Old 03-13-2003, 12:55 PM   #5 (permalink)
Travis Dane
Code Monkey
 
Travis Dane's Avatar
 
Join Date: Feb 2003
Location: Netherlands
Posts: 89
Travis Dane is on a distinguished road
Send a message via ICQ to Travis Dane
If you're going to write a program completely by the standard
you wouldn't come further than a Console Hello World program,
Besides, Must compilers have getch(), His does as well.
__________________
OpenGL, DirectX
Travis Dane is offline   Reply With Quote
Old 03-13-2003, 03:10 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
I was referring to methods like: system("pause").
There are many more options. But using this isn't a good idea anyway.
And for me it is even not an option (preparing for Qt2).

Like I said, I'm a lil rusty but that is going to change in a week or so .
__________________
Valmont is offline   Reply With Quote
Old 03-13-2003, 03:32 PM   #7 (permalink)
alpha
Regular Contributor
 
Join Date: Feb 2003
Posts: 120
alpha is on a distinguished road
Send a message via AIM to alpha
Quote:
Originally posted by Travis Dane
If you're going to write a program completely by the standard
you wouldn't come further than a Console Hello World program,
Besides, Must compilers have getch(), His does as well.
well, point taken. I agree, I have used non-standard code myself.
__________________
alpha is offline   Reply With Quote
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Take a look here pls Eyelfixit Program Design and Methods 8 02-01-2007 10:19 AM
Pls help me interpret this script. infinite_root HTML, XML, Javascript, AJAX 1 09-06-2004 01:53 AM
OMG sde mate!!! Valmont Lounge 3 04-19-2004 05:42 AM


All times are GMT -8. The time now is 05:03 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle