Basic File I/O problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2004
Posts: 1
Reputation: Jeannie is an unknown quantity at this point 
Solved Threads: 0
Jeannie Jeannie is offline Offline
Newbie Poster

Basic File I/O problem

 
0
  #1
Feb 16th, 2004
I'm pretty new at C++ and I have a problem with a program that opens a file with a list of numbers and finds the median. My problem is, no matter what numbers are in the file, it always says the median is 1. I've been able to tell it what position the median is in, but I can't get it to return the number that's in that position. How can I get it to reopen the file, find the median number, and spit it back to me?

Attached is what I've done so far.
Here is what the data file is uses looks like:
file2.dat
1
2
3
4
5
6
7
8


I know the simplicity of it is laughable, but bear with me. I'm a newbie.
Attached Files
File Type: cpp median2.cpp (840 Bytes, 21 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member

Re: Basic File I/O problem

 
0
  #2
Feb 18th, 2004
Hi, don't worry, it's not laughable, we've all been equally as stumped with problems equally as simple once upon a time.

Having calculated which position the median is at, you then reopen the file, read in the first value from the file, and output this as the median. The first value in the file is 1, so the program always outputs this as the median, even though it's not the value in the median position.

You need to read in values until you've reached the one at the median position, rather than just reading in the first value.

Hope that helps.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1
Reputation: Big_D is an unknown quantity at this point 
Solved Threads: 0
Big_D Big_D is offline Offline
Newbie Poster

Re: Basic File I/O problem

 
0
  #3
Jun 19th, 2005
how do u save an array into a file?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Basic File I/O problem

 
0
  #4
Jun 19th, 2005
Dude, this thread is well over a year old. Next time, if you have a new question, start a new thread.

To save an array to file, just open the file, then loop over the array and write each element in turn:
  1. #include <fstream>
  2.  
  3. int main()
  4. {
  5. std::ofstream out("somefile");
  6. int array[] = {0,1,2,3,4,5,6,7,8,9};
  7.  
  8. if (out.is_open()) {
  9. for (int i = 0; i < 10; i++)
  10. out << array[i];
  11. }
  12. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC