Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ help required

Options
  • 08-03-2010 10:39pm
    #1
    Registered Users Posts: 112 ✭✭


    Hi guys I need some help, I'm back in college and not having done programing for 9 years I have totally forgot what I'm doing.

    I need to write a header file and a main.cpp file. Needs to have class which opens a text file, numbers all the lines and saves it again under my own name.

    Sounds easy but I'm totally lost.

    Thanks a lot..


Comments

  • Registered Users Posts: 8,438 ✭✭✭RedXIV


    What in particular is stopping you?

    This is a good link to get you started


  • Registered Users Posts: 112 ✭✭beatman91


    Thanks I've seen this, its a really good site.

    Well three things, firstly thats all written in the main and thats the way I use to do it in C but I just can't get how you would write it in the header file. And the other is how would you number each line, I sure there isin't a command for this, I can't even see how a loop would do this.

    and finally any idea which command saves the file?

    I use to like machine code back in the day but this is just beyond me.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    i would use a while loop to loop through the file reading in each line as you loop.

    then pre-pend a number to the start of each line. then write that line back to the file, then increment that number for the next loop iteration.

    the above is probably not the most efficient method but it's the simplest.

    show us what you've got so far, we'll help you more if you show an effort.


  • Registered Users Posts: 112 ✭✭beatman91


    here is what I have so far but as you can tell it does not work so any help would be great.

    #include <iostream>
    #include <fstream>

    using namespace std;

    int main()
    {
    char inputfile [100];
    cout <<"Type File Path :"<<endl;
    cin >> inputfile;

    ifstream infile;
    ofstream outfile;


    inputfile.open(inputFile);

    if(outfile.is_open()){
    cout<<"output File is open"<<endl;
    outfile.close();
    }
    else{
    cout<<"error"<<endl;
    }
    infile.open ("name.txt", ofstream::out | ofstream::app);

    while(){

    outfile<<""; //this is the part I needs to number each line

    outfile.open("outputfile.dat");

    outfile.close();
    infile.close();

    return 0;
    }


  • Registered Users Posts: 112 ✭✭beatman91


    ok I figured it out


  • Advertisement
Advertisement