Hi all-
Looking for someone to critique my code here. My objective is to write a program that opens a given file ("input.txt"), writes the file's contents to 3 dynamicallly allocated arrays (A, B, C), and create a file ("output.txt"). I wrote this while referencing the C++ Language Tutorial of this website (mostly Control Functions, Dynamic Memory, & Input/Output with Files).
I am least sure about my implementation of the arrays; ie, reading data from the opened input.txt & subsequently writing to output.txt.
Disclaimer-
I don't have internet access on my PC, & therefore cannot get a compiler. Hoping to locate a public computer with one this weekend.
Your time & feedback are appreciated.
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
|
//basic file operations
#include <iostream>
#include <ifstream>
#include <ofstream>
int main ( ) {
ifstream myfile;
myfile.open ("input.txt");
if(!myfile) {
cerr<<"Error: file could not be opened"<< endl;
exit(1);
}
else if (myfile.is_open() ) {
int n; /*declaring n*/
public: int Read(); /*Reads the next character from the input stream and advances the character position by one character. How do I assign this character value to int n?*/
int A [n]; /*arrays of size n?*/
int B [n];
int C [n];
for (int n=1; n>0; n++) /*initial n = 1, while n > 0, increment n*/ {
int x[ ];
int y[ ];
int z [ ];
A[n-1] = int x[n];
B[n-1] = int y[n];
C[n-1] = int z[n];
}
}
myfile.close( ); /* the input.txt file*/
return 0;
ofstream myfile;
myfile.open (“output.txt”);
myfile << int n << endl;
for (int n = 0; n < int n; n++) {
myfile << A[n]<<B[n]<<C[n]<<endl;
}
result = sub ( n, A, B, C);
myfile << result;
myfile.close ( );
return 0;
}
|
edit:The following conditional:
1 2 3 4
|
if(!myfile) {
cerr<<"Error: file could not be opened"<< endl;
exit(1);
}
|
Was found in another thread.
edit: Code tags added. (Thanks, Hutch.)