fstreams

This is a discussion on fstreams within the C++ Programming forums, part of the General Programming Boards category; Can someone help me or give me a hint of how to do this problem? I am supposed to have ...

  1. #1
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32

    Question fstreams

    Can someone help me or give me a hint of how to do this problem?
    I am supposed to have two files of sorted numbers, for example:

    "Section52.cpp" (name of file)
    1
    4
    6
    8
    12
    15
    17

    "Section53.cpp" (name of second file)
    2
    5
    7
    10
    11
    16
    19
    21
    24
    25

    So I am supposed to create a third file that has all of these numbers SORTED from both files in it.
    I am not supposed to use vectors or arrays. So I don't really know a lot about ifstream and ofstream except that I am supposed to use both of these to solve this problem.
    This is what I have so far:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	ifstream FUN1, FUN2;
    	FUN1.open("Section52.cpp");
    	FUN2.open("Section53.cpp");
    	ofstream RUN;
    	RUN.open("Section54.cpp");
    	int aNumber, anotherNumber;
    	int count = 0;
    	int count2 = 0;
    	while (FUN1 >> aNumber)
    	{
    		count++;
    		RUN << aNumber;
    	}
    	while (FUN2 >> anotherNumber)
    	{
    		count2++;
    		RUN << anotherNumber;
    	}
    	
    	int theNumber;
    	if (count >= count2)
    	{
    		theNumber = count;
    	}
    	else
    	{
    		theNumber = count2;
    	}
    and now I am stumped!!!!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You've got to come up with the algorithm to solve the problem. That might be the most important part of programming. Based on your current code it looks like your syntax and coding abilities are fine, you just need to figure out the algorithm to solve this problem.

    Here's some hints: Since you aren't supposed to use vector or an array, chances are that you aren't supposed to store all the values from either input file. That means that you will have to be reading from both files at the same time in some fashion rather than one after the other as you have now. So think about how you can read both files at the same time and only write the smallest value to the output file. You'll also have to make sure that you only read the next value from the input file that had the value you just output.

  3. #3
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32

    Wink

    Okay thanks! I am not really sure about how to input from two files at once, but I am going to mess around with it for awhile and I'm sure I will figure it out. Thanks for the help!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    - get a pack of cards
    - shuffle the deck
    - take 10 cards, sort them into order (this is your first file)
    - take another 10 cards, sort them into order (this is your second file).

    Now, taking 1 card at a time from each pile of 10 (to start with), create a sorted pile of 20 cards.


    If you want to know the detail, it's called a merge sort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32
    I don't understand that exactly.
    Here is what I have updated and this still is not working... of course.
    I am going to do a forum search for merge sort.
    We covered it a little in class this last semester, but I feel like we should have gone over it more.
    I am just doing a little summer practice and am getting myself ........ed off, lol.
    But I guess this it is going to be helpful next semester. Thanks!
    Oh yeah, so here is my code
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	ifstream FUN1, FUN2;
    	FUN1.open("Section52.cpp");
    	FUN2.open("Section53.cpp");
    	ofstream RUN;
    	RUN.open("Section54.cpp");
    	int aNumber, anotherNumber;
    	int count1 = 0;
    	int count2 = 0;
    	while (FUN1 >> aNumber)
    	{
    		count1++;
    	}
    	while (FUN2 >> anotherNumber)
    	{
    		count2++;
    	}
    	int comparison;
    	if (count1 >= count2)
    	{
    		comparison = count1;
    	}
    	else
    	{
    		comparison = count2;
    	}
    	FUN1.close();
    	FUN2.close();
    	FUN1.clear();
    	FUN2.clear();
    	FUN1.open("Section52.cpp");
    	FUN2.open("Section53.cpp");
    
    	while (FUN1 >> aNumber && FUN2 >> anotherNumber)
    	{
    		if (aNumber <= anotherNumber)
    		{	
    				RUN << aNumber << endl << anotherNumber << endl;
    		}
    		else
    		{
    			RUN << anotherNumber << endl << aNumber << endl;	
    		}
    	}
    
    
    	FUN1.close();
    	FUN2.close();
    	RUN.close();
    	return 0;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't care about the count of how many numbers are in the file; you only care about the number you just read in from the file. Compare the "top of each deck", and decide which number to output.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    If you want to know the detail, it's called a merge sort.
    It's just the "merge" part of merge sort, not the "sort" part .

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Please, please, please don't call your data files .cpp, ok? They are not C++ source files, so should not be named as such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    1. "Prime the pump" by reading in a single value from both files.
    2. Loop (if the read from both files was good)
      • If value from 1st file is less, write that value and read another value from 1st file.
      • Otherwise, write value from 2nd file and then read another value from the 2nd file.
    3. Loop, reading from the remaining file (which will still have some values in it) and write those values to the output file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> I am not really sure about how to input from two files at once

    You're getting closer. However your current attempt:
    Code:
    while (FUN1 >> aNumber && FUN2 >> anotherNumber)
    has a problem: "You'll also have to make sure that you only read the next value from the input file that had the value you just output."

    The solution is to not read the two numbers in the control of the while loop. Instead, use something else to control the while loop (perhaps a separate variable). Then inside the loop read from whichever file you need to read to continue your algorithm.

    Make sure to check the result of the read (perhaps by putting the read into an if control) to make sure it succeeds before using that value. Your current does this by putting the reads into the while loop control, but if you follow my suggestion and take it out then you'll have to check the return value another way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fstreams
    By Amyaayaa in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 02:08 PM
  2. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  3. how do I make fstream's >> work for my class?
    By MathFan in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2005, 12:01 PM
  4. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM
  5. trying to use fstreams... specifically input.seekg
    By sarahbee in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2002, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21