Adding line numbers to file

This is a discussion on Adding line numbers to file within the C Programming forums, part of the General Programming Boards category; I want to write a program in which 1.Number the lines in a file 2.Display n lines from a given ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Adding line numbers to file

    I want to write a program in which
    1.Number the lines in a file
    2.Display n lines from a given line number.
    3. Delete a particular line
    4. Search for a given word and print all the lines where the word is found
    i want to know what will be basic idea behind it after opening any file??

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    File I/O is your friend. Sounds like the getline() function will be super handy.

    1) Do you mean modify the existing file with line numbers? If so you might want to getline() then 'add' the line number (kept in order with an indexing integer say in a for loop), and then write that back into the file.

    2)
    Code:
    for NUM_LINES_TO_DISPLAY
        getline(temp, 256)
        cout temp
    3) Index the lines by 1,2,3,4,5... then scan down to the line you want to delete?

    4) Since you can't sort the lines in a file because I'm assuming there is no structure to the data in each line, you'll have to do a linear search.

    Code:
    for each line
       getline()
       for each word
          if tempword = key
              //the word does reside in this line!

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    11
    cool, thx.....let me try to write this down

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    getline/cout are C++, this is the C board.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    11
    so what will be function for c like getline

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    fgets
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you also will need other functions for working with C-strings like strcmp and strstr
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 01:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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