Thread: line numbers

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

    line numbers

    Hey guys i tried doin this but still dosnt work. What im actually trying to do is read the file line by line then list numbers next to each line then print it to standard out. Sorry for double posting unsure if anyone seen my reply


    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(fscan(stdout, "%d", input_char)!=NULL)
        {
    
    
              fprintf(fp, "%s", input_char);
              line_no++;
    
        }
    
           
        
    
        fclose(fp);
    
    
    
        return EXIT_SUCCESS;
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Try this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv) {
        int line_no = 1;
        char line[256];
        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*/
        }
        while(fgets(line, 256, fp)!=NULL) {
              printf("%03d %s", line_no, line);
              line_no++;
        }
    
        fclose(fp);
        return EXIT_SUCCESS;
    }
    Kurt

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Yehh i tried it that way and it seems to work but is there a way to read each line as an int and not as a char[256]. Because im giving this code as int input_char and unfortunately the guidlines say that i cant change the original variables to anything else.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Please don't create 2 threads about the same thing. Hopefully a moderator will close this one.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    maybe this way ?
    Code:
        input_char = 0;
        int nl = 1;
        while(input_char !=EOF) {
              if ( nl ) {
                 printf("\n%03d ", line_no);
                 line_no++;
    	  }
    	  else 
    	    putc(input_char, stdout);
    	  input_char = getc(fp);   
    	  nl = input_char == '\n';
        }
    Kurt

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or you could just keep doing his homework for him...
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with reading numbers from the command line
    By Nterpol in forum C Programming
    Replies: 4
    Last Post: 06-01-2006, 01:40 AM
  2. Help displaying line numbers of a text document
    By Japatron in forum C Programming
    Replies: 4
    Last Post: 05-04-2006, 01:34 PM
  3. Showing Line Numbers in MSVC++
    By drdroid in forum Windows Programming
    Replies: 1
    Last Post: 01-09-2003, 03:23 PM
  4. Specifying Line Numbers
    By drdroid in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2002, 11:00 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM