How do I check if an input file exist, if the numbers in the file are int or fl

This is a discussion on How do I check if an input file exist, if the numbers in the file are int or fl within the C Programming forums, part of the General Programming Boards category; First off. This is my first c project (and one of the first programming projects in any language), so if ...

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

    How do I check if an input file exist, if the numbers in the file are int or fl

    First off. This is my first c project (and one of the first programming projects in any language), so if my syntax looks strange or I do some strange stuff I appolgize.

    I have a program where three input files are used. Each file contains a few numbers, some are integers and some are floats. I've managed to check if the files exist, but not the other stuff. If any of these demands are not met the program should end and display an error message accordingly. I've seen questions around asking one one of these things, but not combined. The answers I have seen also demands quit a bit of space. I was hoping to get this done without on as few lines as possible. Help is much appreciated.

    One of the scripts I've been working on can be seen below. It's gives an error message if the input file is not there, but I have removed my attempts at checking for int vs. float and if they are within range.

    -------------------------------------------------------------------------------------------
    Code:
    void visualreader(int *x_range, int *y_range, int *z_range, int *x_cells, int *y_cells, int *z_cells, int *time){
    
    //Scans the input file containing parameters for the visualizing the velocity vector field
    FILE *fp = fopen("visual_input.dat", "r");
    
            if(fp){
                    fscanf(fp, "%d %d %d\n", x_range, y_range, z_range);
                    fscanf(fp, "%d %d %d\n",x_cells, y_cells, z_cells);
                    fscanf(fp, "%d\n",time);
    
                    fclose( fp );}
            
            else{
                    perror("error opening the file" );
            }
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,465
    There is no "read this file and tell me if it's a float or an int" function, but there are some ways that aren't too bad. I'm assuming the file is a text file and the numbers are separated by spaces, something like
    Code:
    12 345.67 890
    654 32.10 9.87
    If that's the case, you can use the %s modifier with fscanf. That reads a "string". Perhaps "word" would be a better description, as it reads everything up to a white space. You must use a char array to store the result. Then, I would use the strtol (string to long) and strtof (string to float) functions to try and convert what you read into a number. Unless specified otherwise, I would actually use double instead of float for your program as it is more precise. If you do use double, use strtod instead of strtof. Something like:
    Code:
    char *endp;  // this is for strtol and strtof
    char buf[32];  // this stores the string we got from scanf
    errno = 0;
    x = strtol(buf, &endp, 10);  // the 10 is for base 10 numbers.  strtol can handle base 2-36.
    if (errno == 0 && *endp == '\0') {
        // hooray! we have an integer
        // errno is zero, so strtol had some success
        // we also check that endp is pointing to the null terminator for buf
        // to ensure that strtol didn't stop early because it encountered an invalid character
        // in buf such as a decimal point, or a letter or other garbage
    }
    So that's how you use strtol/strtof to check for success/failure. Since integer is stricter format than float, we should check it first. That is, every valid integer is a valid float, but not vice-versa. The general flow is something like:
    Code:
    read a string into buf using fscanf with the "%s" modifier
    if the string is a valid integer
        do some integer stuff
    else
        if the string is a valid float
            do some float stuff
        else
            we have invalid data in our input

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-13-2012, 07:39 PM
  2. Replies: 2
    Last Post: 11-25-2011, 05:42 PM
  3. Replies: 7
    Last Post: 06-16-2008, 01:18 PM
  4. reading numbers from input file
    By ziga in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2007, 06:23 AM
  5. hwo to check if a file exist in certain dir?
    By Jasonymk in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2003, 07:20 PM

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