C Board  

Go Back   C Board > Community Boards > A Brief History of Cprogramming.com

 
 
LinkBack Thread Tools Display Modes
Old 06-22-2004, 10:49 PM   #1
CS Author and Instructor
 
Join Date: Sep 2002
Posts: 511
Adding Line numbers in Word

Hello All,

Currently on another project-will answer those AP questions later this week.

Have a problem.

In VS .NET I can add line numbers to the code, but when I copy and paste this code with line numbers into Word, the line numbers are not there.

How can I keep these line numbers to stay in Word?

PS. I know you can suppress line numbers/add line numbers in Word itself. I tried it via help. It is not easy to do, especially with multiple places in the document to add line numbers to.

Thoughts appreciated.

Also, will let you know about the little project I am working on at a later date.

Mr. C.
__________________
Mr. C: Author and Instructor
Mister C is offline  
Old 06-23-2004, 06:08 AM   #2
5|-|1+|-|34|)
 
ober's Avatar
 
Join Date: Aug 2001
Posts: 4,429
Try pasting it into notepad or wordpad first. Those programs won't try to manipulate your text before showing it to you.
ober is offline  
Old 06-23-2004, 06:40 AM   #3
Registered User
 
anon29's Avatar
 
Join Date: Jun 2004
Posts: 1
Post title

Hello, this post was made with a 3rd party application for testing reasons.
anon29 is offline  
Old 06-23-2004, 06:57 AM   #4
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
Are you sure that when you copy from VS.NET it is actually copying the line numbers? To me it would make more sense for the line numbers to be added by the IDE and not really be text. But of course I don't have VS.NET so I don't know for sure
Thantos is offline  
Old 06-23-2004, 07:26 AM   #5
CS Author and Instructor
 
Join Date: Sep 2002
Posts: 511
Quote:
Are you sure that when you copy from VS.NET it is actually copying the line numbers? To me it would make more sense for the line numbers to be added by the IDE and not really be text. But of course I don't have VS.NET so I don't know for sure
That what it is doing. It is there for printing purposes, not to the text. I guess I will have to add the line numbers in word.
__________________
Mr. C: Author and Instructor
Mister C is offline  
Old 06-23-2004, 07:41 AM   #6
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
Does VS.NET have a print to file option? And could word open that file? Just throw out ideas
Thantos is offline  
Old 06-23-2004, 08:38 AM   #7
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>I guess I will have to add the line numbers in word.
And you call yourself a programmer. Write a script to do it for you.
Code:
#!/usr/bin/perl
#
# nladd
#   Add line numbers to a text file

use warnings;
use strict;

open LINES_FILE, ">lines" or die;

my $line_count = 1;

while (<ARGV>) {
  print LINES_FILE "$line_count $_";
  $line_count++;
}
__________________
My best code is written with the delete key.
Prelude is offline  
Old 06-23-2004, 09:03 AM   #8
5|-|1+|-|34|)
 
ober's Avatar
 
Join Date: Aug 2001
Posts: 4,429
Wouldn't that be techically screwed up if a certain line wraps? Unless when you copy it from the IDE, it carries along any new line characters.
ober is offline  
Old 06-23-2004, 09:08 AM   #9
l'Anziano
 
DavidP's Avatar
 
Join Date: Aug 2001
Posts: 2,630
or with PHP:

Code:
<?

$a_lines = file("sourcefile.cpp");
$line_count = count($a_lines);

$fp = fopen('sourcefile.cpp', 'w');

for ( $i = 0; $i < $line_count; $i++ )
{
	$content = "$i\t$a_lines[$i]";
	fwrite($fp, $content);
}

fclose($fp);

?>
__________________
My Website

"Circular logic is good because it is."
DavidP is offline  
Old 06-23-2004, 09:10 AM   #10
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>Wouldn't that be techically screwed up if a certain line wraps?
Not enough to warrant a fix for the simple use the script was designed for.
__________________
My best code is written with the delete key.
Prelude is offline  
Old 06-23-2004, 01:43 PM   #11
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,700
@Mister C
Why don't you write yourself a nice little VBA macro in word, which you pass to it the name of the .c source file you want to quote from, and the range of lines you want to import, and it does all the hard work for you.

Even better, why not add magic comments to the 'C' source code so you don't have to refer to line numbers at all, but can refer instead to named sections of the code.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline  
Old 06-23-2004, 06:30 PM   #12
Crazy Fool
 
Perspective's Avatar
 
Join Date: Jan 2003
Location: Canada
Posts: 2,596
first get a real operating system , then do this

Code:
grep -n .\* in-file > out-file
Perspective is offline  
Old 06-23-2004, 10:13 PM   #13
Registered User
 
linuxdude's Avatar
 
Join Date: Mar 2003
Location: Louisiana
Posts: 926
behold the many powers of grep. Don't you just love linux?

Last edited by linuxdude; 06-23-2004 at 10:19 PM.
linuxdude is offline  
Old 06-23-2004, 10:55 PM   #14
Yes, my avatar is stolen
 
anonytmouse's Avatar
 
Join Date: Dec 2002
Posts: 2,544
Here is the result of some procrastination:
Code:
Sub ImportCode()
    Dim xlApp As Object, fso As Object, tstream As Object
    Dim nStart As Integer, nEnd As Integer, nLine As Integer
    Dim strLine As String
    
    Set xlApp = CreateObject("Excel.Application")
    strFile = xlApp.GetOpenFilename("C/C++ Source Files (*.c; *.cpp),*.c;*.cpp")
    xlApp.Quit
    
    If (strFile = False) Then Exit Sub
    nStart = CInt(InputBox("Please enter the start line.", "Start", 0))
    nEnd = CInt(InputBox("Please enter the end line.", "End", 999))
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set tstream = fso.OpenTextFile(strFile)
    
    While (Not tstream.AtEndOfStream)
        strLine = tstream.ReadLine
        
        If (nLine >= nStart And nLine <= nEnd) Then
            Selection.Font.Color = wdColorBlueGray
            Selection.TypeText Format(nLine, "#000")
            Selection.Font.Color = wdColorAutomatic
            Selection.TypeText "   " & strLine & vbCrLf
        End If
        
        nLine = nLine + 1
    Wend

End Sub
Paste it into the VBA project and have fun.
anonytmouse is offline  
Old 06-24-2004, 05:15 AM   #15
erstwhile
 
Join Date: Jan 2002
Posts: 2,223
Quote:
Originally Posted by Perspective
first get a real operating system , then do this

Code:
grep -n .\* in-file > out-file
That'll work in windows, too - with msys (includes a port of grep among others).
__________________
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
Ken Fitlike is offline  
 

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Seg Fault in Compare Function tytelizgal C Programming 1 10-25-2008 03:06 PM
brace-enclosed error jdc18 C++ Programming 53 05-03-2007 05:49 PM
adding odd numbers only CheyenneWay C++ Programming 12 05-06-2004 12:22 AM
the definition of a mathematical "average" or "mean" DavidP A Brief History of Cprogramming.com 7 12-03-2002 11:15 AM
Line Numbers in VI and/or Visual C++ :: C++ kuphryn C++ Programming 2 02-10-2002 10:54 PM


All times are GMT -6. The time now is 07:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22