Text File Help

This is a discussion on Text File Help within the C Programming forums, part of the General Programming Boards category; I am trying to copy a text file to another file, but inserting a line number at the beginning of ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Wink Text File Help

    I am trying to copy a text file to another file, but inserting a line number at the beginning of each line.

    an example of this...
    original file:
    Hello C Programming Board
    I need your help with this.
    Thank you in advance.

    new file:
    1. Hello C Programming Board
    2. I need your help with this.
    3. Thank you in advance.

    I know how to copy file to file, but don't know where to start when it comes to inserting a number before each line. My theory is to create a variable such as a numCount. Everytime I run into a \n I can fprintf numCount and then numCount++. I am not even sure if it's fprintf.
    If someone can guide me through this I would greatly appreciate it. Thanks again!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Seems about right, though I would suggest you use fgets() to read a whole line at a time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    to expand on this, using an If statement should do the trick?
    how exactly would i lay this down in coding itself?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    How about starting with the basics, namely a program which simply copies the input file to the output file one line at a time?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 02:13 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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