sscanf help

This is a discussion on sscanf help within the C Programming forums, part of the General Programming Boards category; ive "sort of" written a function that prints lines from a file with line numbers i want modify this program ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    37

    sscanf help

    ive "sort of" written a function that prints lines from a file with line numbers
    i want modify this program so that when i input "ln2,5" it prints
    out lines 2 to 5..
    i know that i need sscanf to do this, but im not sure how it works, can anyone give me some hints.....


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    
    /*void linenumbers(Input){
      while( fgets (Input, 1024, fp) != NULL )
            {
                    
                    printf("%6d: %s", count++, Input);
    		
            }
            printf("\n");                                                                  
            fclose (fp);
            return 0;
    	}*/
    
    int main( int argc, char *argv[])
    {
      char szKey[] = "q\n";
      char input[1000];
      static char array [10];
     
      FILE *fp;
     
      if(argc != 2) {
        printf("Usage: 1021edlin filename\n");
        exit(0);
      }
     
      if(( fp = fopen(argv[1],"r")) == NULL) { //if file does not exist
        printf("New File\n"); //print "New File"
        do {
          printf ("> "); //print >
         fgets(input,100,stdin);
         } 
        while (strcmp (szKey,input) != 0); //if q is entered
        return 0; //exit
      }
     
      /* Seek to end of file*/
     
      if(fseek(fp,0L ,SEEK_END)) {
        perror("Cannot seek to end of file... \n");
        exit(0);
      }
      
      while(fgets(array,sizeof(array),fp)!=NULL) 
      printf("%s\n",array);
     
      /* Display current position of SPI */
     
      printf("%ld\n",ftell (fp));
      
      do {
         printf ("> ");
         fgets(input,100,stdin);
      } 
      
      while (strcmp (szKey,input) != 0);
      return 0;
         
    }

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    37
    so far, if i input q, it quits the program....

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is your input always going to take that format? ln#,#

    If so, you could always do something like:
    Code:
    sscanf( buf, "%*c%*c%d,%d", &var1, &var2 );
    The * skips the format. It basicly reads and discards whatever follows.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    37
    there are more inputs, thats just one of them

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    37
    oh but for ln yes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and string handling question
    By ursula in forum C Programming
    Replies: 14
    Last Post: 05-30-2009, 02:21 AM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 05:42 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. sscanf question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 07:52 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM

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