Thread: fgets/sccanf question

  1. #1
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    fgets/sccanf question

    I am trying to read some info from a file. The text in the file in saved in the following format:

    [String]TAB[Hex Number]TAB[Integer]TAB[Float]

    Blahbllah Blahlahhhuhuhuhuh 3060 8 0.987676
    Hhuhujinjhjbgjvgbj 6788 4 3.22245
    Ghub Blahuahu Blhauhi Bujoj D011 4 7.8889

    All I am trying to do is assign the numbers to variables - one hex, one int and one double.

    Here are my problems:
    - I am using fgets to get each line of the file and it just eliminates all TAB spaces between words and numbers so line two of the above file looks like this in my buffer:
    Hhuhujinjhjbgjvgbj678843.22245

    - If I change the TABS to white spaces fgets works, but I have failed at trying to read just the numbers because the string part is variable in length and number of words.

    How do I go about using fgets with TABS and sscanf to read just the numbers?

    I tried using every function mentioned in this: http://docs.roxen.com/pike/7.0/tutorial/strings/sscanf

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by doia View Post
    Here are my problems:
    - I am using fgets to get each line of the file and it just eliminates all TAB spaces between words and numbers so line two of the above file looks like this in my buffer:
    Hhuhujinjhjbgjvgbj678843.22245
    Um, you are doing something wrong -- post the actual code. I use fgets() alot, it does not ignore tabs.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    Here is the code, paramacquire is just a BOOL that triggers reading the file.


    Code:
    FILE* fp3;
    
    fp3 = fopen ("offsets_list.txt","r");
    char buffer2[300];
    char *find_ptr, *offset_ptr;
    
    while(paramacquire)
    		{
    			fgets(buffer2, 300,fp3);
    			offset_ptr = buffer2;
    
    			if ((find_ptr = strstr(offset_ptr,paramname)))
    			{
    			int j,k;
    			double l;
    			sscanf(offset_ptr,"%*s %*s %x %d %lf",&j, &k, &l);
    			paramacquire=0;
    			}
    		 }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's not always clear, but " " in a scanf format string doesn't mean " ". Any whitespace will stop %s, whether it's spaces or tabs, and " " means any whitespace, not just an actual space character. If you really want "not a tab", then say so:
    Code:
    sscanf(offset_ptr, "%*[^\t] %*[^\t] %x %d %lf", &j, &k, &l);

  5. #5
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    Quote Originally Posted by tabstop View Post
    It's not always clear, but " " in a scanf format string doesn't mean " ". Any whitespace will stop %s, whether it's spaces or tabs, and " " means any whitespace, not just an actual space character. If you really want "not a tab", then say so:
    Code:
    sscanf(offset_ptr, "%*[^\t] %*[^\t] %x %d %lf", &j, &k, &l);
    tabstop, your code doesn't account for the fact that I have a variable number of words. What should I do if I have 6 words instead of 2?
    Also, the tab space does not happen between words, only between word-number and between number-number
    Last edited by doia; 05-25-2010 at 12:44 PM. Reason: trying to make more clear

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by doia View Post
    What should I do if I have 6 words instead of 2?
    Also, the tab space does not happen between words, only between word-number and between number-number
    You are in luck then (altho tabstop's code does contain one error in this case). If the line is:

    Ghub Blahuahu Blhauhi Bujoj\tD011\t4\t7.8889

    then:
    Code:
    sscanf(offset_ptr, "%*[^\t] %x %d %lf", &j, &k, &l);
    will work. %*[\t] will discard everything up to the first tab.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    Smile it works

    merci beaucoup MK27 & tabstop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  2. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM