Thread: How to scan a tab deliminated text file

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    How to scan a tab deliminated text file

    hi all this is my text file.

    bak kut teh[tab]888.0[tab]989.0
    spicy chicken nugget[tab]999.0[tab]978.0

    ---------------
    I'm experiencing some problem in trying to scan and put it into an array for the food names (e.g an array for food which consist of bak kut teh and spicy chicken nugget) and another 2 array for the other individual integer into C. However there seem to be some problem with my code.
    (I am really a beginner)

    Is there a difference if I use both tab and space instead of space for all?

    Thanks for any kind of help






    Code:
    #include<stdio.h>
    
    
    int main()
    {
        FILE *fp;
        char food[100];
        char buff[BUFSIZ];
        float num;
        int i;
        int j=0;
    
    
        if((fp=fopen("C:\\Users\\Rasyiqa\\Desktop\\test.txt","r"))==NULL)
        {
            perror("File cannot be opened");
            getchar();
            exit(1);
        }
    
    
        while(fgets(buff,sizeof(buff),fp)!=NULL)  // read a single line from the file till i get EOF
        {
            for(i=0; food[i]!='\0'; i++){
            if(food[i]>='a' && food[i]<='z'){
                j++;
    
    
            }
            }
            if(sscanf(buff,"food[i] %f",food,&num)==2)  // getting the name and the number from the  buffer which was read from the file
            printf("%s %.1f\n",food,num);
            else
            printf("line cannot be printed\n");
        }
    
    
        getchar();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Unless specifically scanning characters (such as with with the %c format specifier) sscanf() treats all whitespace equivalently.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
        while(fgets(buff,sizeof(buff),fp)!=NULL)  // read a single line from the file till i get EOF
        {
            if(sscanf(buff,"%s %f %f",food,&num)==2)  // getting the name and the number from the  buffer which was read from the file
            printf("%s %.1f \n",food,num);
            else
            printf("line cannot be printed\n");
        }
    You can't have "food[i]" in the format string for fscanf. Tabs are just a waste of space for this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to scan in multiple values from a text file
    By GenghisAhn in forum C Programming
    Replies: 12
    Last Post: 10-30-2012, 02:52 AM
  2. Replies: 2
    Last Post: 03-05-2012, 10:35 AM
  3. Text files scan problem.
    By Siaw Ys in forum C Programming
    Replies: 1
    Last Post: 11-13-2011, 07:12 AM
  4. Read text file, scan and store data into variables?
    By wisdom30 in forum C Programming
    Replies: 8
    Last Post: 04-18-2011, 11:23 PM
  5. Scan some text and replace words
    By Killroy in forum C Programming
    Replies: 3
    Last Post: 11-22-2004, 08:30 AM

Tags for this Thread