Thread: Problem with Parsing Data

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    Problem with Parsing Data

    Hey i have written a program that is supposed to parse the ":" out of a data file and it compiles fine with no errors or anything but when it prints the output it is harshly incorrect so obviously i have the input from the file programed wrong or the parsing messed up any help would be appreciated!! Thanks.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "program8.h"
    
    //structure declaration
    
    int main()
    {
    PAINT mypaint[20];
    char Input[100];
    FILE* infile= fopen("paint.data","r");
    
    if(!infile)
    {
    printf("Could not open file");
    exit(1);
    }
    
    getData (mypaint, infile, Input);
    
    printData (mypaint);
    
    return 0;
    }
    
    void getData(PAINT* mypaint, FILE* infile,char* Input)
    {
    	int i;
    
    fgets(Input, sizeof(Input), infile);
    for(i = 0; i < 5; i++)	
    	{ 
    	sscanf(Input, "%s[^:] %c[^:] %d[^:] %f", mypaint[i].color, &mypaint[i].size, &mypaint[i].qty, &mypaint[i].price);
    	}
    
    return;
    }
    
    void printData (PAINT* mypaint)
    {
    	int i;
    	
    	for (i=0; i<4; i++)
    	{
    	 printf("%s %c %d %f \n\n", mypaint[i].color, mypaint[i].size, mypaint[i].qty, mypaint[i].price);
    }
    return;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    that's because the corresponding argument to the scanset, [], is of type array of char

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    that's because the corresponding argument to the scanset, [], is of type array of char
    Or rather, that each scanset needs to be preceded by % AND matched with the address of the first element of a char array like %s. You don't use "%s[^:]", but "%[^:]" INSTEAD of "%s". For the other fields, where you have numbers and chars, you can actually use a straight formatting string, "%d:" will read an integer, and then look for accept a colon.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  2. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  3. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 05:05 AM
  4. Problem with Printing message
    By robert_sun in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 02:09 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM