File Io

Could someone tell me if this is right. I have to make a program that generates 1000 random numbers in a file than separates the numbers into a positive file and a negative file. Since I cannot find these files on Xcode I am not sure if my program is correct, so could someone tell me if this is right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <fstream>

using namespace std;


bool connectToInputFile(ifstream& fin, const string& filename);
bool connectToOuputFile(ofstream& fout, const string& filename);
void writeRandomNumbersToFile(const string& filename);
void splitNumbersBySign(const string& inputFilename, const string& posFilename, const string& negFilename);


int main() {
    
    /* Generate the 1000 random numbers in the range -1000 to 1000 and save
     * them in a file.
     */
    writeRandomNumbersToFile("randomNumbers.txt");
    
    
    /* Split the random numbers into two files. One containing all the positive
     * numbers and the other all the negative numbers.
     */
    splitNumbersBySign("randomNumbers.txt", "pos.txt", "neg.txt");
    
    cout << "Both pos.txt and neg.txt have been created successfully." << endl;
    
    cout << endl;
    
    return 0;
}// end main()

bool connectToInputFile(ifstream& fin, const string& filename){
    fin.open(filename.c_str());
    
    if (fin.fail()) { return false; }
    else return true;
    
}
bool connectToOuputFile(ofstream& fout, const string& filename){
    fout.open(filename.c_str());
    
    if (fout.fail()) { return false; }
    else return true;
}
void writeRandomNumbersToFile(const string& filename){
    
    ofstream fout;
    
    if ( !connectToOuputFile(fout, "randomNumbers.txt") ) {
        cerr << "Error opening file rand.numbers ";
        exit(1);
    }
    
    
    int r;
    for(int i=0; i<=1000-1; i++){
        r= rand() % 2001-1000;
        
        fout<<r<<" ";
    }
    
    
    
    
    cout << "File was written successfully." << endl << endl;
    
    
    fout.close();
    
    
}

void splitNumbersBySign(const string& inputFilename, const string& posFilename, const string& negFilename){
    int r;
    
    ofstream foutpos;
    ofstream foutneg;
    ifstream fin;
  
    connectToInputFile(fin,  inputFilename);
    connectToOuputFile(foutneg, negFilename);
    connectToOuputFile(foutpos, posFilename);
    fin >> r;
    while ( !fin.eof()) {
        
        
        
        
        if(r < 0){
            
            
            
                                       foutneg<<r<<" ";        }
        
        
        else {
            
            
            
                foutpos << r << " ";
            
        }
        fin>>r;
    }
    
    
    fin.close();
    
    
    
}



    

Last edited on
You have no way of checking your i/o files? I certainly do wish you the best of luck in your future programming endeavors. Although, it seems the wiser path to take would be to figure out why you cannot find the files. I digress.

You program creates only two files, "randomNumbers.txt" and "pos.txt". Here is what is in each:

randomNumbers:
-959 -542 -669 -513 160 717 473 344 -51 -548 703 -869 270 -181 957 -509 -6 937 -175 434 -625 -403 901 -847 -708 -624 413 -293 709 886 445 716 -236 533 869 903 655 -714 27 890 -311 800 307 -682 665 -338 134 708 -761 -135 535 631 -354 -259 -973 -147 -281 737 516 -222 -690 34 -821 842 -712 -909 36 -62 255 -363 433 794 883 -274 -642 343 -1 86 -619 547 620 -383 -928 945 -253 835 -36 373 925 -705 -64 -577 -386 318 535 528 -890 -919 -82 -467 -169 100 -363 644 -307 926 971 -695 658 -625 19 -269 -89 63 -733 827 -236 566 95 -496 975 284 157 -373 -656 -245 644 567 -971 -954 337 150 -67 714 960 429 92 -824 999

pos:
999

I highly suggest you figure out a way to view your output files. I don't think people are going to continue doing this for you.
I fixed some stuff around now I have one file that is correct the randomNumber one, but the Pos and Neg number are only reading one number for some reason.
Lines 88-92 read the entire file. Lines 97-121 take the last number read and write it to one of the two files. The next 999 times through the loop at line 87 you fail to read from the input (because you read the entire file the first time) so r is unchanged and it gets rewritten to an output file.

In pseudo-code, splitNumbersBySign should do this:
- open ALL THREE files: input file, pos file and neg file.
- while you can successfully read a number from input file
- write the number to pos file or neg file, depending on its sign.
- close ALL THREE files
Thank you for your help it was great dhayden. If you were interested I put the code up
Sure, post the code or send it in a private message.
Sure. Post the code or send it in a private message.
Topic archived. No new replies allowed.