Read from a file into an array?

Joined
Nov 9, 2008
Messages
2
Reaction score
0
Hello to all. I am trying to read from a file into an array. I am trying to use the first integer of the file as the size of the array.


int main(int argc, char *argv[])
{
int numberOfChar;


FILE *fp;
fp=fopen("test.txt", "rb");
if (fscanf(fp, "%d", &numberOfChar) == 1)
printf("%d\n", numberOfChar);
int x[numberOfChar];


int s;

for (s = 0; s < numberOfChar; s++)
{

fscanf(*fp, &x);

}


return 0;
}

But I get the errors:

Type error in argument 1 to 'fscanf'; found 'FILE', expected 'FILE *'.
Type error in argument 2 to 'fscanf'; found 'int *', expected 'const char *'.

both pointing to the:

fscanf(*fp, &x);

Line. Is my logic completely wrong? I thank you in advance!
 
Joined
Nov 9, 2008
Messages
2
Reaction score
0
I changed one line

fscanf(fp, "%d", &x);


so now:

int main(int argc, char *argv[])
{
int numberOfChar;


FILE *fp;
fp=fopen("test.txt", "rb");
if (fscanf(fp, "%d", &numberOfChar) == 1)
printf("%d\n", numberOfChar);
int x[numberOfChar];


int s;

for (s = 0; s < numberOfChar; s++)
{
fscanf(fp, "%d", &x);
}


return 0;
}


It returns the number 3... which is the first number of the text document...so I think I'm heading in the right direction, but still not entering the data into the array?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top