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
Old 02-19-2003, 05:50 PM   #1 (permalink)
carrja99
Registered User
 
Join Date: Feb 2003
Posts: 34
carrja99 is on a distinguished road
File Input/Output

O.K., this week let's start with something simple and easy. Assuming you know your simple screen input/output via cout and cin, you should have no problem with file input/output.

First, let's include the header file for file i/o:
Code:
#include <fstream.h>
Now, lets look at the two important types of fstream we will use: ifstream (InFileStream) and ofstream(OutFileStream). You will use these to declare the internal filenames for the program. The file you are recieving input from will be designated by ifstream, and the file you are outputing to will be designated by ofstream.

Code:
int main()
  {
  ifstream fin; 
  ofstream fout;

  return 0;
  }
Here I will use the common fin and fout so you can feel comfortable and see the similarity between them and cin and cout. You can use any variable names, however, and I commonly use read and write myself.

Now that we have established the internal filenames, we need to specify external filenames (what they are called on the system) to connect them to.

Before we start, make a txt file named "input.txt" and enter the numbers on the first line like this:

12 32 43 32 11 10

Now, let's connect fin and fout to appropriate files.
Code:
int main()
  {
  int x;
  ifstream fin; 
  ofstream fout;
  
  fin.open("input.txt");
    
  if( fin.fail() )
    {
     cout << "File not found! Now exiting!" << endl;
     exit(1);
    }

  fout.open("output.txt");
   
  if( fout.fail() )
   {
     cout << "File error!" << endl;
     exit(1);
    }
   
  

  return 0;
  }
Now, notice the if statements. The fin.fail() usually is true when the input file does not exist, so we need to exit if it is true, otherwise we will have some nasty problems. It may also evaluate to true under other circumstances, which fout.fail() may evaluate to true if your hard drive is tightly packed to it's limits!

Now, let's do something already!!! Add the following after the fout if statement and compile it. Compare what you see on the screen and what you now have in the output file.
Code:
fin >> x;
cout << x << endl;
fout << x;
Repeate these three lines 6 times, or even put them in a for loop like the following:
Code:
for (int i = 0; i < 6; i++)
   {
   fin >> x;
   cout << x << endl;
   fout << x;
   }
You've just coppied the contents of the file! Now try using fout as you would cout. For example:
Code:
fout << "This is another lame Hello World output." <<endl;
Try using other functions you are familiar with in the cout/cin operations, such as getline and such.

Now, before we go any further, lets not forget the very important function close. It's always good technique to close files once you are finished with them, so you should include either of the following either when you are done with a file (in the case where you use multiple files) or at the end right before return 0;

Code:
fin.close();
fout.close();
Now, let's look at some Input/Output functions we can use. consider the following to replace our earlier for-loop:

Code:
while ( ! fin.eof() )
  {
  fin >> x;
  cout << x << " ";
  fout << x << " ";
  }
You may notice that the last number from the file is taken twice. Why? This is one problem for you to try to solve.

Well, that wraps it up for tonight. My next posting will be tomarrow to continue this out. For practice, take what we have done so far and try implementing different input/output functions you know for cout and cin, and try using different types of data (ints, char, strings, etc.).

Also, use a user defined filename instead. For example, use a string for the user to enter, then use that string as the filename.

Code:
char filename[16];
cin >> filename;
fin.open(filename);
And, if you know how to sort, try sorting the numbers from the input file and storing them sorted in the output file.

And, for those interested, here is a link to some of the member functions fstream uses if you want to explore some other functions I may not touch.

http://www.cplusplus.com/ref/iostream/fstream/

If you have any questions or problems, post it in this thread and I will respond before my next post.
carrja99 is offline   Reply With Quote
Old 02-25-2003, 10:16 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
oh yeah! i finally got a chance to do this. I've never written to a text file before and I am feeling very enlightened right now.

Excellent Tutorial.

If it is ok with you, I will copy this over into the C++ tutorials.

Thanks!
sde is offline   Reply With Quote
Old 02-26-2003, 02:20 PM   #3 (permalink)
Pooper
Registered User
 
Pooper's Avatar
 
Join Date: Feb 2003
Location: Memphis
Posts: 1
Pooper is on a distinguished road
Thanks

This site is just what I needed! Very nice web design also...I plan to come here often.

Thanks to all the admins for taking the time to do this!:rock:
Pooper is offline   Reply With Quote
Old 02-27-2003, 08:29 AM   #4 (permalink)
carrja99
Registered User
 
Join Date: Feb 2003
Posts: 34
carrja99 is on a distinguished road
No problem. Sorry for not getting something in since my last post, a certain situation occured (you can see my post on this here), but I am now back and I'll hopefully be able to follow this one up tonight or tomarrow, and start on the next "lesson". I am thinking of doing some introduction to elementary data structures (arrays, linked lists, etc) and then perhaps some other things too. Let me know if there is any concept you are curious about.
carrja99 is offline   Reply With Quote
Old 02-27-2003, 01:27 PM   #5 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
don't forget what I consider 2 very important file functions and those are
get( char )
put( char )

allows you to pull every char from the file including the end of line characters. These come in very handy when writing a scanner.
palin is offline   Reply With Quote
Old 03-11-2003, 10:34 AM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Good work carr
Valmont is offline   Reply With Quote
Reply

Bookmarks

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing C++ file structure in C# IAmWeasel MS Technologies ( ASP, VB, C#, .NET ) 1 07-08-2004 08:38 AM


All times are GMT -8. The time now is 01:57 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting