Writing to an outfile

I am having problems writing to an out-file, I am new to programming but I have problems with writing to out files, just not sure where or how to place syntax, where should I place my out statement and how? My code is listed below. Sorry it is such a long post.


#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void addLoop(ifstream&);
void zeroArray(int [], int);
void printArray(int [], int);
void getLine(int [], int);
void rightJustify(int [], int, int);
void addArray(int [], int [], int [], int);
bool checkValid(int [], int);

int main()
{
ifstream inFile;
// Open file to read
inFile.open("BigNumbersV2.txt");
// Check for error opening file
if(!inFile)
{
cout<<"Error opening BigNumbersV2.txt."<<endl;
return 1;
}

// Start addition loop
addLoop(inFile);

// Close file
inFile.close();

return 0;
}
void addLoop(ifstream& inFile)
{
// Function reads one number per line from a file, adds the numbers, and prints the sum
// Assumes:
// 'inFile' is the input file

char ch;

int a1,a2;
int size=26;

int ary1[size];
int ary2[size];
int ary3[size];

// Loop continues until end of file
while(inFile)
{
// Fill all three arrays with 0's
zeroArray(ary1, size);
zeroArray(ary2, size);
zeroArray(ary3, size);

a1=0; // reset array 1 counter
a2=0; // reset array 2 counter
while(inFile.get(ch) && ch!='\n')
{
// Read first number into ary1
ary1[a1]=ch-'0';
a1++;
}
while(inFile.get(ch) && ch!='\n')
{
// Read second number into ary2
ary2[a2]=ch-'0';
a2++;
}

// Right-justify ary1 and ary2
rightJustify(ary1, size, a1);
rightJustify(ary2, size, a2);

// Check that both arrays are valid before continuing
if(checkValid(ary1, size) && checkValid(ary2, size))
{
// move output over one space for formatting
cout<<" ";
printArray(ary1,size);
// print addition symbol
cout<<"+";
printArray(ary2,size);

// setfill() is used to fill row with underscores
// to format the summation line
cout<<setfill('_')<<setw(27)<<"_"<<endl;

// ary1 is added to ary2 and stored in ary3
addArray(ary1, ary2, ary3, size);
cout<<" ";
// print summation of ary1 and ary2
printArray(ary3,size);
cout<<endl; // Print end of line for formatting purposes
}
cout<<endl; // Print end of line for formatting purposes
}
}

void zeroArray(int myArray[], int size)
{
// Function will fill array with 0's
// Assumes:
// 'myArray' is an array
// 'size' is the size of the array

for(int i=0; i<size; i++)
{
myArray[i]=0;
}
}
void printArray(int myArray[], int size)
{
// Function will print all values of an array
// Assumes:
// 'myArray' is an array
// 'size' is the size of the array

bool zero=true;
int count=0, i=0;

// Replace starting 0 with space
if(myArray[0]==0)
{
cout<<" ";
count++;
}
// Check for leading zeros, incrementing count for each leading
// zero that is found
while(zero)
{
// Check for leading zeros and replace with spaces
if(myArray[count]==0 && myArray[count+1]==0)
{
cout<<" ";
// Increment counter if leading 0 is found
count++;
}
else if(myArray[count-1]==0 && myArray[count]==0)
{
cout<<" ";
// Increment counter if leading 0 is found
count++;
}
else
// No leading zeros found? Set zero to false to end loop
zero=false;
}
// Begin printing loop after the last leading zero (i=count)
for(i=count; i<size; i++)
{
cout<<myArray[i];
}

// Print end line
cout<<endl;
}
void rightJustify(int myArray[], int size, int i)
{
// Function will shift values of an array to right-justify
// Assumes:
// 'myArray' is an array
// 'size' is the size of the array
// 'i' is the farthest-right position of the number stored in the array

int pos=size;
int count=size-i;

for(int j=0; j<size; j++)
{
// Set array index pos to value of array index i
// to shift values to the right
myArray[pos]=myArray[i];
// decrement i to traverse left-values
i--;
// decrement pos to traverse right-values
pos--;
}
for(i=0; i<count; i++)
{
// fill left-side of array with 0's
myArray[i]=0;
}
}
void addArray(int ary1[], int ary2[], int ary3[], int size)
{
// Function adds two arrays and stores result in third array
// Assumes all arrays are of the same size and:
// ary1 is the first array to add
// ary2 is the second array to add
// ary3 is the array to store the sum
// size is the size of the three arrays

for(int i=size-1; i>0; i--)
{
// Add ary1 to ary2 and store value in ary3
ary3[i]+=ary1[i]+ary2[i];
// Check to see if the sum is larger than 9
if(ary3[i]>9)
{
// Store "carry" in previous index position
ary3[i-1]=1;
// Use modulo operator to remove carry
ary3[i]=ary3[i]%10;
}
}
}
bool checkValid(int myArray[], int size)
{
// Function checks that the array is valid (containing more than all 0 values)
// Assumes:
// 'myArray' is an array
// 'size' is the size of the array

int check=0;

for(int i=0; i<size;i++)
{
if(myArray[i]>0)
check++;
}
if(check>0)
return true;
else
return false;
}
Last edited on
yeah I can't see where an ofstream has been declared.

I'm sure there's better way to do it but,
try something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char * fix_string(string str_ref);
int main()
{
    string s_new_text;
    s_new_text = "Whatever you want to write to the file.";
    char * cs_new_text;
    cs_new_text = fix_string(s_new_text);
    
    fstream fs_file("outfile.txt", ios::in | ios::out); // opening file this way
    fs_file.seekp(0,ios::end); // keeps application from erasing file's content..
    fs_file << '\n' << cs_new_text;
    fs_file.close();

    return 0;
}

char * fix_string(string str_ref)
{
    char * cst_ref = new char [str_ref.length()+1];
    strcpy(cst_ref, str_ref.c_str());
    return cst_ref;
}


everyone's gonna laugh at me and also say just use ios::app
but this is just an example

oh and yeah if all you need to write is numbers you can skip the string to c string bit
Last edited on
Thanks for the info but this is a little too advanced, lol...if you know what I mean for this class as that we haven't covered any of this sort of coding yet. I may see stupid to you to make such an observation, but he is a stickler.
Last edited on
Topic archived. No new replies allowed.