0

This class function CLASS::READWRITE is supposed to read data from an input file and write to an output file only if its input value is of type int. When I enter a floating point value is passed as x, the decimal places are truncated and the function executes normally instead of outputting a warning. If a lowercase letter like 'b' is passed as x, the value is apparently read as "0" so the function executes normally instead of outputting a warning.

How can I enforce the check (x != int)? I've seen suggestions for using cin.fail(), but I am not using cin here.

#include <iostream>
#include <fstream>
using namespace std;

ifstream input_file("in_file", ios::in);
ofstream output_file("out_file", ios::out);

int main(void)
{
    CLASS object(n);                // n is the number of values held in in_file
    object.READWRITE(x);            // x is some test value

}

void CLASS::READWRITE(int x)
{
    int i;
    for(i = 0; i < n; i++)
    {
        input_file >> number[i];
    }

    for(i = 0; i < n; i++)
    {
        if((x != int) || (x < 0))
        {
            out_file << "Error" << endl;
            break;
        }
        else
        {
            out_file << num[i] << endl;
        }
    }
}
photon
  • 606
  • 4
  • 14
  • 31
  • It would seem to me, that you're looking for the `typeof` function. While it's not in the C standard, it's still widely used. Check this link http://stackoverflow.com/questions/12081502/typeof-operator-in-c. Also, regarding your other problem...you're suffering from 'implicitely conversion'. Most simple data types (char, int, float, etc.) can be implicitely converted to each other. You'll lose precision, but that's. If you REALLY want to know what the stuff in your file is, you need to check the string, not the number. – Refugnic Eternium Feb 24 '13 at 21:18
  • How are integer strings differentiated from other strings? – photon Feb 24 '13 at 21:22
  • Simple enough. Scan for anything that's not in an integer. Step one: Get the string (anything between two delimiters, like spaces, line breaks, etc.) Step 2: Check for ANYTHING that's not a number in this string. if there's anything not in the range of 48-57 (Both included, ASCII values for 0-9), it's NOT an integer. – Refugnic Eternium Feb 24 '13 at 21:24

2 Answers2

2

This isn't exactly what you're looking for, but if you want to make sure that you're only transferring integers, you'll have to check the strings. And while you specifically asked for C++, I'm just more comfortable with C file handling, so please bear with me.

FILE *fp = fopen("input.file", "rb"), op = fopen("output.file", "wb");
if(fp && op) {
     while(!feof(fp)) {
         char buffer[20], *ptr = buffer;
         fscanf(fp, "%20[^\s\n]", buffer);
         while(ptr && *ptr != 0) {
             if(*ptr >= '0' && *ptr <= '9') ++ptr;
             else ptr = 0;
         }
         if(ptr) fprintf(op, "%s", buffer);
     }
     fclose(fp);
     fclose(op);
}

In short, this code scans the entire file for blocks, that are delimited by spaces or linefeeds (Assuming that any one number is not greater than 20 characters.) In the next step, it detects, whether the character in question is a number (in which case the pointer is moved up one step), or not. In the latter case, the string is invalid as integer and discarded (the pointer is set to 0, which in turn voids the 'write' after the loop.

I believe this should do what you are looking for.

Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24
1

You declared the parameter x to be an int so it's an int

void CLASS::READWRITE(int x)

When you pass in a double or a char, the compiler implicitly casts them to an integer value. For doubles this means truncating after the decimal and for chars, the compiler treats the ascii representation as an integer.

Assuming, as others here have, that the parameters x are coming from user/file input, you have to check each input string manually to be sure that it's a valid integer. If the input is binary then there's really no way to tell since data is context sensitive.

C. M.
  • 100
  • 1
  • 4