0

I have a file that I would like to read and that file follows a pattern, basically the file only contains integer and those integer are actually x, y, z co-ordinates of a location and followed by x, y, z co-ordinates of another location in the same line. The line does not have fixed number of coordinates. Line 1 might have co-ordinates of 15 location and line 2 might have co-ordinates of 24 location and there may be an arbitrary amount and type of whitespace (newlines, spaces, tabs) between any of the integers in the file.

> integer x_coordinates integer y_coordinates integer z_coordinates...
> integer x_coordinates integer y_coordinates integer z_coordinates...
> integer x_coordinates integer y_coordinates integer z_coordinates...
>
> integer x_coordinates integer y_coordinates integer z_coordinates...
>
>
> integer x_coordinates integer y_coordinates integer z_coordinates...

Now, I am planning to read through every line of file and avoid every empty line and store the co-ordinates into a typedef struct.

typedef struct{
    int* x;
    int* y;
    int* z;
}Coordinates;

I want to store all the x coordinates in int* x, all y coordinates in int* y and all z coordinates in int* z.

But problem is that there can be arbitrary number and type of white spaces in between 2 numbers even between y coordinate of location 1 and z coordinate of location 1 or between z coordinate of location 5 and x coordinate of location 6.

So all in all I am not able to figure out how to use fscanf here to read through a file like this.

Jay Dixit
  • 11
  • 2
  • 1
    You should probably use `int` in the structure and create an array of the structure type, rather than effectively three arrays within the structure. `fscanf()` cares not a whit about lines. You may have to worry about commas after the data — are the numbers really comma-separated? Will you mind if one triplet of coordinates is split across two lines? If so, you'll need to use `fgets()` or POSIX `readline()` to read lines and then use `sscanf()` ([in a loop](https://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops)) to process each line in turn. Check `sscanf()`'s return value. – Jonathan Leffler Dec 11 '20 at 22:13
  • Is the last number on the line followed by a comma? Also, what have you tried? You should show what you're up to. For example, there's a pretty straight-forward piece of memory management code required to allocate increasing amounts of memory. Don't reallocate one more entry at a time. Use a doubling algorithm for the number of rows allocated. Keep tabs on the rows allocated and the rows in use, of course. – Jonathan Leffler Dec 11 '20 at 22:15
  • Showing a few example lines of your real input data would help to understand your description. Probably it would be good to use `fscanf` to read 3 values (x,y,z) in a loop as long as the return value indicates that 3 values have been read and use dynamic (re)allocation of an array of structures. – Bodo Dec 11 '20 at 22:22
  • I have edited the question, I added commas in the sample view of file by mistake.... Integers are not seperated by commas infact there are no commas there is only spaces – Jay Dixit Dec 11 '20 at 22:27
  • all you have are numbers and spaces in this file? – vmp Dec 11 '20 at 22:27
  • 1
    Provide some sample input. From your description, I *think* the input must have space separated integers, and there must be a multiple of 3 integers per line. But I'm not really sure about the multiple of 3 requirement. Some examples would help to clarify. But it sounds like you would treat the input `1 2 3\n4 5 6\n` exactly the same way you would tread `1 2 3 4 5 6\n`, so I don't understand what the problem is. (eg, just use `"%d"` format specifier and forget about the whitespace.) – William Pursell Dec 11 '20 at 23:22

1 Answers1

0

If all your file have is integers and whitespaces... Your strugle with reading can be done as shown below:

int x, y, z;
FILE *f = fopen("filename.txt", "r");
while(fscanf(f, "%d %d %d", &x, &y, &z) == 3)
{
    // your logic to store in the struct goes here
}
vmp
  • 2,370
  • 1
  • 13
  • 17