reading a text file printing line number

This is a discussion on reading a text file printing line number within the C Programming forums, part of the General Programming Boards category; Hi guys i have done the following code and basically im trying to read a file and put line numbers ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    reading a text file printing line number

    Hi guys i have done the following code and basically im trying to read a file and put line numbers to every line in the file. How would i read it in line by line then putting line numbers next to it. I have to use an Int for the lineNo's and int input_char variables i cant change them so im having difficulty finding a correct function to read them line by line which takes an int and not a char.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
    
        int line_no = 1;
        int input_char;
        FILE *fp;
    
        if(argc !=2) /* check for 2 arguments*/
        {
    
          fprintf(stderr, "invalid usage :%s\n", argv[0]);
          return 1; /* stop processing if failes */
    
        }
    
        if((fp = fopen(argv[1], "r")) == NULL) 
        /* opens file for reading and checks if it exists */
        {
         
         fprintf(stderr, "invalid usage: %s\n", argv[0]);
         return 1; /* stop processing if fails*/
    
        }
    
              printf("dude\n");
    
        while(fgetc(fp)!=NULL)
        {
    
    
              fprintf(fp, "%s", input_char);
              line_no++;
    
        }
    
           
        
    
        fclose(fp);
    
    
    
        return EXIT_SUCCESS;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > while(fgetc(fp)!=NULL)
    Read up on
    - what fgetc() returns
    - how to assign a value to say input_char

    > fprintf(fp, "%s", input_char);
    Read your printf manual page again.
    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
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    while(fgetc(fp)!=NULL)
    Look up the return value of fgetc. You might want to store the character returned in your input_char if you plan to write it.
    Code:
    fprintf(fp, "%s", input_char);
    %s is used with a string, not an int. Use %c to print a character. Trying to write to the file you are reading from is not a good idea, though.
    Last edited by Dave_Sinkula; 09-16-2005 at 09:38 AM. Reason: D'oh! Pokey again!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    So what im basically tryin to do is read the file using an int variable input_char then listing line numbers on each then print it out to standard out.

    eg
    1 abc
    2 abc
    3 abc

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    The people that replied understand what you're trying to do. They're waiting for you to fix the issues they pointed out and for you to make a sincere attempt at doing it first.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text file by line number
    By whiskedaway in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2009, 11:09 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  4. Reading a line from a text file Please help
    By Blizzarddog in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2003, 01:35 PM
  5. 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

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