# Thread: File Manipulation in C?

1. ## File Manipulation in C?

Hi,

I have some data points (x points) in my program like this:

Code:
```double x[10][4];
x[0][0] = 5.1; x[0][1] = 3.5; x[0][2] = 1.4; x[0][3] = 0.2;
x[1][0] = 4.9; x[1][1] = 3.0; x[1][2] = 1.4; x[1][3] = 0.4;
x[2][0] = 4.7; x[2][1] = 3.2; x[2][2] = 1.3; x[2][3] = 0.8;
x[...................and the.............................];
x[...................other points................];```

As you can see, each point has four coordinates that shows their positions and all I want to do is getting these "x" points' coordinates and their dimension sizes (in this code they have 4 dimension) and number of x points (in this code there is 10 x points) from a data file.

You can see a data file sample at the below:

Code:
```336 5
0.49 0.29 0.48 0.5 0.56
0.07 0.4 0.48 0.5 0.54
0.56 0.4 0.48 0.5 0.49
.
.
.```

See? It's a data set which has been compiled in GCC. There is totally 336 'x' points in this file, and each point has 7-Dimension so there is seven coordinates which shows each point's position. And at the last, you can see the points' coordinates. I don't want to waste my time with entering these values manually one by one, so I heard that I can use File Manipulation method but I really don't know how. Can somebody show me the way just "with a sample code" please?

So I want to make program like this automatically:

Code:
```double x[336][5]; // 336 is number of data points and 5 is their dimension, I want to get these values from data file. I just wrote 3 of 336 points here.
x[0][0] = 0.49; x[0][1] = 0.29; x[0][2] = 0.48; x[0][3] = 0.5; x[0][4] = 0.56; // First point's coordinates, I want to get this and all those x values from the data file either.
x[1][0] = 0,07; x[1][1] = 0.4; x[1][2] = 0.48; x[1][3] = 0.5; x[0][4] = 0.54;
x[2][0] = 0.56; x[2][1] = 0.4; x[2][2] = 0.48; x[2][3] = 0.5; x[0][4] = 0.49;```

By the way, I'm using a Windows compiler (Bloodshed Dev C++) and these data files compiled in Linux (GCC), so I don't know if there will be a problem when I use these data files in my Windows compiler.

You can ask me anything, I will answer them with my poor English in pleasure

2. If you don't know anything about parsing a file, I would suggest looking it up on google first. When you find a decent tutorial, come back here and post your attempt to parse the file yourself.

smorgasbordet - Pelles C
Code::Blocks

Bloodshed Dev C++ hasn't been maintained for the last decade.

Next, prepare a simple file to test your code with.
Say
3 5
0.49 0.29 0.48 0.5 0.56
0.07 0.4 0.48 0.5 0.54
0.56 0.4 0.48 0.5 0.49

Then you start to write some simple code.
Really basic code to begin with, like print each line as you read it.
line=3 5
line=0.49 0.29 0.48 0.5 0.56
line=0.07 0.4 0.48 0.5 0.54
line=0.56 0.4 0.48 0.5 0.49

Use fopen(), fgets() and fclose()
If it prints anything else, then you need to fix it before continuing.

Then start to parse this data, so you can print
rows = 3, columns = 5
[0][0] = 0.49 ....
....

Come back when you can do that much, then we can talk about arrays to store your data.