GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / 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 04-Apr-2005, 13:04
raptorhawk raptorhawk is offline
New Member
 
Join Date: Mar 2005
Posts: 25
raptorhawk is on a distinguished road

Reading and Writing to a text file


Currently, I am in high school and doing an independent study of C++ programming. I am the only one in my high school (teachers included) who has any idea of how to program (and I’ve only been programming for 2 months).

There is a programming contest coming up hosted by the local university. I have been asked to lead a team in the compatition.

Here is a sample question that they have posted on their http://contests.unl.edu/?page=Solutions.
"Read in a list of integers from a file. Output to another file the number of numbers on the list."
They have also provided the solution.

CPP / C++ / C Code:
#include <iostream>           // Standard in/out header
#include <fstream>            // File io header

using namespace std;

int main()
{
    ifstream fin;               // Create a input file stream
    ofstream fout;              // Create a output file stream
    int counter=0, input;       // A counter and a dummy input

    fin.open("p100in.txt");     // Open the input file

    while(!fin.eof())           // Loop until we find the end of file
    {
        fin >> input;           // Read in the dummy input
        counter++;              // Increase the counter
    }

    fin.close();                // Close the input file

    fout.open("p100out.txt");   // Open the output file
    fout << counter << endl;    // Write the number of numbers
    fout.close();               // Close the output file

    return 0;                   // We're done
}
In my studies I have not covered how to do simple file I/O. I've tried to replicate the solution but I can't get it to work. Could someone tell me how to do simple file I/O?
  #2  
Old 04-Apr-2005, 13:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by raptorhawk
Currently, I am in high school and doing an independent study of C++ programming. I am the only one in my high school (teachers included) who has any idea of how to program (and I’ve only been programming for 2 months).

There is a programming contest coming up hosted by the local university. I have been asked to lead a team in the compatition.

Here is a sample question that they have posted on their web page.
"Read in a list of integers from a file. Output to another file the number of numbers on the list."
They have also provided the solution.

In my studies I have not covered how to do simple file I/O. I've tried to replicate the solution but I can't get it to work. Could someone tell me how to do simple file I/O?

Well, if you compile their program and you create a file (with a text editor) named p100in.txt, and you put 10 integers in the file, and you put the txt file in the same directory as the executable (the compiler output), and you run the program, then you will see a new file in the same directory. The new file will be named p100out.txt, and is supposed to contain an integer that is equal to the number of integers that are in p100in.txt.

Unfortunately the "solution" has a bug (it says there are 11 integers in the file).

Use the following as p100in.txt (just copy and past from this post into your text editor). This is their example input file (has 10 integers --- count 'em).

Code:
1 54 89 101 32 3 49 23 3 2

Here is what you will see in p100out.txt:

Code:
11

Now, there have been several threads on this site about detecting end of file and the "solution" that they published is a very good example of how not to do it.

You can search this site or others on the web to see the right way (actually more than one right way) to do it.

Find an on-line tutorial or a book about C++ file I/O. If you have any specific questions, just ask. If you have some code that doesn't work the way you think it should: post the code. Tell us what you expected to get. Tell us what you got. Tell us what you don't understand about what you got. Ask specific questions.

Regards,

Dave
  #3  
Old 04-Apr-2005, 14:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by raptorhawk

There is a programming contest coming up hosted by the local university.

Here is a sample question that they have posted on their web page.
"Read in a list of integers from a file. Output to another file the number of numbers on the list."
They have also provided the solution.

Things I don't like about their "solution":

1. They don't test to see if the input or output files were actually opened successfully.

2. They don't test to see if the program was able to read an int every time it tried.

3. They don't test the end-of-file condition correctly. The result is that they get the wrong answer from their own example program.



Things I like about the "solution":

It gives C++ programmers some things to look up in textbooks or on-line tutorials, or whatever, to learn about file I/O:

#include <fstream>

ifstream thingies
ofstream thinties
open(), eof(), close() member functions, and >> and << overloaded operators for ifstream and ofstream thingies.

Regards,

Dave
  #4  
Old 04-Apr-2005, 17:32
raptorhawk raptorhawk is offline
New Member
 
Join Date: Mar 2005
Posts: 25
raptorhawk is on a distinguished road
I'll try that at school tomorrow.

Ive been using Borland C++ BuilderX to write all of my files. Ive been writing, compiling, and running code from inside the program. I doubt this is the way I sould be doing this. Does any one use Borland and could help out? I dont realy know how to save my program as an .exe. As I said earlyer i'm new to programming and there isnt realy anyone arround that can help. Thanks alot.
  #5  
Old 05-Apr-2005, 02:10
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
Your program only makes it into binary format (.exe) after it has been compiled and linked. Are you using a command line compiler, or is it an interactive developer environment with a text editor and a line debugger? If so, there should be an option to "Build" or "Run" your program. When you click on such a button, it will attempt to compile your code. If unsuccessful, it'll display a list of errors that it found so that you can fix them, otherwise, it'll build the .exe for you and run your program. If you need help with Borland BuilderX, just google your question. I use Borland Builder 6 for most of my development, so just let me know if you have any questions about compiling your code with Borland.
__________________
-Aaron
  #6  
Old 05-Apr-2005, 13:16
raptorhawk raptorhawk is offline
New Member
 
Join Date: Mar 2005
Posts: 25
raptorhawk is on a distinguished road
Thanks so much. Finaly got it to work. The problem was that I've been writing, compiling, and running my code from inside Borland. Now that I know where the .exe is things are working better.

Dave, in your first response you said that p100out.txt would read 11. Mine reads 10.
  #7  
Old 05-Apr-2005, 13:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by raptorhawk

Dave, in your first response you said that p100out.txt would read 11. Mine reads 10.

What is in your input file? Paste it exactly into your post.

Regards,

Dave
  #8  
Old 06-Apr-2005, 13:13
raptorhawk raptorhawk is offline
New Member
 
Join Date: Mar 2005
Posts: 25
raptorhawk is on a distinguished road
Quote:
Originally Posted by davekw7x
Use the following as p100in.txt (just copy and past from this post into your text editor). This is their example input file (has 10 integers --- count 'em).

Code:
1 54 89 101 32 3 49 23 3 2

Here is what you will see in p100out.txt:

Code:
11


Thats exactly what I put in and I got 10.

Also, im now trying to use Visual Studio. When I want to start a new project do I create a "Win32 Console Project"? All I want to do is create an .exe, nothing fancy.
  #9  
Old 06-Apr-2005, 13:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by raptorhawk
Thats exactly what I put in and I got 10.

Also, im now trying to use Visual Studio. When I want to start a new project do I create a "Win32 Console Project"? All I want to do is create an .exe, nothing fancy.

Second thing first: A Windows Console Project has a main program named main(), and can be any kind of C or C++ program that you want it to be (you can select C or C++).

Now: If your program gives creates a file that contains "10" with the input that I showed, then I would really appreciate it if you could do me a big favor:
Use the following as your program file and tell me what you see on the console when you run it. I haven't changed any of the program steps except to add some output statements that will appear on the console. Some of the stuff will scroll off of the console screen; don't worry about that --- just show me what appears in the last few lines. Thanks.

CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream fin;
    ofstream fout;
    int input;
    int counter=0;

    fin.open("p100in.txt");

    while(!fin.eof()) {
      fin >> input;
      cout << "input = " << input << " with counter = " << counter << endl;
      if (!fin) {
        cout << "fin failed with counter = "  << counter << endl;
      }
      counter++;
      cout << "After incrementing, counter = " << counter << endl << endl;
    }
    fin.close();
    cout << "After loop, counter = " << counter << endl;
    fout.open("p100out.txt");
    fout << counter << endl;
    fout.close();
    cout << "Press 'Enter': ";
    cin.get(); 
    return 0;
}

Regards,

Dave
Last edited by davekw7x : 06-Apr-2005 at 14:11.
  #10  
Old 07-Apr-2005, 13:04
raptorhawk raptorhawk is offline
New Member
 
Join Date: Mar 2005
Posts: 25
raptorhawk is on a distinguished road
This is what I get.

Code:
input = 1 with counter = 0 After incrementing, counter = 1 input = 54 with counter = 1 After incrementing, counter = 2 ........ (Im going to skip the middle readings since they are the same thing)..... input = 2 with counter = 9 After incrementing, counter = 10 After loop, counter = 10 Press 'Enter':

I kind of just realized this, there were only 10 numbers in the input file that you gave me, not 11.
 

Recent GIDBlogFirst week of IA training 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
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Reading and writing binary files in certain format Dream86 CPP / C++ Forum 10 06-Aug-2004 10:38
reading and writing to a file in C++ mgp6q CPP / C++ Forum 17 02-Mar-2004 12:42
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07

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

All times are GMT -6. The time now is 22:47.


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