Thread: How Do I Read a File to a Program?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    14

    How Do I Read a File to a Program?

    I tried to search Google, and found absolutely nothing simple that could help. Basically, what I need to do is make filter options for a program. I have no idea how to do this at all. The program I have is this:
    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main()
    {
    int arrayname[10], q = 0;
    while (q < 10) //assign each element of arrayname[] to a random integer from 1 to 10
    {
    arrayname[q] = rand()%10;
    q++;
    }
    q = 0;
    while (q < 10) //cout every element of arrayname on its own line
    {
    cout << arrayname[q]
    q++
    }
    What I want to be able to do is make it so that, from an external file, you can set numbers that cannot be chosen. So, you can go into a separate file, and choose that you don't want the random generator to ever get the numbers 4 or 7, or something like that. I just need to be able to filter it using an external file. How do I do it? Can someone tell me, or just link me to a tutorial?

    Sorry for wasting your time. =[

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You can look here on how to open and read from a file
    For example, if you have two numbers in a line like "4 7" separated by space, you can read this line and then parse the string and separate "4" and "7".
    To convert a string to an actual integer to use in your code take a look at this for example

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You can make your own Rand() function and after reading the file and parsing the excluded integers do something like:
    Code:
    FUNCTION Rand : integer
    VARIABLES
        random : integer
        i : integer
    BEGIN
    
    LOOP
        random <- rand()
        FOR i FROM 1 TO sizeof(exclusionArray)
            IF random = exclusionArray[i]
                BREAK FOR
            END IF
        NEXT i
    WHILE i < sizeof(exclusionArray)
    Rand <- random
    
    END
    Disclaimer: I don't know if this pseudocode is working or even if I made up some of the names, use at your own risk!
    Last edited by GReaper; 09-26-2012 at 08:21 AM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    Quote Originally Posted by C_ntua View Post
    You can look here on how to open and read from a file
    For example, if you have two numbers in a line like "4 7" separated by space, you can read this line and then parse the string and separate "4" and "7".
    To convert a string to an actual integer to use in your code take a look at this for example
    Thanks, I'll check it out.
    Quote Originally Posted by GReaper View Post
    You can make your own Rand() function and after reading the file and parsing the excluded integers do something like:
    Disclaimer: I don't know if this pseudocode is working or even if I made up some of the names, use at your own risk!
    I'll try that out, and see if I can get it to work, but either way I would need to be able to read from a file before I can make use of it. =P

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    Apparently, the board no longer wants me to edit my other post, since it won't let me, so I guess I have no choice but to double post.

    I can comfortably read from files with the use of ifstream, and that is exactly what I needed to do, so thank you. However, what if I need to read data from two different files? It doesn't seem to want me to use ifstream twice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why Won't My Program Read My .hpp file?
    By Insidia in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2012, 11:59 AM
  2. Basic file read, file write program
    By starwarsyeah in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2011, 03:23 PM
  3. C program file read/write help
    By BobDole11 in forum C Programming
    Replies: 5
    Last Post: 10-13-2008, 09:55 AM
  4. Read data from file - C program ?
    By Pawan Sangal in forum C Programming
    Replies: 2
    Last Post: 08-22-2008, 04:28 AM
  5. My program can't read an internet file!
    By Queatrix in forum Windows Programming
    Replies: 3
    Last Post: 05-06-2005, 04:25 PM