fscanf() or gets()

This is a discussion on fscanf() or gets() within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char x = 's'; int a, sum = 0; ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    fscanf() or gets()

    Code:
    #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char x = 's'; int a, sum = 0; FILE *ifp, *ofp; /* input file pointer,output file pointer */ ifp = fopen("c:\\test1.txt", "r"); /* open for reading */ ofp = fopen("c:\\test2.txt", "w"); /* open for writing */ //while(ifp != NULL) { x = fscanf (ifp,"%c" ); } printf("%c",x); fclose(ifp); fclose(ofp); return 0; }
    I want in the while loop to read all this lines.
    4.0 6.0
    5.0 6.0
    7.0 8.0

    How can I read 40 then 6.0 then 5.0 and then 6.0..and how does fscanf work..

    thanks I am new to c

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    something like
    Code:
    	double d;
    	while(fscanf (ifp,"&#37;lf",&d ) == 1)
    	{
    		printf("Number read = %f",d);
    	}
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    Code:
    while(ifp != NULL) { x = fscanf (ifp,"&#37;c" ); printf("%c",x); ifp++; }
    doing that isnt working

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    can you explain fscanf (ifp,"&#37;lf",&d ) == 1
    what does that mean...
    the fscanf method..thanks alot

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    &#37;lf - format specifier of fscanf for reading doubles
    &d - pointer to the double var where fscanf will store the parsed data

    ==1 : fscanf returns number of successfully parsed items (in the sample it should be 1) so only in this case we can process the value of d
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    How can I read first two numbers? then read the other ones in the while loop?

    Thanks for all help.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    no problem solved it, learning c hehe.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Just to beat it into your memory, I'll say it:

    Don't use gets().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 03:52 PM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 06:24 AM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

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