5 Replies - 1351 Views - Last Post: 09 November 2008 - 01:57 PM Rate Topic: -----

#1 fmsguy06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 04-May 07

Reading from a file into an array

Posted 09 November 2008 - 12:20 PM

Didn't mean to post twice, the original question is bellow.

This post has been edited by fmsguy06: 09 November 2008 - 12:26 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Reading from a file into an array

#2 fmsguy06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 04-May 07

Re: Reading from a file into an array

Posted 09 November 2008 - 12:21 PM

Hello to all! I am trying to read from a file and store that information into an array, to manipulate it later. I want the first character read from the file to be the size of the array. I have done something similar with file pointers:


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

	int numberOfChar;
	int thisInt;
	int currentInt;

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


for(currentInt = 0; currentInt < numberOfChar; currentInt++)
	{
	fscanf(fp, "%d", &thisInt);
	printf("%d\n", thisInt);
	
}

	printf("\n");
	printf("\n");


 return 0;
}




I thought using some of that same code I could read the file into an 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]; //allocating memory

	
	int s;

	for (s = 0; s < numberOfChar; s++)
   	{
	fscanf(*fp, &x[s]);
   	}
	  //reading the data into the array

	return 0;
}




But it looks like I am doing something wrong. I receive two errors, both pointing to line:

fscanf(*fp, &x[s]);




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


Am I making a simple error I can not see, or is my logic completely wrong? I thank you in advance!
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Reading from a file into an array

Posted 09 November 2008 - 12:22 PM

Topics merged :)
Was This Post Helpful? 0
  • +
  • -

#4 fmsguy06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 04-May 07

Re: Reading from a file into an array

Posted 09 November 2008 - 12:54 PM

I changed one line:
fscanf(fp, "%d", &x[s]);



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[s]);
   	}


	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?
Was This Post Helpful? 0
  • +
  • -

#5 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: Reading from a file into an array

Posted 09 November 2008 - 01:23 PM

when you say read 3 characters into the array did you mean something like "abc"?
What you're trying to do is read three numbers into the array.
the %d format specifier means that the program should scan the file for a number
eg 123, 456789, etc
if you want to collect characters, do this:
char x[numberOfChar];
int s;
for (s = 0; s < numberOfChar; s++)
    fscanf(fp, "%c", &x[3]);


Was This Post Helpful? 0
  • +
  • -

#6 fmsguy06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 04-May 07

Re: Reading from a file into an array

Posted 09 November 2008 - 01:57 PM

View PostGWatt, on 9 Nov, 2008 - 12:23 PM, said:

when you say read 3 characters into the array did you mean something like "abc"?
What you're trying to do is read three numbers into the array.
the %d format specifier means that the program should scan the file for a number
eg 123, 456789, etc
if you want to collect characters, do this:
char x[numberOfChar];
int s;
for (s = 0; s < numberOfChar; s++)
    fscanf(fp, "%c", &x[3]);



That helped!


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


   	FILE *fp;
	fp=fopen("test.txt", "rb");
	if (fscanf(fp, "%d", &numberOfChar) == 1)
	printf("Number of characters printed: %d\n\n", numberOfChar);

int *x;
int n=numberOfChar;

if ( ( x = (int *)malloc(n*sizeof(int)) ) == NULL )
    {
    printf("\nError, memory not allocated.\n");
    exit(1);
    }


	
	int s;

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

	fscanf(fp, "%d", &x[s]);
	printf("%d ", x[s]);
	
   	
	}
	

    return 0;
}




That returns the first 3 characters, or if I change the first number to 15 it returns the first 15. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1