fscanf and it's use in filtering through text

This is a discussion on fscanf and it's use in filtering through text within the C Programming forums, part of the General Programming Boards category; I'ld first like to thank everyone on this site for their contributions, it's actually allowed some of my hair not ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    fscanf and it's use in filtering through text

    I'ld first like to thank everyone on this site for their contributions, it's actually allowed some of my hair not to be pulled out this time around.

    I am trying various approaches (since they keep failing) to have my program scan through a file that has dates scattered about in it. First attempt tried making two arrays of date desired and date found and then use strcmp. Since it's been a while with handling data types, I believe it's comparing numbers to ascii despite my best efforts to convert them (c=c-48 ... but I bet it's still ascii #!)

    So I am trying scanf, which I never learned. Testing with human input, scanf("%d", &dtest) reports 12 if I enter 12/. However, will this keep going in a file that has mm/dd/yyyy? My printf debugger shows it's not, but it keeps crashing on me.

    TL;DR: the date format is mm/dd/yyyy
    Will fscanf in a file skip past the 2nd / and keeps on going if I tell it scanf(fread, "%d", &found); ? Is there a way to tell a scanf to stop if another data type is found or after a 2nd or 3rd character?

    I can add the codes I have right now, but they're so amateurish it's embarrassing. In brief the main part:
    Code:
        Int c; /*char?*/
        FILE *fread;
        fread=fopen("c:\\duedate.txt", "r");
        c=getc(fread);
        int count=0;
        int found;
        int dtest;
     while (c!=EOF)
              {
                    c=fgetc(fread);
                     if (c=='/')
                          {
                              fscanf(fread, "%d", &found);
                              fscanf(fread, "%d", &dtest);
    /*assuming it stopped at the / and i need to verify that it's 2012*/
                              if (strcmp(found, dtest)==0)
                                    {
                                        count++;}
                                    }
                              }
                }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    How about
    Code:
    if ( fscanf(fread,"%d/%d/%d", &day, &month, &year ) == 3 )
    Or if you're just after the year
    Code:
    if ( fscanf(fread,"%*d/%*d/%d", &year ) == 1 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using fscanf to read text from a file.
    By november1992 in forum C Programming
    Replies: 25
    Last Post: 09-15-2012, 01:15 AM
  2. filtering
    By kezkez in forum C Programming
    Replies: 3
    Last Post: 07-05-2010, 05:13 PM
  3. using FSCANF to read mixed text/double files
    By cfdprogrammer in forum C Programming
    Replies: 3
    Last Post: 03-23-2009, 03:18 AM
  4. Filtering a string?
    By Look- in forum C# Programming
    Replies: 1
    Last Post: 04-18-2008, 07:41 AM
  5. Using fscanf for reading numerical data from text file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-15-2002, 05:18 PM

Tags for this Thread


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