Help with Streams

This is a discussion on Help with Streams within the C++ Programming forums, part of the General Programming Boards category; I have written code at the bottom to write 100 random numbers to a file then read the file but ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Help with Streams

    I have written code at the bottom to write 100 random numbers to a file then read the file but Im not sure how to get it to read all 100 numbers, also I need to have it pick out the greatest number which I have no clue as to how to do

    CODE:
    #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;

    char str[10];

    ofstream outstream("digits2.dat");

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

    int x;
    srand(time(NULL));



    for(counter=0; counter<100; counter++)
    {
    x=1 + rand() % 100;



    }

    outstream<<x;

    outstream.close();



    ifstream instream("digits2.dat");


    instream>>x;

    cout<<x<<endl;

    instream.close();



    return(0);
    }

    ThANX for any Help
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    I don't know about the streams but for finding the greaters number have it iterate through all the numbers. If the number is greater then the one before it save it to a variable

    something like this probably isn't right all the way but you will get the idea

    for(int i = 0; i < 100; ++i)
    {
    int numbertovariable = 0;
    if(numberfrom file > numbertovariable)
    {
    numbertovariable = numberfromfile;
    }
    }

    sorry for my crude explanation but I absolutely suck at writing loops and using boolean expressions.

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    for(counter=0; counter<100; counter++)
    {
    x=1 + rand() % 100;



    }

    outstream<<x;

    How many numbers does that write to your file? Maybe only one...

    You need markers in your file in between the numbers (like a space) otherwise you just have a really long number.

    for(counter=0; counter<100; counter++)
    {
    x=1 + rand() % 100;

    outstream<<x;

    outstream.put(' '); // or a # or a comma... Something to deliminate the numbers

    }

    Then you have a file of numbers that you need to loop through until instream.eof(). (It doesn't matter how many numbers are in there then.)

    If you just need it to read the numbers, that isn't too bad (though it is terribly easier with numbers 0-9). I guess you would be dealing with one or two digit numbers in most case, but the possibilty exists for a three digit, I think (100).

    You can use

    your_ifstream.get(type_char);

    to pull a character and advance the pointer. If the character is not a space place into an array and get next character. If that character is not a space, place it into the next array slot, etc.

    atoi() the character array which gives you an integer.

    Integers are easily compared against eachother.

    Others may have easier ideas... but going this route would allow you to handle files of any size.
    Last edited by Betazep; 12-12-2001 at 10:50 PM.
    Blue

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Consider the following code. I figured I would give you a little more, but you really should try to figure the rest out.


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

    int main()
    {
    char x[3];

    int y;

    x[0] = '1';
    x[1] = '2';

    cout << x << endl;

    y = atoi(x);

    cout << y;

    return 0;
    }
    Blue

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    I dont know what markers are and when I put cout<<x in the part where its supposed to write to the file it makes all 100 number but i dont think it writes any to the file.

    If I make x into a string would it display all the numbers?
    Last edited by venomgts550; 12-13-2001 at 04:14 PM.
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  6. #6
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    There is no possible way that it outputs 100 numbers to the file or to the screen with the way you have it written.


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


    This is what this says... make a random number and add one. Place that number in x. Do that 100 times. Then put x into a file. Only since x is an integer and not an integer array, you are only overwriting x several times (100 to be exact) and then you put x into the file which will be the last random integer that was placed into x.

    continuing...

    Think in your head how the file is created, one number at a time (kinda like typing). It goes along and places a number

    13

    which is a 1 and then a 3-and now the counter/marker is at the third position now

    Then you want it to go and place a space so that that number is all by itself and then place the next number. You want to do this many times...

    13 14 12 87 etc

    So to do this, you must do this...


    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
    instream.put(' '); // this puts a space and moves the cursor/marker/counter to next space
    }// this is where it goes back and puts the next number and another space... does this 100 times



    outstream.close();



    Now you have a file with 100 numbers that are seperated by spaces. That is file output.
    Last edited by Betazep; 12-13-2001 at 05:48 PM.
    Blue

  7. #7
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412

    File input

    Now you open the input stream to read from the file that has your 100 numbers seperated by spaces.

    Your marker/counter/cursor is at the beginning of the first number (you suspect) in the file.

    Still you need to check to see if the character there is a number and not a space.

    Like I said... it is a character now. Not a number. 92 is two characters... 92 13 is five characters (including the space).

    .get() is your friend in this application. It gets one character and then moves the cursor/marker/counter right after it.

    So if you file has

    92

    in it, and you used ifstream.get(mychar), mychar would hold 9 in it and the marker would be pointing to 2. Do it again and you have 2 in mychar and the cursor would be pointing at the space after it.

    This helps you considerably.... because you can now do this.

    check to see if you have a number or a space

    instream.get(mychar);

    if (mychar != ' ')

    //do this



    so what if you have a single digit number in mychar? How does that help if your actual number has two or three digits? Imagine this


    instream.get(mychar)

    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
    }



    Now we have one number of a two digit number in the first position of a character array and a counter that would point to the second position of the character array if we ran back through it.

    These are the basics of it... you need to loop your characters into the char array and then atoi() the array to make an integer. Store that integer, output it to the screen, and then loop back up for the next integer. Compare that integer and if it is larger, store it, and then ouput it to the screen (perhaps with spaces) and then loop back up... over and over again. And the computer will do it in less than a second for 100 iterations.

    do this untill instream.eof();

    Confused more or is it taking shape?
    Blue

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Hey thanx for the help
    It makes more sense now
    only thing is when I use instream.put(' ')
    my complier says it that .put is not a memeber of the ifstream
    so wut should I include to make it recognize that
    also wut is the atoi() function do exactly.
    Last edited by venomgts550; 12-13-2001 at 07:27 PM.
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    I made this new code and tried to incorporate what you said but when I use instream.eof() i get no output at all.
    #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");
    ifstream instream("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
    instream.putback(' '); // this puts a space and moves the cursor/marker/counter to next space
    }// this is where it goes back and puts the next number and another space... does this 100 times



    outstream.close();


    cout<<x;

    outstream.close();
    char size_three_char_array[120];
    int integer_counter;
    char mychar;

    do{

    instream.get(mychar);

    for(int count2=0; count2<100; count2++)
    {

    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
    }


    }


    }while(!instream.eof());

    return(0);
    }
    Last edited by venomgts550; 12-13-2001 at 07:50 PM.
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  10. #10
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    #include <string.h>


    You are not using the ANSI standard.

    .put is a member function of fstream using the standard namespace.

    Try this....

    #include <fstream>
    using namespace std;

    in place of

    #include <fstream.h>

    Then you can use

    .get()
    .put()
    .eof()

    etc...

    I will look at your second post now...
    Blue

  11. #11
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>i get no output at all

    did you look in the numbers.dat file to see if you had numbers in it? Open it with notepad. Did you get that far? Why or why not?

    I like helping people... but you gotta think too.
    Blue

  12. #12
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    outstream.close();


    cout<<x;

    outstream.close();


    you closed your stream twice...?
    Blue

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    sry im willing to think its just i get so frustrated sometimes
    didnt know i could open it in notepad, i thought it deleted what I wrote to it when I closed the file.
    yea I see some numbers in the file
    but they dont get outputted
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  14. #14
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    The do-while and the for loop defeat eachother on the file read...


    you need to break this problem down. First do a program that just does the output to a file. Examine the file. Make sure it works for you.

    Then do a program that reads from that file, outputs the file to the screen, and tells you what the biggest number read was.

    Then combine the two.

    Outputting to a file is considerably easier than inputting from in many cases.

    Remove all the input code including the ifstream instantiation at the beginning, and try getting your program to make the desired file.

    Once you pass that step. Post it, and I will help you with the second.... more difficult process.
    Blue

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    ok, thanx for all of your help so far
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

Page 1 of 2 12 LastLast
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