Like Tree1Likes
  • 1 Post By whiteflags

fscanf problem.. PLS HELP

This is a discussion on fscanf problem.. PLS HELP within the C Programming forums, part of the General Programming Boards category; The value of my e.pdes is wrong.. It should be this "ASUS 101 Motherboard" not this "ASUS 101 Motherboard 4500.00". ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    61

    fscanf problem.. PLS HELP

    The value of my e.pdes is wrong.. It should be this "ASUS 101 Motherboard" not this "ASUS 101 Motherboard 4500.00". And e.price to becomes "0.00"
    Please help, thanks?

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    struct forInfo
    {
      char pnum[50];
      char pdes[50];
      float price;
    } e;
    
    
    void view()
    {
     FILE *fp = fopen("master.dat","r");
     while((fscanf(fp, "%s\t%[^\n]s\t%f\n", &e.pnum, &e.pdes, &e.price)) != EOF)
     {
    	printf("%s\t%s\t%0.2f\n", e.pnum, e.pdes, e.price);
     }
     fclose(fp);
    }
    
    
    int main()
    {
      view();
      getch();
      return 0;
    }
    MASTER.DAT
    Code:
    ASUS101112	ASUS 101 Motherboard     4500.00
    BOKA118001	BOKA 118 Speakers	    3500.00
    ASUS101112	ASUS 110 Motherboard	    4500.00
    BOKA118001	BOKA 118 Speakers	    3500.00
    OUTPUT
    Code:
    ASUS101112	ASUS 101 Motherboard     4500.00     0.00
    BOKA118001	BOKA 118 Speakers	    3500.00     0.00
    ASUS101112	ASUS 110 Motherboard	    4500.00     0.00
    BOKA118001	BOKA 118 Speakers	    3500.00     0.00

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    few things...

    fscanf(fp, "%s\t%[^\n]s\t%f\n", &e.pnum, &e.pdes, &e.price)

    The first conversion %s does not have a corresponding argument of the correct type; my compiler says:
    C:\Users\Josh2\Documents\sandboxc\main.c||In function 'view':|
    C:\Users\Josh2\Documents\sandboxc\main.c|16|warnin g: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[50]' [-Wformat]|

    Strings can be read into character arrays by the name alone, it is incorrect to use & in this situation.

    It also generates a strange warning for the second string you try to read:
    C:\Users\Josh2\Documents\sandboxc\main.c|16|warnin g: format '%[^|
    ||=== Build finished: 0 errors, 2 warnings (0 minutes, 0 seconds) ===|

    %[^\n]s

    This part of the format string is a mess. %[^\n] does not help you here, it will just consume the line in the file and try to store it in the corresponding argument. Then you repeated the same mistake: &e.pdes is the wrong type of expression. Furthermore, the trailing s is a problem. Characters by themselves must match, so 's' will not match the next character. You end up failing to read the first line.

    To help you read the file... Are there restrictions on product descriptions? Anything will help. For example if descriptions are Guaranteed to be three words, then just read the three words and glue them together before you fill the structure's member. In general you need a smarter approach to that part of the file.
    rogster001 likes this.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Maybe even something like %[^\t]\t is better for project descriptions if you can guarantee they end in tabs.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    Thanks it worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf problem?
    By zapree in forum C Programming
    Replies: 8
    Last Post: 03-20-2012, 11:55 PM
  2. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 10:40 AM
  3. fscanf problem maybe?
    By zdream8 in forum C Programming
    Replies: 13
    Last Post: 05-24-2008, 04:33 PM
  4. Problem with fscanf()
    By cnorther in forum C Programming
    Replies: 4
    Last Post: 01-24-2007, 12:35 AM
  5. Problem with fscanf..
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 01-11-2002, 03:56 PM

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