Changing a text file to an array

This is a discussion on Changing a text file to an array within the C++ Programming forums, part of the General Programming Boards category; Hello, I was wondering it it's possible to change my textfile to an arrray. For example this is my text ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    Question 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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    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)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Is this similar to stream? I'm a beginner and I'm really having a hard time implementing this.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Salem View Post
    Code:
    char buff[100];
    fgets(buff,sizeof(buff),fp);
    As we all do on occasion I think you've got your forums crossed.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help] Text file as a 1D or 2D array in C
    By Zallman in forum C Programming
    Replies: 10
    Last Post: 03-13-2011, 01:31 PM
  2. from text file to array
    By apisio in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2009, 06:34 AM
  3. Deleting / Changing a record in a text file
    By clearrtc in forum C Programming
    Replies: 9
    Last Post: 08-21-2006, 12:09 AM
  4. Read text from a file into an array
    By ccwash in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2005, 03:19 PM
  5. Text file to 2D Array PLEASE HELP!
    By lostboy101 in forum C Programming
    Replies: 0
    Last Post: 03-26-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21