# Changing a text file to an array

• 10-14-2012
Amethyst
Changing a text file to an array
Hello, I was wondering it it's possible to change my textfile to an arrray.

For example this is my text file: 4x3 + 5x^2 + 3x + 2

I want to get the number of coefficients and the coefficients. That would look something like this

Code:

`double [number of coef]= {2,3,5,4}`
Is this possible? I have a hard time figuring out a code for this
• 10-14-2012
Salem
To read from a file, you will need these.
- fopen
- fgets

Then you might end up with a char buffer like this
Code:

`char buff[100] = "4x^3 + 5x^2 + 3x + 2";`
Next, write some code which identifies all the +/-, which gives you each of the terms of the polynomial.

Then you separate each term into a coefficient and a power (4 and 3, 5 and 2, 3 and 1, 2 and 0)
• 10-14-2012
Amethyst
Is this similar to stream? I'm a beginner and I'm really having a hard time implementing this.
• 10-15-2012
Salem
Changing
Code:

`char buff[100] = "4x^3 + 5x^2 + 3x + 2";`
Into
Code:

```char buff[100]; fgets(buff,sizeof(buff),fp);```
is trivial.

Focus on trying to split buff into it's components.

As a first exercise, try to get it to print the following
Found term=+4x^3
Found term=+5x^2
Found term=+3x^1
Found term=+2x^0

You can omit the red parts initially, but you should be able to infer them.
• 10-15-2012
oogabooga
Quote:

Originally Posted by Salem
Code:

```char buff[100]; fgets(buff,sizeof(buff),fp);```

As we all do on occasion I think you've got your forums crossed. :)
• 10-15-2012
iMalc
What parts of the C++ programming language have you covered in class so far, and what parts do you think you understand well enough so far?

Give an example of what the most complex program you have written so far did.