GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Jan-2004, 22:47
pin215 pin215 is offline
New Member
 
Join Date: Jan 2004
Posts: 2
pin215 is on a distinguished road

opening files and displaying text


Could someone help me with some C++ code. I need to open a file, display the contents and do some calculations. We need to use functions. i am a little confused on the opening of files. Many thanks
  #2  
Old 28-Jan-2004, 11:00
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi pin. I was hoping that someone else might answer because my familiarity with C++ is pretty slim. You should look through this forum though, because there are some good C++ code snippets here as well as some good tutorial locations.

For instance, this thread links to a particularly good page for file handling under c++.

Here is some sample code of C++ for reading & writing files:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;


void read_file(char filename[])
{
	ifstream 	file(filename);
	char		line[80];

	while(!file.eof()){
		file.getline(line,80);
		cout << line << "\n";
	}
	file.close();
}

void write_file(char filename[])
{
	ofstream	file(filename);
	char		line[80];

	do{
		cout << "\nEnter text: ";
		cin.getline(line,80);
		if(line[0])
			file << line << "\n";
	}while(line[0]);

	file.close();
}

int main(){

	char filename[80];

	cout << "This is a C++ program\n";
	cout << "Please enter a filename to read: ";
	cin.getline(filename,80);
	read_file(filename);

	cout << "Please enter a filename to write to: ";
	cin.getline(filename,80);
	write_file(filename);

	return 0;
}

Listed below are several points about this code. I am unsure of what level of coding experience you have, so this may seem pretty basic.
  • C/C++ always contain a main function. In C++ it must be defined as having a return value of int. I always put this function as the last function in my file, because it will call the other functions from here. You want those defined before your main function calls them.
  • It is a good idea to define your functions with a return value, even if it is void.
  • The compiler directives are located at the top of the file. In this case, we are telling the compiler to include the iostream(console input/output) and fstream (file input/output) headers for these libraries.
  • Get used to using: using namespace std; in all of your C++ routines. It allows/defines things like cin and cout.

Anyway, if you get some of your program done, you can post it here and let the really good C++ guys critique it.

Good luck...
Last edited by dsmith : 20-Feb-2004 at 09:56.
  #3  
Old 28-Jan-2004, 22:36
pin215 pin215 is offline
New Member
 
Join Date: Jan 2004
Posts: 2
pin215 is on a distinguished road

many thanks


I will post the part of the program soon. Many thanks for the start. I thought that I was a little vague. Talk to you soon.
  #4  
Old 20-Feb-2004, 00:57
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
You actually can use void main() instead of int main(). Some compilers may not like it, though.
  #5  
Old 20-Feb-2004, 04:38
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,270
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by aaroncohn
You actually can use void main() instead of int main(). Some compilers may not like it, though.
According to the standard, you should NOT use void main(). And there has been a test to show that is in fact can crash your program or system. So always use int main()

And since some compilers may not like it, why should you use something that doesn't work sometime. Use what works all the time.
  #6  
Old 20-Feb-2004, 09:35
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Well, I've been using it with every console app I've ever done. It's never caused me any problems, and the only reason I knew it wasn't standard in the first place was because someone told me so. I've never heard of anyone actually having problems with it.
  #7  
Old 20-Feb-2004, 14:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,270
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I have. So go ahead and use it if you want, but do NOT recommend it to others. We want to teach good practices here.
  #8  
Old 21-Feb-2004, 22:27
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,270
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
It has come to my attention that my previous post can be interpreted as harsh. I apologize for that, I did not mean it as a rebuke nor with malice. All I meant was the point under discussion in rare cases does cause problems. In most cases and on most compilers it seems fine, so use it if you want to, but just know it's not standard.

Again, please accept my apology. I'll watch it...
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying Code Using Notepad BobbyDouglas Web Design Forum 1 16-Aug-2003 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:57.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.