# Line numbering program

**URL:** <https://boards.straightdope.com/t/line-numbering-program/234895>\
**Category:** Factual Questions\
**Created:** [March 17, 2004, 7:53pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895 "2004-03-17T19:53:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Electronic\_Chaos](https://avatars.discourse-cdn.com/v4/letter/e/bbe5ce/32.png) [@Electronic\_Chaos](https://boards.straightdope.com/u/Electronic_Chaos)\
**Post date:** [March 17, 2004, 7:53pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/1 "2004-03-17T19:53:30Z")

</div>

I’m working on a project which requires a large list of words, each to be numbered (No, this doesn’t have to do with hacking, I’m making a random poetry engine). I was just wondering if there was any program that would take a text file and add a number to each line just so I don’t have to do it all by hand, or if not, could someone help me figure out how to make one in C/C++.

---

<div class="post-metadata">

**Author:** ![Gfactor](https://avatars.discourse-cdn.com/v4/letter/g/9de053/32.png) [@Gfactor](https://boards.straightdope.com/u/Gfactor)\
**Post date:** [March 17, 2004, 8:21pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/2 "2004-03-17T20:21:14Z")

</div>

> [@xgxlx](#):
>
> I’m working on a project which requires a large list of words, each to be numbered (No, this doesn’t have to do with hacking, I’m making a random poetry engine). I was just wondering if there was any program that would take a text file and add a number to each line just so I don’t have to do it all by hand, or if not, could someone help me figure out how to make one in C/C++.

Word does it. So does NoteTab.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [March 17, 2004, 8:22pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/3 "2004-03-17T20:22:17Z")

</div>

Well, I’m the hacker who’s going to help you.

In C++, 'cause C isn’t as nice for this kind of work:

```auto

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

using namespace std;

int main(int argc, char *argv[])
{
   vector<string> v;
   string s;
   ifstream file;

   if (argc < 2) {
      cout << "usage: linenum file" << endl;
      return 1;
   }

   if (!file.open(argv[1])) {
      cout << "Can't open file " << argv[1] << endl;
      return 1;
   }

   while (s << file)
      v.push_back(s);

   for (int i = 0; i < v.size(); ++i)
      cout << i + 1 << " : " << v*;

   file.close();

    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![Gfactor](https://avatars.discourse-cdn.com/v4/letter/g/9de053/32.png) [@Gfactor](https://boards.straightdope.com/u/Gfactor)\
**Post date:** [March 17, 2004, 8:25pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/4 "2004-03-17T20:25:16Z")

</div>

You can download Note Tab for free [here](http://www.notetab.com/).

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [March 17, 2004, 8:32pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/5 "2004-03-17T20:32:47Z")

</div>

Jeez. There’s no reason to use C++ for such a simple job:

```auto

perl -pi -e 'print "$.: "' < input.txt > numbered.txt

```

Easy as pie.

---

<div class="post-metadata">

**Author:** ![Metacom](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Metacom](https://boards.straightdope.com/u/Metacom)\
**Post date:** [March 17, 2004, 8:44pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/6 "2004-03-17T20:44:47Z")

</div>

Ewww, perl. 😉

```auto

awk '{ print NR " " $0 }' < source_file > destination_file

```

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [March 17, 2004, 11:26pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/7 "2004-03-17T23:26:36Z")

</div>

Not everyone is lucky enough to have a Perl or awk interpreter. I assumed the OP just had a C++ compiler (probably one of the MS variants) and was running a Win32 system with none of the stuff that makes a \*nix command line so nice.

If it had been my system (Fedora Core 1, at the moment), I’d have used awk out of force of habit. 😉

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [March 17, 2004, 11:36pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/8 "2004-03-17T23:36:18Z")

</div>

Oh, and my code is broken.

Replace the ‘while (s \<\< file)’ with ‘while (getline(file, s))’ and the ‘cout \<\< i + 1 \<\< " : " \<\< v\*;’ with ‘cout \<\< i + 1 \<\< " : " \<\< v\* \<\< endl;’ and it should all work fine.

That’s what you get for using C++ to do Perl’s job. 😃

---

<div class="post-metadata">

**Author:** ![Pleonast](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pleonast/32/1183_2.png) [@Pleonast](https://boards.straightdope.com/u/Pleonast)\
**Post date:** [March 17, 2004, 11:43pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/9 "2004-03-17T23:43:50Z")

</div>

How about a grep:

```auto

grep "" -n input.txt > output.txt

```

---

<div class="post-metadata">

**Author:** ![John\_T.Conklin](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/john_t.conklin/32/3307_2.png) [@John\_T.Conklin](https://boards.straightdope.com/u/John_T.Conklin)\
**Post date:** [March 17, 2004, 11:45pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/10 "2004-03-17T23:45:38Z")

</div>

**C++** , **perl** , and **awk** all seem like overkill when plain old **cat** ’s _-n_ flag will do the trick.

```auto

cat -n < input.txt > output.txt

```

---

<div class="post-metadata">

**Author:** ![Pleonast](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pleonast/32/1183_2.png) [@Pleonast](https://boards.straightdope.com/u/Pleonast)\
**Post date:** [March 17, 2004, 11:52pm UTC](https://boards.straightdope.com/t/line-numbering-program/234895/11 "2004-03-17T23:52:08Z")

</div>

I think **John T. Conklin** wins the prize. Although to be even more pedantic, don’t actually need the input redirection.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [March 18, 2004, 1:02am UTC](https://boards.straightdope.com/t/line-numbering-program/234895/12 "2004-03-18T01:02:48Z")

</div>

Yep, **John** wins. I had forgotten about that option to cat.
