I wrote this a little while ago... I know i need to add a file check so overwrite does not occur but I am interested in learning to write clearer and less amatuer code. Any serious note would help...

this program creates a file with an input name, saves text, and opens the file after being created/saved in that order just to simplify error checking...


C++ saver
------------------------------------

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

char something[256];
char text[256];
char text1[256];
char filetext[256];
char filename[256];
string line;
string write1 = "write";
string open1 = "open";


int main(){
    
    cout << "Command... write?\n";
    cin.getline (text, 256);

    if (text == write1){
         cout << "\nEnter text to be saved.\n";
         cin.getline (filetext, 256);
    }

    cout << "\n";
    
    cout << "Enter filename...\n";
    cin.getline  (filename, 256);
    
    ofstream myfile;
    myfile.open (filename);
    myfile << filetext;
    myfile.close();

    cout << "\nCommand... open?\n";
    cin.getline (text1, 256);
    
    if (text1 == open1){
         ifstream myfile(filename);
         getline (myfile, line);
         cout << "\n" << line << "\n\n";
         myfile.close();
    }
    
    system("PAUSE");
return 0;
}

------------------------------------------

thank you

looks ok to me, except in the cin.getline() lines you should use sizeof instead of hardcoding the number cin.getline (text1, sizeof( text1 ) ); And you could declare a const int to define 256 instead of hardcoding it everywhere.

writting a better code is not difficult until and unless you think that you will not avoid the things you know and well understand the problem.

first of all this is not an OOP.
if you are willing to write OO Program you should think in terms of objects and their relationship.

lets optimize your code.

char something[256];
char text[256];
char text1[256];
char filetext[256];
char filename[256];
string line;
string write1 = "write";
string open1 = "open";

1) always declare the variable near the point where they should be used.
making all the variable global doesn't make sense.
2) make variable constant if they will not change through out the program life. don't use obscure name like write1 or open1.

const string WRITE = "write";

3. Don't use "\n" rather than use std::endl ? why search about it. what if you compile where newline is is defined as "\r\n"
4. Magic Numbers problem as mentioned by Anciant Dragon.

5. check if the stream is_open(..).

6. make proper functions to do read and write.

Hope this helps.

3. Don't use "\n" rather than use std::endl ? why search about it. what if you compile where newline is is defined as "\r\n"

It doesn't matter because the compiler will make the conversion appropriate to what is required by the file system. On MAC the '\n' is converted to '\r' before saving to the file. MS-Windows/MS-DOS its converted to "\r\n". Other operating systems could be almost anything. Its been standard practice every since C was invented a million years ago to use '\n'.

I rarely use std::endl any more because it also flushes the file system, which can be time consuming when writing a lot of lines to the file.

This article has been dead for over six months. Start a new discussion instead.