white space and fscanf

This is a discussion on white space and fscanf within the C Programming forums, part of the General Programming Boards category; Is it possible to get 'fscanf' to read past or ignore whitespace characters??? I need to get a file into ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    35

    Question white space and fscanf

    Is it possible to get 'fscanf' to read past or ignore whitespace characters???

    I need to get a file into an array, but I only get the first few characters up to whitespace. This is what I have so far.

    <code>

    main()
    {
    char buffer[200] ;
    FILE *infile ;
    FILE *outfile ;

    infile = fopen( "input.txt", "r" ) ;
    outfile= fopen( "output.txt","w" ) ;

    fscanf( infile, "%s", buffer ) ;
    fprintf( outfile, "%s", buffer ) ;

    fclose(infile);
    fclose(outfile);

    }

    </code>

    As you can see, I have not implemented the array aspect yet, because my 'output.txt' file only gives me characters up to the first whitespace, instead of the complete file.

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Try using fgets(s,n,file) instead of fscanf(). This will read in a maximum n-1 characters into array s or up to a newline. Use a loop to read multiple lines. fgets() returns NULL when end of file (or error) occurs, so you could use that to terminate your loop.
    Something like:
    Code:
    #include <stdio.h>
    
    #define BUFFER_SIZE 200
    
    int main(void)
    {
      char buffer[BUFFER_SIZE] ;
      FILE *infile ;
      FILE *outfile ;
    
      infile = fopen( "input.txt", "r" ) ;
      outfile= fopen( "output.txt","w" ) ;
    
      while (fgets( buffer, BUFFER_SIZE, infile ) != NULL)
        fprintf( outfile, "%s", buffer ) ;
    
      fclose(infile);
      fclose(outfile);
    
      return 0
    }
    Have a look at the programming FAQ page of this site for more info.
    Also note that main() should return int.
    Last edited by DavT; 06-10-2003 at 04:45 AM.
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    35

    thanks

    Thanks, DAvT.

    I will give this a try after looking at the FAQ...

    Peace

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 11:44 AM
  2. Help with struct... not returning correct results.
    By drty2 in forum C Programming
    Replies: 7
    Last Post: 01-18-2009, 11:25 PM
  3. fscanf Issue
    By Maser in forum C Programming
    Replies: 3
    Last Post: 04-23-2007, 08:42 PM
  4. fscanf problem
    By srtonline in forum C Programming
    Replies: 4
    Last Post: 11-21-2006, 11:39 AM
  5. fscanf() and fgetc()
    By MadStrum! in forum C Programming
    Replies: 6
    Last Post: 01-23-2003, 09:36 AM

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