![]() |
|
#1
|
|||
|
|||
Reading and Writing to a text fileCurrently, 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:
|
|||
|
#2
|
|||
|
|||
|
Quote:
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:
Here is what you will see in p100out.txt: Code:
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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
What is in your input file? Paste it exactly into your post. Regards, Dave |
|
#8
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
Quote:
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:
Regards, Dave Last edited by davekw7x : 06-Apr-2005 at 14:11.
|
|
#10
|
|||
|
|||
|
This is what I get.
Code:
I kind of just realized this, there were only 10 numbers in the input file that you gave me, not 11. |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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 | C++ Forum | 10 | 06-Aug-2004 10:38 |
| reading and writing to a file in C++ | mgp6q | 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