-3

This function reads a text file containing numbers of char size(the text file is written in char in another program) and I need to get them into integers after reading. Since the rest of my program is in C++, I want to get this function in C++ too. The most trouble I have is with the fread sizeof(char)

void VAD_Selector(vector<int>& x){
    FILE *ptr = NULL;
    if ((ptr = fopen(USABLE_VAD, "r")) == NULL) {
        cout << "Error opening VAD file" << endl;
        exit(0);
    }
    short mode = 0;
    char VAD_input[2] = "0";
    x[0] = 0;
    int i = 1;
    while (fread(VAD_input, sizeof(char), 1, ptr)) {
        mode = (short)atoi(VAD_input);
        if (mode != 1)
            x[i] = 0;
        i++;
    }
    fclose(ptr);
}

this is what the input text file look like:

00000000000000000000000000000000000001111111111111111111111111111111111111111111111

there is not output but what I want to do is get all data from text file into th x vector (x[0] is always 0)

this is what I tried:

ifstream ptr;
ptr.open(USABLE_VAD);
if (!ptr.is_open()) {
    cout << "Error opening VAD file" << endl;
    exit(0);
}
else {
    x[0] = 0;
    int i = 1;
    char c[2] = "0";
    while (!ptr.eof()) {
        ptr >> c;
        x[i] = atoi(c);
        cout << x[i];
                    i++;
    }


}
ptr.close();

I get this error in VS2015 before ptr << c:

Exception thrown at 0x60C4B8BA (msvcp140d.dll) in Algo_gen.exe: 0xC0000005: Access violation reading location 0x6CB95C28.

If there is a handler for this exception, the program may be safely continued.

I changed the while loop condition and used c - '0' and it works. Thanks to everybody. If it can help anybody else, there is my solution:

void VAD_Selector(vector<int>& x){
ifstream ptr;
ptr.open(USABLE_VAD);
if (!ptr.is_open()) {
    cout << "Error opening VAD file" << endl;
    exit(0);
}
else {
    x[0] = 0;
    int i = 1;
    char c = '0';
    while (ptr >> c) {
        x[i] = c - '0';
        i++;
    }
}
ptr.close();

}

  • What *have* you tried? How didn't it work? What problems do you have with your attempt? – Some programmer dude Oct 05 '17 at 14:18
  • 2
    And, your question is? "_The most trouble I have is with the fread sizeof(char)_" What kind of trouble? Does it not compile? Specify compilation error. It does compile, but provide incorrect results? Provide the input you are using, expected output, and actual output. And, in any case, provide [mcve]. – Algirdas Preidžius Oct 05 '17 at 14:18
  • Indeed the size of a character literal [does have a difference between C and C++](https://stackoverflow.com/a/2172948/1460794), but otherwise the size should always be 1. – wally Oct 05 '17 at 14:22
  • [Reading a single character from an fstream?](https://stackoverflow.com/q/9176867/669576) – 001 Oct 05 '17 at 14:23
  • 2
    @rex Technically, that's a different about constant character literals, not the `char` type itself. – Some programmer dude Oct 05 '17 at 14:23
  • `sizeof(char)` is `1` by definition. `sizeof` output is given in units of `char`. – Angew is no longer proud of SO Oct 05 '17 at 14:26
  • 1
    The original function you present *is* C++. It will not compile as C. I guess you mean only that you want to replace direct calls to C standard library functions with use of C++ I/O streams. That's not wrong to do, but I'm a firm believer that if it ain't broke, don't fix it! – John Bollinger Oct 05 '17 at 14:49
  • @JohnBollinger I still want to do it because my program is going to be published, so I want it to be uniform (all other files that I handles I use I/O streams – Mister Milk Oct 05 '17 at 14:54
  • `c` is a char array. It should just be a `char c;` – 001 Oct 05 '17 at 15:04
  • @JohnnyMopp when I use only char variable, I get an error using atoi(c) ("char" incompatible with type "const char*"). – Mister Milk Oct 05 '17 at 15:07
  • 1
    It looks like there are other issues, too, but see [Why is iostream.eof() inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – John Bollinger Oct 05 '17 at 15:07
  • You don't need `atoi`. You can just convert char to int like: `x[i] = c - '0'`; – 001 Oct 05 '17 at 15:08
  • The `atoi()` is a bit clumsy even in the original code. You can convert the `char` representing a decimal (or binary) digit to the correspond number simply by subtracting `'0'`. – John Bollinger Oct 05 '17 at 15:09
  • Thanks guys, I got it figured out with the c - '0' and ptr >> in the while loop – Mister Milk Oct 05 '17 at 15:19

2 Answers2

0

If I am correct, you are trying to implement C function VAD_Selector(vector<int>& x) in C++ and you are facing trouble implementing code line while(fread(VAD_input , sizeof(char), 1, ptr)) in C++. Right?

I think above fread function is implemented in C and you might have a header file for it. Because of name mangling in C++ you must have to add the below codes (extern "C") at the beginning of your C++ file to use function fread in your C++ program. But still you have another problem i.e. file pointer ptr, the 4th parameter in function fread. Now if you pass ifstream reference ptr in place of FILE *ptr you will get a compilation error.

Hence you cannot use ifstream reference in this case but still you can implement it with a FILE pointer which is allowed in C++ as well.

extern "C"{
  //hear header file of your function ‘fread’ 
   #include <fread function headerfile.h>
}
Walter
  • 44,150
  • 20
  • 113
  • 196
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
0

I think what you want is something like this

std::vector<int> VAD_Selector(std::string const&file_name)
{
  std::ifstream input(file_name);
  if(!input.is_open())
    throw std::runtime_error("failed to open file '"+file_name+"' for reading");
  std::vector<int> data = {0};
  for(char c; input >> c;)
    data.push_back(int(c-'0'));
  return data;
}
Walter
  • 44,150
  • 20
  • 113
  • 196