Fscanf (very short code)

This is a discussion on Fscanf (very short code) within the C Programming forums, part of the General Programming Boards category; Code: int n; double j; FILE *fp= fopen(argv[1],"r"); fscanf(fp,"%d",&n); printf("There are %d numbers", n); So basically im extracting data from ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    135

    Fscanf (very short code)

    Code:
     
    int n;
    double j;
    
    
    
     FILE *fp= fopen(argv[1],"r");
    
           fscanf(fp,"%d",&n);
           printf("There are %d numbers", n);
    So basically im extracting data from a text file.
    This is the text file:
    (1st line) 5
    (2nd line) 4.34 23.4 18.92 -78.3 17.9.

    When i attempt to print the n amount of numbers, somehow the program prints the doubles instead. Whats wrong?

    It has something to do with my program attempting to read the 2nd line as ints..
    Last edited by tmac619619; 10-28-2012 at 01:22 PM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    589
    Im not quite sure, but I think the first number in your file represents the number of arguments. Someone correct me if im wrong please.

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Can you reduce your problem to a small program (10-20 lines) and post that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I make this code short by for looping?
    By thisbeme in forum C Programming
    Replies: 8
    Last Post: 03-11-2011, 04:41 AM
  2. Short Speech Code :)
    By KathySbeat in forum C# Programming
    Replies: 4
    Last Post: 08-20-2010, 05:55 AM
  3. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-23-2006, 11:46 PM
  4. help with this short code on my quiz..
    By vtlo99 in forum C++ Programming
    Replies: 2
    Last Post: 03-01-2002, 07:17 AM
  5. how will this short c++ code look in pure c?
    By dune911 in forum C Programming
    Replies: 8
    Last Post: 10-19-2001, 11:21 AM

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