0

I am trying to read a file in C which has four numbers per line which represents coordinates of two points and then I am trying to find the distance between the two points. For this, I am reading the file lines one by one and I am getting it as a char array. Below is what I have done.

Output function which I am calling from read()

void output(char *buff){
    int co1,co2,co3,co4;
    co1 = atoi(buff[0]);
    co2 = atoi(buff[2]);
    co3 = atoi(buff[4]);
    co4 = atoi(buff[6]);
    printf("(%d,%d) lies on the %s,(%d,%d) lies on the %s, distance is %f.\n",co1,co2,quadrant(co1,co2),co3,co4,quadrant(co3,co4),distance(co1,co2,co3,co4));

}

Read function which I am calling from main.

int read(){
    FILE *file;
    char buff[255];
    file = fopen("point.dat", "r");
    while(!feof(file)){
        fgets(buff,255,file);
        output(buff);
    }
    return !feof(file);
}

Main function

int main()
{
    read();
    return 0;

}

But while doing so, I am getting below error.

[Error] invalid conversion from 'char' to 'const char*' [-fpermissive]

The data of point.dat is

0 0 3 4
-1 -4 5 6
-1 3 -1 -2
4 -5 -5 -6
3 5 -6 5
0 5 5 5 
-5 0 0 -5

How can I convert char to array in output function? I tried stoi function too, but I got error that "stoi is not in scope"

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
Vivek
  • 207
  • 1
  • 4
  • 17
  • The type of `atoi()` argument should be `const char *` and you are passing it a `char` type. – H.S. Apr 05 '20 at 05:08
  • How can I change it to const char*? – Vivek Apr 05 '20 at 05:10
  • 1
    You will want to look at [**Why is while ( !feof (file) ) always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) Also, don't *hardcode* filenames in functions. Either read it as input, or pass as an argument to `int main (int argc, char **argv)` and open and validate the file is open in the caller (`main()` here) and pass the open `FILE*` as a parameter to your `read()` function. – David C. Rankin Apr 05 '20 at 05:14
  • Sorry I am not able to get it from https://stackoverflow.com/questions/4600797/read-int-values-from-a-text-file-in-c – Vivek Apr 05 '20 at 05:15
  • I used that too, still, I got some error. @H.S. – Vivek Apr 05 '20 at 05:17
  • A typical approach is to use `fgets` to read a line, and then use `sscanf` to extract the numbers from the line. – user3386109 Apr 05 '20 at 05:18
  • Or `while (fgets (buff, 255, file)) { if (sscanf (buff, "%d%d%d%d", &col1, &col2, &col3, &col4) == 4) { /* you have a good read and conversion of all points */ }` – David C. Rankin Apr 05 '20 at 05:18
  • Please make a [mre] of the attempt to use the functions made for this. Also, do not name your own functions (e.g. `read()` like existing functions). – Yunnosch Apr 05 '20 at 05:19
  • You would need, e.g. `co1 = atoi(&buff[0]);` to fix your error (the `'&'` for all conversions), but never use `atoi()` in production code. It has *zero* error reporting capabilities and will silently return zero if, for example, you attempt `atoi ("my cow");` and you will never know an error occurred. – David C. Rankin Apr 05 '20 at 05:21
  • @DavidC.Rankin thanks, your & trick worked. – Vivek Apr 05 '20 at 05:26
  • Also, rename your `read` function as `read_points` (or whatever you like other than `read()`), there is already a standard library function named `read`. It is legal to declare a function with the same name, but that is generally something you want to avoid `:)` – David C. Rankin Apr 05 '20 at 05:28
  • @DavidC.Rankin Sure. – Vivek Apr 05 '20 at 05:29

1 Answers1

0

In your output() function, buff[i] is only one char which is not a string (char*) for atoi(). You should use strtok() to buff into tokens using the delimiter space and then pass each token to atoi().