reading the next line using fscanf

This is a discussion on reading the next line using fscanf within the C Programming forums, part of the General Programming Boards category; the first two lines in a file are W1 44444 WingThing 20 W1 2222 SmallThing 8 why in the last ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    reading the next line using fscanf

    the first two lines in a file are
    W1 44444 WingThing 20
    W1 2222 SmallThing 8
    why in the last fscanf i dont get
    a=W1
    b=2222
    c=SmallThing
    ??
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define false 0
    #define true 1
    
     
      
    int main()
    {
    
    	FILE *f;
    	
    	char a[20];
    	char b[20];
    	char c[20];
    	f=fopen("c:\\ware1.txt","r");
    	fscanf(f,"%20s%20s%20s",a,b,c);
    	fscanf(f,"100[^\n]",a);//put in "a" the un wanted chars till you get \n
    	fgetc(f);  \\go to the next line
    	fscanf(f,"%20s%20s%20s",a,b,c);
    	fclose(f);
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by transgalactic2 View Post
    the first two lines in a file are
    W1 44444 WingThing 20
    W1 2222 SmallThing 8
    why in the last fscanf i dont get
    a=W1
    b=2222
    c=SmallThing
    ??
    You have 4 items to be scanned in. Might be three strings and one integer, I'm not sure about the second item.

    Code:
    	fscanf(f,"%18s %18s %18s %d",a,b,c, &d);
    Should be all you need.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    no i want only the first 3
    in my fscanf i take only 3 strings
    there is no integer

    i showed in my comment the logic i am using
    where is the mistake?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Then just put a * before the d in the fscanf() line, and that should work. I haven't done that, however.

    What I do is scan the item in and assign it to "gar" (short for garbage), and then just ignore it. Gar gets re-used for different items like that.

    But don't Rube Goldberg the whole fscanf() stuff with multiple lines of it, unless you need it.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Adak View Post
    Then just put a * before the d in the fscanf() line, and that should work. I haven't done that, however.

    What I do is scan the item in and assign it to "gar" (short for garbage), and then just ignore it. Gar gets re-used for different items like that.

    But don't Rube Goldberg the whole fscanf() stuff with multiple lines of it, unless you need it.
    %*d is more elegant solution
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    but i dont have the 4th parameter d in my fscanf


    i only scanf for 3 strings
    ??

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have to let fscanf() move past that 4th item in each line, somehow.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what??
    how do i stumble on the 4th item in each line??
    this part by all laws should point the start of the next line
    Code:
    fscanf(f,"100[^\n]",a);//put in "a" the un wanted chars till you get \n
    	fgetc(f);  \\ points  at \n and fgetc points to the next line

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have no data type for input of a, so nothing will work there.

    I'm not sure if my compiler will give me a warning about having no variable for fgetc(), or not. If it works for you, OK. That first line has to be fixed though, no doubt about it.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so why its working for the first line
    ??

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in the first line i copied the first 3 strings perfectly

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You probably have a c or something for the data type in the actual code, and just missed it for your post.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    nope
    this is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define false 0
    #define true 1
    
     
      
    int main()
    {
    
    	FILE *f;
    	
    	char a[20];
    	char b[20];
    	char c[20];
    	f=fopen("c:\\ware1.txt","r");
    	fscanf(f,"%20s%20s%20s",a,b,c);
    	fscanf(f,"100[^\n]",a);
    	fgetc(f);
    	fscanf(f,"%20s%20s%20s",a,b,c);
    	fclose(f);
    	return 0;
    }

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by transgalactic2 View Post
    so why its working for the first line
    ??
    It's not obvious? The first line is the first line. You scan in three items. Now there is another item in the buffer you didn't scan, which throws off the next line.

    Since each line is 4 items long but you scan 3 items, you will come back into sync on every 4th line.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i solved it like this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define false 0
    #define true 1
    
     
      
    int main()
    {
    
    	FILE *f;
    	
    	char a[20];
    	char b[20];
    	char c[20];
    	f=fopen("c:\\ware1.txt","r");
    	fscanf(f,"%20s%20s%20s",a,b,c);
    	while(fgetc(f)!='\n')
    	{
            fgetc(f);
    	}
    	 
    	fscanf(f,"%20s%20s%20s",a,b,c);
    	fclose(f);
    	return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-21-2006, 12:17 AM
  2. Replies: 6
    Last Post: 04-28-2006, 01:06 PM
  3. reading a file line by line
    By marsudhir in forum C Programming
    Replies: 1
    Last Post: 04-16-2003, 09:14 AM
  4. file reading line by line
    By Jerry & Tom in forum C++ Programming
    Replies: 1
    Last Post: 01-17-2003, 02:47 PM
  5. Reading a line including blanks
    By Ec4U2du in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 07:32 PM

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