views:

352

answers:

8

I want to write a word to each line before all the words.

DATA.txt
{
ie. 

    Hello 
    my 
    name 
    is 
    steven

ie.

    1.hello
    1.my
    1.name
    1.is
    1.steven
}

IN FILE I/O to put the number 1 and a dot before all the words in the beginning of the line Ok i want it to find the string then ut the certain text "1. " before it i dont want to put it by myself i want it to do it automatically Sorry i did not specify

+3  A: 

I have no idea what this has to do with notepad. If you want to prepend each line in a text file with a string, simply

  1. read the file line-wise
  2. output the prefix to a new file
  3. output the line contents to the new file
  4. read next line and go to 2 until you reached the end of the file.

If this is not what you want you should ask more specifically.

bluebrother
I guess the notepad reference comes from the ".txt is the native application file format of the mighty notepad" idea. Many people see it this way.
balpha
A: 

I don't really understand the question.

However, you should choose which tool to use with care. Neither notepad nor C++ look like the right tool for the job you want. You should use sed, perl, or a good text editor (vim).

Gilad Naor
A: 

I think a brief refresher on standard I/O through cout statements is in order. You can print to the console via the "cout" function which is invoked in the following example:

cout << "I used a cout statement, lolz";

Similarly you can print a newline to the console via the newline character "\n" or using "<< endl" at the end of a "cout" statement

cout << "This is the newline character \n";
cout << "This is endl" << endl;

I hope this helps. Remember that in order to use cout you need to include the iostream library.

Nadewad
A: 

One posssible way to do it.

#include <fstream>
using namespace std;

int main()
{
    //use for a series of numbers
    int i=0;
    // or use if you want 1 number
    int j=1;

    ofstream out ("yourfile.txt", ios::app);

    //use j if you want to display 1 with dot (.)
    out << i++ << "." << "Hello" << endl;
    out << i++ << "." << "my" << endl;
    out << i++ << "." << "name" << endl;
    out << i++ << "." << "is" << endl;
    out << i++ << "." << "Steven" << endl;
}

EDIT: Same code with string!

#include <fstream>
#include <string>
using namespace std;

    int main()
    {
        string strText[] = {
         "Hello",
         "my",
         "name",
         "is",
         "steven"
         };

        //use for a series of numbers
        int i=0;
        // or use if you want 1 number
        int j=1;

        ofstream out ("yourfile.txt", ios::app);

        //use j if you want to display 1 with dot (.)
        out << i++ << "." << strText[0] << endl;
        out << i++ << "." << strText[1] << endl;
        out << i++ << "." << strText[2] << endl;
        out << i++ << "." << strText[3] << endl;
        out << i++ << "." << strText[4] << endl;
    }
Secko
I want it to find the string and place it before the text on each line
H4cKL0rD
You didn't specify this! :(
Secko
A: 

Just use a prefix variable:

#include <fstream>
using namespace std;

ofstream file;
string prefix = "1. ";

out_file.open("test.txt");

out_file << prefix << "some text" << endl;
out_file << prefix << "some other text" << endl;

out_file.close();

Or you could write a little convenience function to do this:

void writeLine(ofstream file, string text)
{
    file << "1. " << text << endl;
}

Certainly, there are more possibilites.

Michael Barth
+2  A: 

I hope the following code will be of help to you:

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

int main() {
    string line, new_content;
    string prefix   = "1.";
    char filename[] = "DATA.txt";

    // Read lines and prepare contents by prefixing lines with a prefix
    ifstream infile(filename);
    while ( infile >> line ) {
        new_content += prefix + line + "\n";
    }
    infile.close();

    // Write contents
    ofstream outfile(filename);
    outfile << new_content;
    outfile.close();

    return 0;
}
Alan Haggai Alavi
+1  A: 

This is a stab in the dark, but do you mean that it should read an existing text file to get the lines of text?

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

int main()
{
    std::ifstream inputFile("c:\\input.txt");
    std::ofstream outputFile("c:\\output.txt");

    std::string line;
    while (std::getline(inputFile, line))
        outputFile << "1." << line << std::endl;

    return 0;
}

Which means, open the input file and the output file, and then for each line read from the input file, write it to the output file with 1. in front of it.

Daniel Earwicker
+1  A: 

This is a perfect example of something that C++ should not be used for. If you were on Unix, you'd do

cat myFile.txt | sed 's/^/1./

On Windows, I am sure you can do something with bat files or the new shell (PowerSomething?). Please don't use C++ for this.

Arkadiy