6 Replies - 1744 Views - Last Post: 24 May 2012 - 07:41 AM Rate Topic: -----

#1 Larz.Lawoko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-May 12

Reading a text file into an array

Posted 24 May 2012 - 06:30 AM

The problem i need to work our goes like this:
we need to read two text files which will have a group of numbers in it in matrix form.
We need to read these files into an array then multiply the matrices together.

The trick is that the program is suppose to be flexible and be able to take in matrices of any size....
I can do the math part of the problem fine, i think, the problem lies in reading the file into the array.
The array seems to be copy the numbers from the file properly, but i have this weird error where the numbers in the array start to change, even if i'm not doing anything with the array. Ive spent 2+ hours trying to figure it out and now i go here to plead my case :P I have no idea what is going wrong.

here is the main part of the code that assigns the numbers to array:
while (fgets(line, sizeof(line), fA))
	{
		pnt = strtok (line, " ");
		for (xpos = 0;pnt != NULL;xpos++)
		{
			if (verbose == 2)
				printf("Assgining: %d to x pos %d and y pos %d of Array X \n", atoi(pnt), xpos, ypos);
			arryX[xpos][ypos] = atoi(pnt);
			pnt = strtok (NULL, " ");
		}
		ypos++;
		if (verbose == 2)
			printf("Next Line\n");
	}




Ill upload the whole code and an example txt file too
All programs compiled with "gcc test.c -o test"

P.s had to change file name extension because im "not permitted to upload this kind of file"

Thank you if you can help :bigsmile:

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Reading a text file into an array

#2 jimblumberg   User is offline

  • member icon

Reputation: 5537
  • View blog
  • Posts: 17,143
  • Joined: 25-December 09

Re: Reading a text file into an array

Posted 24 May 2012 - 06:41 AM

First you need to include stdlib.h in order to use atoi(). Next you should always check that you successfully opened your files. Also you should see about compiling with warnings and debugging enabled "gcc -d -Wall -Wextra test.c -o test". A couple of your printf() statements have a mismatch between the number of format specifiers and the number of arguments. Here are the warnings I got (after including stdlib.h).

Quote

main.c||In function ‘main’:|
main.c|12|warning: suggest explicit braces to avoid ambiguous ‘else’|
main.c|95|warning: too many arguments for format|
main.c|163|warning: too many arguments for format|
main.c|116|warning: unused variable ‘arryY’|
main.c|27|warning: unused variable ‘dimensionY_max’|
main.c|27|warning: unused variable ‘dimensionX_max’|
main.c|10|warning: unused variable ‘test’|
||=== Build finished: 0 errors, 7 warnings ===|


Lastly main() should be defined to return an int and you should return an int from this function, int main().


Jim
Was This Post Helpful? 1
  • +
  • -

#3 Larz.Lawoko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-May 12

Re: Reading a text file into an array

Posted 24 May 2012 - 06:49 AM

View Postjimblumberg, on 24 May 2012 - 06:41 AM, said:

First you need to include stdlib.h in order to use atoi(). Next you should always check that you successfully opened your files. Also you should see about compiling with warnings and debugging enabled "gcc -d -Wall -Wextra test.c -o test".


When i run that i get;
cc1: warning: command line option ‘-foutput-class-dir=-Wall’ is valid for Java but not for C [enabled by default]

and ill check for those errors thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Larz.Lawoko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-May 12

Re: Reading a text file into an array

Posted 24 May 2012 - 07:04 AM

So i have fixed the printf statement, included the library, and put brackets in the if statement, but there is still no change. The array is still getting edited even though im not using it at all past the first section of code, do you have any idea why that might be?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5537
  • View blog
  • Posts: 17,143
  • Joined: 25-December 09

Re: Reading a text file into an array

Posted 24 May 2012 - 07:07 AM

Post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 Larz.Lawoko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-May 12

Re: Reading a text file into an array

Posted 24 May 2012 - 07:15 AM

Here it is:

Attached File(s)

  • Attached File  test.txt (4.48K)
    Number of downloads: 73

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5537
  • View blog
  • Posts: 17,143
  • Joined: 25-December 09

Re: Reading a text file into an array

Posted 24 May 2012 - 07:41 AM

Fist in future post the code in the post inside code tags: :code:

Next I don't recommend the use of Variable Length Arrays (VLA) in this program. Either use constants to define your array size or use malloc/free. When I ran your program with your data files it appears that you are trying to create an array with a size of zero. At about line 33 you have the following code:
	fgets(line, sizeof(line), fA);

	char *pnt;
	pnt = strtok (line, " ");

	for (xsize = -1;pnt != NULL;xsize++)
	{
		pnt = strtok (NULL, " ");
	}

	for (ysize = 0;fgets(line, sizeof(line), fA) != NULL;ysize++)
	{
	}

	arryA_xsize = xsize;
	arryA_ysize = ysize;
        int arryA[xsize][ysize];// Note this is one of the VLA that I don't recommend.

When I evaluate xsize it evaluates to zero and ysize evaluates to 18. You can not declare arrays with a size of zero.

EDIT: You may find the blog helpful: Dynamically allocating and populating an array of ints in C.


Jim

This post has been edited by jimblumberg: 24 May 2012 - 07:47 AM
Reason for edit:: Added Link

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1