Infile, Outfile help!

Pages: 12
heyya guys i seem to be having a problem, hopefully someone can point it out, my outfile only prints out one line of results so not sure what the error is.
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>

using namespace std;

const int MAX = 100;

enum NumType {Odd, Even};

struct Number
{
int no;
NumType type;
int oddDigits;
int evenDigits;
int sumDigits;
int noDigits;
};

void arrayToOutfile (ofstream&, char [], Number [], int);
void constructInfile (fstream&, char []);
int constructArray (fstream&, char [], Number []);
void processArray (Number [], int);
NumType whatType (int);
void getStringLabel (NumType, char []);

int main ()
{
srand (time_t(NULL));
fstream inFile;
char fileName [MAX];
//int size = rand() % 100;

cout << "Enter filename: ";
cin >> fileName;

constructInfile (inFile, fileName);

cout << "----------------------------------------" << endl;

Number n [MAX];

int size = constructArray(inFile, fileName, n);
cout << "Elements in Array = " << size << "\n\n";

processArray (n, size);

cout << "----------------------------------------" << endl;

ofstream outFile;

cout << "Enter the output filename: ";
cin >> fileName;

arrayToOutfile (outFile, fileName, n, size);
}



void constructInfile (fstream& inFile, char fileName[])
{
inFile.open (fileName, ios::out);

if (!inFile)
{
cout << fileName << " cant be created for write"
<< endl;

exit (-1);
}

int size = rand() % 100;

for (int i = 0; i < size; i++)
{
inFile << rand() << "\t"
<< "// Number" << i << endl;
}

cout << "written to outfile successfully" << endl;

inFile.close();

}

int constructArray (fstream& inFile, char fileName[], Number n[])
{
inFile.open (fileName, ios::in);

if (!inFile)
{
cout << fileName << " cant be accessed for array creation."
<< endl;
exit (-1);
}
cout << "Begin file to array" << endl;
int i = 0;
while (inFile >> n[i].no)
{
++i;
}

inFile.close();
cout << "File to array transfer success" << endl;

return i;

}

void processArray (Number n [], int size)
{

cout << "Begin processing array" << endl;

//Odd or Even
for (int i = 0; i < size; i++)
{
n[i].type = whatType (n[i].no);
}


//copy number n array to temp n array
Number t [MAX];

for (int i = 0; i < size; i++)
{
t[i].no = n[i].no;
}

for (int i = 0; i <size; i++)
{
n[i].evenDigits = 0;
n[i].oddDigits = 0;
}

//odd and even function

int countEven;
int countOdd;

for (int i = 0; i < size; i++)
{
countEven = 0;
countOdd = 0;
while (t[i].no > 0)
{
if (t[i].no % 2 == 1)
{
countOdd++;
t[i].no/= 10;
}
else
{
countEven++;
t[i].no/=10;
}
n[i].oddDigits = countOdd;
n[i].evenDigits = countEven;
}

}

//copy number n array to temp n array again.
for (int i = 0; i < size; i++)
{
t[i].no = n[i].no;
}


for (int i = 0; i <size; i++)
{
n[i].sumDigits = 0;
}

for (int i = 0; i < size;)
{
while (t[i].no > 0)
{
n[i].sumDigits = n[i].sumDigits + t[i].no % 10;
t[i].no /= 10;
}i++;
}

//copy number n array to temp n array again.
for (int i = 0; i < size; i++)
{
t[i].no = n[i].no;
}
//SET TO DEFAULT 0 for COUNT DIGITS
for (int i = 0; i <size; i++)
{
n[i].noDigits = 0;
}

//DIGIT COUNT
for ( int i = 0; i < size; i++)
{
int countDigits = 0;

while (t[i].no != 0)
{
t[i].no /= 10;
countDigits++;
}
n[i].noDigits = countDigits;
}


//for (int i = 0; i < size; i++)
//{

//}

cout << "The array was processed" << endl;
}

//Number type.
NumType whatType (int n)
{

if (n % 10 % 2 == 1)
return Odd;
else
return Even;
}

//Array to Outfile.
void arrayToOutfile (ofstream& outFile, char fileName[], Number n[], int size)
{
outFile.open (fileName);

outFile << "No" << "\t"
<< "Type" << "\t"
<< "Odd" << "\t"
<< "Even" << "\t"
<< "Sum" << "\t"
<< "Digit"
<< endl;

while (!outFile)
{
cout << "Array to " << fileName << " failed" << endl;
outFile.close ();
}

cout << "Begin from array to " << fileName << endl;

cout << fixed << showpoint << setprecision (3);

char label [MAX];

for (int i = 0; i < size; i++)
{
outFile << n [i].no << "\t";

NumType whatType (int n);

getStringLabel (n[i].type, label);

outFile << n[i].no << "\t"
<< label << "\t"
<< n[i].oddDigits << "\t"
<< n[i].evenDigits << "\t"
<< n[i].sumDigits << "\t"
<< n[i].noDigits << "\t"
<< endl;
}


outFile.close();

cout << "Array to outfile OK" << endl;
}

//String label
void getStringLabel (NumType type, char label[])
{

switch (type)
{
case Odd: strcpy (label, "Odd");
break;
case Even: strcpy (label, "Even");
break;
default: strcpy (label, "err");
}
}

Why don't you indent your code?

And then put then put your code in like this:

[.code.]
Your code here...
[./code.]

Remove the periods (.)
I put them there so it wouldn't actually format my post as code.
Last edited on
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
[/output]
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>

using namespace std;

const int MAX = 100;

enum NumType {Odd, Even};

struct Number
{
    int no;
    NumType type;
    int oddDigits;
    int evenDigits;
    int sumDigits;
    int noDigits;
};

void arrayToOutfile (ofstream&, char [], Number [], int);
void constructInfile (fstream&, char []);
int constructArray (fstream&, char [], Number []);
void processArray (Number [], int);
NumType whatType (int);
void getStringLabel (NumType, char []);

int main ()
{
    srand (time_t(NULL));
    fstream inFile;
    char fileName [MAX];
	//int size = rand() % 100;

    cout << "Enter filename: ";
    cin >> fileName;

    constructInfile (inFile, fileName);

    cout << "----------------------------------------" << endl;

    Number n [MAX];

    int size = constructArray(inFile, fileName, n);
	cout << "Elements in Array = " << size << "\n\n";

    processArray (n, size);

    cout << "----------------------------------------" << endl;

    ofstream outFile;

    cout << "Enter the output filename: ";
    cin >> fileName;

    arrayToOutfile (outFile, fileName, n, size);
}



void constructInfile (fstream& inFile, char fileName[])
{
    inFile.open (fileName, ios::out);

    if (!inFile)
    {
        cout << fileName << " cant be created for write"
        << endl;

        exit (-1);
    }

    int size = rand() % 100;
    
	for (int i = 0; i < size; i++)
	{
		inFile << rand() << "\t"
		<< "// Number" << i << endl;
	}

    cout << "written to outfile successfully" << endl;

    inFile.close();

}

int constructArray (fstream& inFile, char fileName[], Number n[])
{
    inFile.open (fileName, ios::in);

    if (!inFile)
    {
        cout << fileName << " cant be accessed for array creation."
        << endl;
        exit (-1);
    }
    cout << "Begin file to array" << endl;
    int i = 0;
    while (inFile >> n[i].no)
    {
        ++i;
    }

    inFile.close();
    cout << "File to array transfer success" << endl;

    return i;

}

void processArray (Number n [], int size)
{

    cout << "Begin processing array" << endl;
    
	//Odd or Even 
    for (int i = 0; i < size; i++)
    {
        n[i].type = whatType (n[i].no);
    }


//copy number n array to temp n array
    Number t [MAX];

    for (int i = 0; i < size; i++)
    {
        t[i].no = n[i].no;
    }

    for (int i = 0; i <size; i++)
    {
        n[i].evenDigits = 0;
        n[i].oddDigits = 0;
    }

//odd and even function

    int countEven;
    int countOdd;

    for (int i = 0; i < size; i++)
    {
        countEven = 0;
        countOdd = 0;
        while (t[i].no > 0)
        {
            if (t[i].no % 2 == 1)
            {
                countOdd++;
                t[i].no/= 10;
            }
            else
            {
                countEven++;
                t[i].no/=10;
            }
            n[i].oddDigits = countOdd;
            n[i].evenDigits = countEven;
        }

    }

    //copy number n array to temp n array again.
    for (int i = 0; i < size; i++)
    {
        t[i].no = n[i].no;
    }

    
    for (int i = 0; i <size; i++)
    {
        n[i].sumDigits = 0;
    }

    for (int i = 0; i < size;)
    {
        while (t[i].no > 0)
        {
            n[i].sumDigits = n[i].sumDigits + t[i].no % 10;
            t[i].no /= 10;
        }i++;
    }

    //copy number n array to temp n array again.
    for (int i = 0; i < size; i++)
    {
        t[i].no = n[i].no;
    }
    //SET TO DEFAULT 0 for COUNT DIGITS
    for (int i = 0; i <size; i++)
    {
        n[i].noDigits = 0;
    }

    //DIGIT COUNT
    for ( int i = 0; i < size; i++)
    {
        int countDigits = 0;

        while (t[i].no != 0)
        {
            t[i].no /= 10;
            countDigits++;
        }
        n[i].noDigits = countDigits;
    }


    //for (int i = 0; i < size; i++)
    //{

    //}

    cout << "The array was processed" << endl;
}

//Number type.
NumType whatType (int n)
{

    if (n % 10 % 2 == 1)
        return Odd;
    else
        return Even;
}

//Array to Outfile.
void arrayToOutfile (ofstream& outFile, char fileName[], Number n[], int size)
{
    outFile.open (fileName);
	
	outFile << "No" << "\t"
    << "Type" << "\t"
    << "Odd" << "\t"
    << "Even" << "\t"
    << "Sum" << "\t"
    << "Digit"
    << endl;

    while (!outFile)
    {
        cout << "Array to " << fileName << " failed" << endl;
        outFile.close ();
    }

    cout << "Begin from array to " << fileName << endl;

    cout << fixed << showpoint << setprecision (3);

   char label [MAX];
   
	for (int i = 0; i < size; i++)
	 {	
	 	outFile << n [i].no << "\t";
	 
	 	NumType whatType (int n);
	 
        getStringLabel (n[i].type, label);

        outFile << n[i].no << "\t"
        << label << "\t"
        << n[i].oddDigits << "\t"
        << n[i].evenDigits << "\t"
        << n[i].sumDigits << "\t"
        << n[i].noDigits << "\t"
		<< endl;
    }

    
	outFile.close();

    cout << "Array to outfile OK" << endl;
}

//String label
void getStringLabel (NumType type, char label[])
{

        switch (type)
    {
        case Odd:   strcpy (label, "Odd");
            break;
        case Even:  strcpy (label, "Even");
            break;
        default:    strcpy (label, "err");
    }
}
[output]

haha thxs was trying how to use tht....
vinci65812301 wrote:
heyya guys i seem to be having a problem, hopefully someone can point it out, my outfile only prints out one line of results so not sure what the error is.

Tell us what your code is supposed to do and where in your code (which line) the outfile prints out only 1 line.
Last edited on
i think the prob lies on line 232 onwards theres no error and stuff in the compiler everything works, but the code is meant to print out multiple lines, so basically transfer the info from the infile to the outfile...

lemme knw if you need any info
Last edited on
On line 235:

outFile.open(fileName)

should be

outFile.open(fileName, ios::app)


This opens the file in append mode. If you don't open the file in append mode you don't append to the file and all exisiting data is overwritten. Keep in mind you still need to provide newlines if you want them.
so this lets more lines to be in? originally it only allows one line? sorry first time seeing ios::app
It allows you to write to the file more than once. Without it every time you write to it everything that was in there before is "erased".

You can write more than one line into a file without ios::app but you have to do it all at once.

So, yes it does let more lines in even when you output to the file more than once.

File mode options:

ios::in Open for input operations.

ios::out Open for output operations.

ios::binary Open in binary mode.

ios::ate Set the initial position at the end of the file.
If this flag is not set, the initial position is the beginning of the file.

ios::app All output operations are performed at the end of the file, appending the content to the current content of the file.

ios::trunc If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.


class default mode parameter

ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
Last edited on
ok ok thanks! so am i missing anything else? now it just shows two lines...but i need it to print out a list....sry for all the questions....really noob at this
Which line in your code prints out the list?
same line 233 for the outfile....the infile works fine...infile is on line 65
vinci65812301 wrote:
ok ok thanks! so am i missing anything else? now it just shows two lines...but i need it to print out a list....sry for all the questions....really noob at this

Where does it "show"? On the screen with std::cout or in the file?
Last edited on
as in where it prints out? in the .txt file
Are you trying to write "No\tType\tOdd\tEven\tSum\tDigit" to the file?
i dont get your meaning....its alrdy in the file...like it shows up in the .txt
When you write to the outFile, you write "No\tType..." etc to it before you write to it from the inFile. This makes it so all the data from the inFile is after the "No\tType\tOdd...".

You need to first write "No\t" to the outFile, then the data from the inFile. After that "Type\t", then data from inFile and so on.
There's a lot of things in your code to be fixed (it's large so hard to tell) and it's getting late. Hopefully, you get help from someone else.
so its smth like

outfile << "No" << "\t" << n[i].no << "\t" ?
Pages: 12