Help with Streams

This is a discussion on Help with Streams within the C++ Programming forums, part of the General Programming Boards category; ok this outputs about 100 numbers with the spaces #include <iostream.h> #include <fstream.h> #include <iomanip.h> #include <stdlib.h> #include <time.h> #include ...

  1. #16
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    ok this outputs about 100 numbers with the spaces

    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    #include <string.h>




    int main()

    {
    int num[120];
    int counter;

    int x;

    ofstream outstream("digits2.dat");



    if(outstream.fail())
    {
    cout<<"File Problem"<<endl;
    exit(1);
    }


    srand(time(NULL));


    for(counter=0; counter<100; counter++)
    {
    x=1 + rand() % 100;
    outstream<<x; // this puts number into file and increments marker/counter to the end of the number

    }// this is where it goes back and puts the next number and another space... does this 100 times





    outstream.close();

    return(0);

    }
    Last edited by venomgts550; 12-13-2001 at 08:53 PM.

  2. #17
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>yea I see some numbers in the file



    awesome... see you are halfway there. No reason to get frustrated.


    Also...

    .put() is used with your ofstream... sorry bout that. I meant it in my head but wrote something different.





    Examine this ANSI complient code... how does it differ from your output to file code.

    Code:
    
    #include <fstream>
    #include <iostream>
    #include <stdlib>
    
    using namespace std;
    
    int main ()
    {
    
         // Local variables
         char mychar;        // Character Storage
         int counter=0;        // Integer counter
    
         char array[5] = "1234";
         
         ofstream outs;     // output buffer
         ifstream ins;      // input buffer
    
         outs.open("betazep.txt"); // open output file
    
         if (outs.fail()) // error checking
         {     
              cout << "Output file failure." << endl;
              return 0;
         } // end if
    
    
         //********************************//
        
    
         // Cout array
         cout << array << endl << endl;
          
          
          // Output array to file
          while(counter < 5)
          {
    
               outs.put(array[counter]);
               outs.put(' ');
               cout << array[counter];
               ++counter;
    
           }// end while
    
         outs.close(); // close output file
    
    
         // Copying from one file to another**************************
         ins.open("betazep.txt");  // open input file
    
         if (ins.fail())
         {
    
             cout << "Input file failure." << endl;
             return EXIT_FAILURE;  // as found in stdlib
    
         } // end if
    
         outs.open("venomgts550.txt");  // open output file
    
         if (outs.fail())  // error checking
        {
    
              cout << "Output file failure." << endl;
              return 0;
    
        } // end if
    
        while (!ins.eof())
        {
    
             ins.get(mychar);
             if (mychar != ' ')
             {
             outs.put(mychar);
             cout << "copying " << mychar << endl;  // show what is getting copied
             } // end if
    
         } // end while... file copied without spaces
    
         ins.close();
         outs.close();
    
    return 0;
    
    } // end main
    
    
    
    

    compile that and see how it works... also try to increase the array size by one and add one more number and then go down and increase the counter < 5 to counter < 6.

    Notice that the file manipulation can handle any file size. It is only limited by the end of file.
    Last edited by Betazep; 12-13-2001 at 09:00 PM.
    Blue

  3. #18
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Its a little confusing to me but it looks like it open betazip.txt then copies it to venomgts550.txt
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  4. #19
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    good... now lets clean it up... try this on your compiler

    #include <iostream>
    #include <fstream>
    #include <stdio>
    using namespace std;

    int main()

    {

    int x, counter;

    ofstream outstream("digits2.dat");

    if(outstream.fail())
    {
    cout<<"File Problem"<<endl;
    return 0;
    }

    for(counter=0; counter<100; counter++)
    {
    x=1 + rand() % 100;
    outstream << x;
    outstream.put(' ');
    }

    outstream.close();

    return(0);

    }
    Blue

  5. #20
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>Its a little confusing to me but it looks like it open betazip.txt then copies it to venomgts550.txt

    It opens betazep.txt and puts 1 2 3 4 into it and closes it.

    Then it opens both files and copies betazep.txt over to venomgts550.txt without the spaces.

    Like...

    1234

    Doesn't do much really. But the latter portion could copy an entire text file into another file.
    Blue

  6. #21
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    ok i got it to work
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  7. #22
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    I gotta clean my house. Try writing a seperate program that can read the input from the file and view them as integers.

    Get it to do just one number and I will show you how to loop through the rest effectively.

    Bye for tonight....

    Betazep
    Blue

  8. #23
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    ok thanx for all your help
    good night
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  9. #24
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Can someone please help me I cant seem to get this input right
    this is what i have so far

    #include
    #include
    #include
    #include
    #include
    #include
    int main()
    {
    int array[120];
    int x, counter;
    ofstream outstream("digits2.dat");
    if(outstream.fail())
    {
    cout<<"File Problem"<return 0;
    }
    srand(time(NULL));
    for(counter=0; counter<100; counter++)
    {
    x=1 + rand() % 100;
    outstream<outstream.put(' ');
    }
    outstream.close();
    ifstream instream("digits2.dat");
    instream>>x;
    //----------------------------------------------------------------------------------------------------
    char mychar;
    int integer_counter;
    int size_three_char_array[120];
    for(int count=0; count<100; count++)
    {
    instream.get(mychar);
    cout<outstream.put(' ');
    if ( mychar != ' ')
    {
    size_three_char_array[integer_counter]; // put the number in an array at position zero for now
    integer_counter++; // originally set to 0
    }
    else {
    integer_counter = 0; // if we didn't find a number then we found a space, and we need to clear our counter
    }
    //----------------------------------------------------------------------------------------------------
    }
    instream.close();
    return(0);
    }
    Last edited by venomgts550; 12-14-2001 at 10:51 PM.
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  10. #25
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    bump
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  11. #26
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    where have you gotten with this?
    Blue

  12. #27
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    i tried to get the output to work

  13. #28
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Code:
    // Program opens a file filled with integer values seperated by spaces and finds the
    // largest value within the file.  Integers must be three digits or less, and the final
    // integer must be followed by a space to have it included.  (all which can be modified
    // easily within the code)
    
    # include<fstream>
    # include<iostream>
    # include <stdio>
    using namespace std;
    
    int main ()
    {
    
    	// Local variables
    	ifstream ins;
            char yourfile[100], mynum[4];
            char mychar;
            int count, temp, final = 0;
    
    	cout << "What is the file path/name: ";
    	cin  >> yourfile;
    
    	ins.open(yourfile); 
    
    	if (ins.fail())
    	{
    	   while (ins.fail())
    	   {
    		cout << "Failure opening input file.  Check file path." << endl << endl
                         << "You entered " << yourfile << endl
                         << "Enter path/name (or X to quit): ";
                    cin  >> yourfile;
    		
                    if ((yourfile[0]=='X')||(yourfile[0]=='x'))
    		     return 1;
    
    
                    ins.open(yourfile);
    
    	   } // end while
    	}// end if
    
            count = 0;
    
            while (!ins.eof()) // go until file ends
            {
                    ins.get(mychar);
                    cout << mychar;
    
                    if ((isdigit(mychar))&&(mychar!=' '))
                    {
                       mynum[count] = mychar;
                       ++count;
                    } // end if
    
                    if (mychar == ' ')
                    {
                          count = 0;
                          temp = atoi(mynum);
    
                          if (temp > final)
                          {
                             final = temp;
                          }
    
                          mynum[0] = '\0';
                          mynum[1] = '\0';
                          mynum[2] = '\0';
                    } // end if
    
                     
             }// end while
    
    
        	ins.close();
    
            cout << endl << endl <<"The largest number in the file was "
                 << final;
    
    
    
    return 0;
    }
    That does it pretty much. I cannot help you anymore. you put the two together and try to figure it out. There are other ways as well....
    Blue

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

Similar Threads

  1. Writing to two streams: va_arg issues
    By dwks in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 10:14 AM
  2. input/output streams
    By cybernike in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2007, 11:15 PM
  3. Independent streams of pseudo random number generator
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 04:32 AM
  4. File Streams, new error on me.
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 07:28 PM
  5. Replies: 4
    Last Post: 10-16-2001, 02:00 PM

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