Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ help required

  • 08-03-2010 10:39PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2, Paid Member Posts: 8,548 ✭✭✭RedXIV


    What in particular is stopping you?

    This is a good link to get you started


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 26,449 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 112 ✭✭beatman91


    ok I figured it out


  • Advertisement
Advertisement