Thread: help with comparing data from a file

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    44

    help with comparing data from a file

    data is being read from a txt file with this format
    Meet the Fockers,12345678918,04/19/2005,116
    Meet the Parents,PG,03/06/2001,108
    Fantastic Four,PG-13,07/10/2005,99
    Harry Potter: Prisoner of Azkaban,Not-Rated,11/23/2004,142
    Hitch,PG-13,07/02/2005,103
    lets say if length is > than 3 digits or a negative number is in the input file then it would print invalid.. how would i go about doing that.. i tried changing char length[50] to int length and changed to %d in fscanf but then the displayed weird numbers when i printed it for testing...

    Code:
    
    typedef struct {
    char title[50];
    char rating[50];
    char date[50]; 
    char length[50];
    } Videos;
    
    ---------------
    
              fscanf(in_file, "\n%50[^,],%50[^,],%50[^,],%s",movies[i].title, movies[i].rating,movies[i].date,&movies[i].length);
    Last edited by loso44x; 10-03-2005 at 01:53 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Read the last number in as a string and just do if(input[0] == '-' || strlen(input) > 3) to test for invalidity.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    what does input[0] == '-' do? how does that check for all the characters?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It checks if the first character is a '-' (i.e. if the number is negative.)
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM