Thread: Need help with writing to file

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    Need help with writing to file

    The program is to generate random numbers to a file and will have one integer parameter, Open a file and then using a loop write the required number of random numbers to the file. Scale the random numbers from 1 and 100 inclusive. Then closes the file .The last function will read the numbers in the file into your program.
    so far i have
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <ctime>
    #include <fstream>
    #include <cstdlib>
    
    
    using namespace std;
    int Validate();
    
    
    int main()
    {
        int t;int random;
        ifstream inputFile; 
        t = Validate();
        int random_integer;
        inputFile.open("Randoms.txt");
    //How do you make it put  the random numbers in a file after asking the user to determine how many to put.
    
        srand((unsigned)time(0)); 
        for(int index=0; t; index++);
            random = (rand()%1000)+1; 
            cout << random << endl; 
            int index;
            ifstream ifile("Randoms.txt");
        if(ifile)
        {
            for(index = 0;index > 0; random++)
                
                ifile >> random;
                
            ifile.close();
        }
        else
        {
            cout << "Error, file failed to open\n";
            system("pause");
            exit(2);
        }
    
    
    
    
    system("pause");
    return 0;
    }
    
    
    int Validate()
    {
        int t;
        do 
        {
        cout << "How many numbers to write?"<< endl;
        cin >> t;
        }
        while (t <50 || t >1000);
        return (t);
    }
    Last edited by gevans; 04-03-2013 at 07:49 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you trying to open the same file twice? And why aren't you opening the file in the constructor:

    Code:
    ifstream inputFile("Randoms.txt");

    The following line is incorrect for a couple of reasons

    Code:
        for(int index=0; t; index++);
    First the condition section is malformed, it should have some kind of comparison operation. Also you should get into the habit of using braces with your condition statements, only one of those lines would be executed inside the loop. That is if the statement didn't terminate with the semicolon.

    Also in future you may want to actually ask a question.


    Jim

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    12
    how do i check to see if the file actually got written to?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I would say it probably didn't get written. First you can't write to an input stream. Second input streams, with the default open arguments, will not create a file if the file doesn't exit.

    You should always check to see that your files open properly. You may want to study the following tutorial: File IO

    To actually check, you should view the programs working directory to see if the file exists.

    Jim

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    alright

    Quote Originally Posted by jimblumberg View Post
    I would say it probably didn't get written. First you can't write to an input stream. Second input streams, with the default open arguments, will not create a file if the file doesn't exit.

    You should always check to see that your files open properly. You may want to study the following tutorial: File IO

    To actually check, you should view the programs working directory to see if the file exists.

    Jim
    alright i see it now! now how do i write the random numbers to it

  6. #6
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by gevans View Post
    how do i check to see if the file actually got written to?
    Go to the location of the file and open it.After opening the file check whether the file is open before trying to write to it.You can check this link for a rough idea of how you should go about it,..A development process

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    help

    how do i get more than one random number to output in a file

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <ctime>
    #include <fstream>
    #include <cstdlib>
    
    
    using namespace std;
    int Validate();
    
    
    int main()
    {
    	
    	ofstream myfile;
      myfile.open ("example.txt");
       srand((unsigned)time(0)); 
           int random = (rand()%1000)+1; 
            cout << random << endl; 
            int index;
            ifstream ifile("Randoms.txt");
    myfile << random << endl;
    
    
      myfile.close();
      return 0;
    }
    
    
    int Validate()
    {
    	int t;
    	do 
    	{
    	cout << "How many numbers to write?"<< endl;
    	cin >> t;
    	}
    	while (t <50 || t >1000);
    	return (t);
    }
    Last edited by gevans; 04-03-2013 at 09:28 PM.

  8. #8
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    You are not checking whether your file is open.
    You are also opening the file twice.
    Try doing something like this:
    while(inputFile.good()){
    for(int index=0; index<t; index++)
    random = (rand()%1000)+1;
    inputFile << random << endl;
    inputFile.close()

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    how do i get more than one random number to output in a file
    Usually by printing more than one number to your file. Think about looping.

    You need to find an indentation style you like and use it consistently. This will make reading your code much easier.

    Jim

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    lost

    im lost guys.....its not working

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <ctime>
    #include <fstream>
    #include <cstdlib>
    
    
    using namespace std;
    int Validate();
    
    
    int main()
    {
    	int t;
    	int random;
    	ofstream myFile;
      myFile.open ("example.txt");
        
    while(myFile.good())
    for(int index=0; index<t; index++)
    random = (rand()%1000)+1; 
    myFile << random << endl; 
    myFile.close();
      return 0;
    }
    
    
    int Validate()
    {
    	int t;
    	do 
    	{
    	cout << "How many numbers to write?"<< endl;
    	cin >> t;
    	}
    	while (t <50 || t >1000);
    	return (t);
    }

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    Need help with loops

    still not working
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <ctime>
    #include <fstream>
    #include <cstdlib>
     
     
    using namespace std;
    int Validate();
     
     
    int main()
    {
    	int random;
         float t;
        ofstream myfile;
      myfile.open ("example.txt");
       
         while(myfile.good())
    for(int index=0; index<t; index++)
    random = (rand()%1000)+1; 
    myfile << random << endl;
     
     
      myfile.close();
      system("pause");
      return 0;
    }
     
     
    int Validate()
    {
        int t;
        do
        {
        cout << "How many numbers to write?"<< endl;
        cin >> t;
        }
        while (t <50 || t >1000);
        return (t);
    }

  12. #12
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    You have not initialized the valiable t,...m?

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    12
    i give up!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 04-20-2009, 04:33 PM
  2. Writing to the file!
    By Terran in forum C++ Programming
    Replies: 12
    Last Post: 07-20-2008, 08:35 AM
  3. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  4. reading from file and writing in a nother file
    By undisputed007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2004, 02:17 PM
  5. Writing to file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 01-17-2002, 09:37 AM