d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Another Problem In C > Reading From File Into An Array Of Point
12Next
Add Reply New Topic New Poll
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 7 2012 09:59pm
I've been in the library all day and this is like my 3rd program and I just can't get the ball rolling on it.

there's a lot more to this program, hence the header files and whatnot, but right now I am just trying to get this thing to read floats from a file into an array of float type pointers via this function getData.

right now the thing crashes over and over no matter what I change but it compiles fine.

Any help would be greatly appareciated. IF you want to post examples I'd also appreciate it if they were in C for understanding's sake.

Thanks.

Edit: Just treat this as pseudo code as it's pretty hacked atm. I was just trying to assign 1 float into the address in position 0;

Edit Edit: yes I put a semicolon at the end of that cause that's how long I've been doing this today haha...

Code
#include <stdio.h>
#include <string.h>

#include <stdlib.h>

void getData(float**);

int main() {

float *array1[20];

float **arrayP;

arrayP = array1;

getData(arrayP);


return 0;

}

void getData(float **array1F) {

int x;

FILE *fp;

fp = fopen("FloatNums.txt", "r");

while ((x = fgetc(fp)) != EOF) {

 fscanf(fp, "%f", array1F[0]);

}

fclose(fp);


}


This post was edited by Nom_Nomz on Dec 7 2012 10:02pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Dec 8 2012 12:51am
you're probably segfaulting because of this:

Code
float *array1[20];
float **arrayP;
arrayP = array1;


look at what you just wrote here.
Member
Posts: 6,882
Joined: Aug 23 2007
Gold: 2,124.99
Dec 8 2012 04:14am
Where are you putting FloatNums.txt?
Member
Posts: 14
Joined: Nov 14 2012
Gold: 1,500.00
Dec 8 2012 04:38am
why are you passing the double pointer to getData?
As long as I read your code, giving a pointer(float *) should be sufficient.

I'd do something like that.

void getData(float *f) {
......
fscanf(fp, "%f", f);
......
}

This post was edited by banbey on Dec 8 2012 04:59am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Dec 8 2012 04:49am
are you only wanting to enter stuff into the first element of array1?

not terribly familiar with the C scan function but does it go to element 1 and 2 and so on?

This post was edited by Eep on Dec 8 2012 04:49am
Member
Posts: 14
Joined: Nov 14 2012
Gold: 1,500.00
Dec 8 2012 02:46pm
I don't know your file format, so I didn't touch the while part very much.
Also, I didn't run or compile this code, but it should work.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void getData(float*);

int main() {

float array1[20];
float *arrayP;
int i;

/*Is there any reason you can't use array1 directly? */
arrayP = array1;

getData(arrayP);

printf("Your array has the following elements\n");
for(i = 0; i++ i < 20){
printf("No%d:%f\n", i, arrayP[i];
}
return 0;
}

void getData(float *floatArray) {

int x; /*why are you using it? */
int count = 1;
FILE *fp;


if ((fp = fopen("FloatNums.txt", "r")) == NULL){
printf("File Not Found Beep Beep\n");
return;
}

/*
* Make sure your file contains no more than 20 floats value
* or it WILL PROBABLY cause a segfalut.
* Also, you might wanna consider using a better way to read your file.
* It looks messy at least for me.
*/

while ((x = fgetc(fp)) != EOF) {
fscanf(fp, "%f", floatArray++);
}
fclose(fp);
}
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 8 2012 03:18pm
It is supposed to be an array of float pointers. Each of these pointers in the array are supposed to point to a float in the file.
Member
Posts: 14
Joined: Nov 14 2012
Gold: 1,500.00
Dec 8 2012 04:15pm
I guess it's a school assignment in which students are supposed to learn about how pointers works.
Otherwise, it doesn't make any sense.

In your code, each element of array1[20] has a random address.
So you gotta initialize them first or Using them will probably cause segfalut.

You can do that using malloc or you can just do that as below.

float *array1[20];
float **arrayP;

/* I added this */
float boobs[20];
int i;

arrayP = array1;

/* Now array1 has valid pointers */
for(int i = 0; i < 20; i++){
array1[i] = &boobs[i];
}

getData(arrayP);
return 0;


This post was edited by banbey on Dec 8 2012 04:16pm
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 8 2012 05:01pm
yeah I guess the instructor wants to make sure we have a REALLY good grasp on how these things work, which is great and all but, unfortunately we don't cover too much in class (I wish we did) so I have to go all over the internet looking it up.

So with my array of pointers I have to set them to point to another array of floats first because obviously I can't just run through my float text file and assign floats to a pointer type right?

I am not sure how my logic was working before I guess I thought the array of pointer would just point to the numbers while they were in the text file which seemed really weird to me but I thought that might be how it worked haha

Sorry my understanding is still minimal on this and thanks.

This post was edited by Nom_Nomz on Dec 8 2012 05:01pm
Member
Posts: 14
Joined: Nov 14 2012
Gold: 1,500.00
Dec 8 2012 08:04pm
Quote (Nom_Nomz @ Dec 8 2012 04:01pm)
So with my array of pointers I have to set them to point to another array of floats first because obviously I can't just run through my float text file and assign floats to a pointer type right?

It doesn't have to be an array of float. But, your pointers have to have a address(reference w/e) of actually allocated memory.

Either way works.

1.
Code
for(int i = 0; i < 20; i++){
 array1[i] = (float *) malloc(sizeof(float));
}

or....

float *ptr;
ptr =  (float *) malloc(sizeof(float) * 20);
for(int i = 0; i < 20; i++){
 array1[i] = ptr + i;
}


2.
Code
float a,b,c,d,.......;
array1[0] = &a;
array1[1] = &b;
array2[2] = &c;
...........



3.
The way showed in my previous post.

This post was edited by banbey on Dec 8 2012 08:30pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll