sequential access files?

This is a discussion on sequential access files? within the C++ Programming forums, part of the General Programming Boards category; how do create an outfile that will contain only the even numbers from the infile txt heres the infile numbers ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20

    sequential access files?

    how do create an outfile that will contain only the even numbers from the infile txt

    heres the infile numbers

    40
    13
    25
    58
    55
    12
    20
    9
    3
    6
    8
    12

    the outfile can only contain the even numbers

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    1. Read the infile
    2. Store the even numbers
    3. Create the outfile
    4. Write the even numbers

    Please show the code you have attempted so far.
    Woop?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20
    anyone???

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    O_o

    I understand your confusion. I'll explain. With "show the code you have attempted" prog-bman is asking you to show the code you have attempted.

    You'll be surprised by how readily the regulars here will help you if you show a willingness to help yourself.

    Soma

  5. #5
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20
    Code:
    		while (infile.eof()== false)
    		{
    			infile >> number;
    			infile >> evenNumber;
    			if (num%2 == 0)
    		
    			
    			outfile << evenNum << endl;
    		}

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    o_O

    A complete example if you please.

    Why are you reading into two variables?

    Soma

  7. #7
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20
    i have to get this number list

    Code:
     40
    13
    25
    58
    55
    12
    20
    9
    3
    6
    8
    12
    which is located in the infile

    and i have to write only the even numbers to the outfile

    my questio is how do i figure out how to only get the even ones...i keep getting the wrong numbers in the outfile txt

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    If that example is truly representative of your code, it is no wonder that you get the wrong values.

    I'll ask again. What code are you using?

    You need to inspect every value to determine if the value is even.

    Soma

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Couple of bugs in that posted code.

    Code:
    while (infile.eof()== false)
    Should be replace with
    Code:
    while(infile >> number)
    Reason is answered in the FAQ, but basically using eof with cause you to read in more characters than you want.

    Also this line
    Code:
    infile >> evenNumber;
    Does not need to be there. You just need to check if number is even and then output it.
    Woop?

  10. #10
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20
    ok and how do i check for even numbers using what code and how do i code to put it into the outfile...

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Using the code you already posted.

    Code:
    if(number % 2 == 0){
        outfile << number<< endl;
    }//if
    Woop?

  12. #12
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20
    this is what i get in the outfile txt

    13
    9
    12

    clearly not the even numbers

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Post the code as it stands now, so I can check it out.
    Woop?

  14. #14
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20
    Code:
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
    	short number = 0;
    	short evenNumber = 0;
    
    	ofstream outfile;
    	ifstream infile;
    	outfile.open ("evennumList.txt",ios.app);
    	infile.open ("numberList.txt",ios.in);
    
    	if (infile.is_open()== false)
    		cout << "Error"<<endl;
    	else
    		while (infile.eof()== false)
    		{
    			infile >> num;
    			infile >> evenNum;
    			if (num%2 == 0)
    			{
    				outfile << evenNum << endl;
    			}
    		}
    		infile.close();
    		outfile.close();
    
    		return 0;
    
    
        
    }
    here the numberList.txt
    Code:
    40
    13
    25
    58
    55
    12
    20
    9
    3
    6
    8
    12

  15. #15
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    I am going to re-quote myself.

    Couple of bugs in that posted code.

    Code:

    while (infile.eof()== false)

    Should be replace with
    Code:

    while(infile >> number)

    Reason is answered in the FAQ, but basically using eof with cause you to read in more characters than you want.

    Also this line
    Code:

    infile >> evenNumber;

    Does not need to be there. You just need to check if number is even and then output it.
    Woop?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 06:59 PM
  2. Access file's info in MINIX
    By Zuko in forum Linux Programming
    Replies: 3
    Last Post: 01-07-2008, 10:14 AM
  3. Replies: 3
    Last Post: 09-22-2003, 10:48 PM
  4. How to Access a Function from Multiple Files?
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2002, 07:24 PM
  5. Random Access Files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 09:06 AM

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