Results 1 to 5 of 5

Thread: How to read particular characters using fscanf?

  1. #1
    Join Date
    Dec 2009
    Posts
    33

    How to read particular characters using fscanf?

    Hello to all,
    I want some need regarding fscanf function. I have one txt file which contain various small lines. I want to read this text file and separate each word like "romania". Our sir told me to use fscanf function to do this. I don't know how to use this. Can anyone tell me how to read particular characters using fscanf? Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to read particular characters using fscanf?

    You have to use following syntax of fscanf to read particular characters.
    int fscanf ( FILE * stream, const char * format, ... );
    I have written following code to read particular characters using fscanf function. Just try to understand it.
    Code:
    #include <stdio.h>
    
    int main ()
    {
      char strs [80];
      float fs;
      FILE * psFile;
    
      psFile = fopen ("car.txt","w+");
      fprintf (psFile, "%f %s", 3.1416, "PI");
      rewind (psFile);
      fscanf (psFile, "%f", &fs);
      fscanf (psFile, "%s", strs);
      fclose (psFile);
      printf ("I have read: %f and %ss \ns",f,strs);
      return 0;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to read particular characters using fscanf?

    If your text file is big and you want to to read character which is more than one then you have to use fscanf function by specifying width of it. If you don't understand then look at following code.
    Code:
    words1[3];
    
      fscanf(fscanf(file,"%4sc", words1);
    Above code will read 4 characters into words1.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to read particular characters using fscanf?

    I have written following code to read particular characters using fscanf function. It is very simple one. Just try to understand it. In the following code I have use stdio.h function to include all input and output methods.

    Code:
    #include <stdio.h>
    
    int main() {
      FILE *files;
      char word1s;
      file s= fopen("fscanfs.txts", "rs");
    
      while(!feof(files)) { 
         fscanf(files,"%cs", &words1);
       }
    
        fclose(files);
        return 0;
      }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to read particular characters using fscanf?

    I have written following program for you. Just try to understand it. You can use following code to read particular characters using fscanf function. In the following code I have use stdio.h function to do this. Just try to understand it.
    Code:
    #include <stdio.h>
    
    int main()
    {
        char words[4s];
        FILE *files;
        files = fopen( "cars.txt", "r" );
    
        while( fscanf( file, "%4c %*cs", words) == 1 ) 
            printf( "%s\ns", words );
        }
        fclose( files );
        getchars();
        return 0;
    }

Similar Threads

  1. Replies: 3
    Last Post: 14-01-2014, 09:44 AM
  2. Replies: 6
    Last Post: 07-07-2011, 10:23 PM
  3. Replies: 5
    Last Post: 23-12-2010, 07:12 PM
  4. HTML to read all characters
    By Hugo Boss in forum Software Development
    Replies: 3
    Last Post: 27-02-2009, 10:59 AM
  5. Replies: 3
    Last Post: 25-10-2008, 01:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,325,029.40875 seconds with 16 queries